ReadOnly: update the loop test tool to print number of fetches per second
[sahlberg/ctdb.git] / tests / src / ctdb_fetch_readonly_loop.c
1 /* 
2    simple ctdb test tool
3    This test just fetch_locks a record and releases it in a loop.
4
5    Copyright (C) Ronnie Sahlberg 2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <time.h>
22 #include "includes.h"
23 #include "lib/tevent/tevent.h"
24 #include "system/filesys.h"
25 #include "popt.h"
26 #include "cmdline.h"
27 #include "ctdb_private.h"
28
29 static struct ctdb_db_context *ctdb_db;
30
31 const char *TESTKEY = "testkey";
32 static int count;
33
34 /*
35         Just try locking/unlocking a single record once
36 */
37 static void fetch_lock_once(struct ctdb_context *ctdb, struct event_context *ev)
38 {
39         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
40         TDB_DATA key, data;
41         struct ctdb_record_handle *h;
42         static time_t t = 0, t2;
43
44         key.dptr = discard_const(TESTKEY);
45         key.dsize = strlen(TESTKEY);
46
47 //      printf("Trying to fetch lock the record ...\n");
48
49         h = ctdb_fetch_readonly_lock(ctdb_db, tmp_ctx, key, &data, true);
50         if (h == NULL) {
51                 printf("Failed to fetch record '%s' on node %d\n", 
52                         (const char *)key.dptr, ctdb_get_pnn(ctdb));
53                 talloc_free(tmp_ctx);
54                 exit(10);
55         }
56
57         count++;
58         t2 = time(NULL);
59         if (t != 0 && t != t2) {
60                 static int last_count = 0;
61
62                 printf("count : %d\n", count - last_count);
63                 last_count = count;
64         }
65         t = t2;
66
67         talloc_free(tmp_ctx);
68 }
69
70 /*
71   main program
72 */
73 int main(int argc, const char *argv[])
74 {
75         struct ctdb_context *ctdb;
76         TDB_DATA key;
77
78         struct poptOption popt_options[] = {
79                 POPT_AUTOHELP
80                 POPT_CTDB_CMDLINE
81                 { "record",      'r', POPT_ARG_STRING, &TESTKEY, 0, "record", "string" },
82                 POPT_TABLEEND
83         };
84         int opt, ret;
85         const char **extra_argv;
86         int extra_argc = 0;
87         poptContext pc;
88         struct event_context *ev;
89
90         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
91
92         while ((opt = poptGetNextOpt(pc)) != -1) {
93                 switch (opt) {
94                 default:
95                         fprintf(stderr, "Invalid option %s: %s\n", 
96                                 poptBadOption(pc, 0), poptStrerror(opt));
97                         exit(1);
98                 }
99         }
100
101         /* setup the remaining options for the main program to use */
102         extra_argv = poptGetArgs(pc);
103         if (extra_argv) {
104                 extra_argv++;
105                 while (extra_argv[extra_argc]) extra_argc++;
106         }
107
108         ev = event_context_init(NULL);
109
110         ctdb = ctdb_cmdline_client(ev);
111
112         key.dptr  = discard_const(TESTKEY);
113         key.dsize = strlen(TESTKEY);
114
115         ret = ctdb_ctrl_getvnnmap(ctdb, timeval_zero(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
116         if (ret != 0) {
117                 printf("failed to get vnnmap\n");
118                 exit(10);
119         }
120         printf("Record:%s\n", TESTKEY);
121         printf("Lmaster : %d\n", ctdb_lmaster(ctdb, &key)); 
122
123         /* attach to a specific database */
124         ctdb_db = ctdb_attach(ctdb, "test.tdb", false, 0);
125         if (!ctdb_db) {
126                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
127                 exit(1);
128         }
129
130         printf("Waiting for cluster\n");
131         while (1) {
132                 uint32_t recmode=1;
133                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
134                 if (recmode == 0) break;
135                 event_loop_once(ev);
136         }
137
138         while (1) {
139                 fetch_lock_once(ctdb, ev);
140         }
141
142         return 0;
143 }