Typo deamon -> daemon
[sahlberg/ctdb.git] / tests / src / ctdb_fetch_one.c
1 /* 
2    simple ctdb benchmark
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 "includes.h"
22 #include "lib/tevent/tevent.h"
23 #include "system/filesys.h"
24 #include "popt.h"
25 #include "cmdline.h"
26
27 #include <sys/time.h>
28 #include <time.h>
29
30 static int timelimit = 10;
31 static int lock_count = 0;
32
33 static struct ctdb_db_context *ctdb_db;
34
35 #define TESTKEY "testkey"
36
37
38 static void alarm_handler(int sig)
39 {
40         printf("Locks:%d\n", lock_count);
41         lock_count=0;
42
43         timelimit--;
44         if (timelimit <= 0) {
45                 exit(0);
46         }
47         alarm(1);
48 }
49
50 /*
51         Just try locking/unlocking the same record over and over
52 */
53 static void bench_fetch_one_loop(struct ctdb_context *ctdb, struct event_context *ev)
54 {
55         TDB_DATA key, data;
56
57         key.dptr = discard_const(TESTKEY);
58         key.dsize = strlen(TESTKEY);
59
60
61         while (1) {
62                 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
63                 struct ctdb_record_handle *h;
64
65                 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
66                 if (h == NULL) {
67                         printf("Failed to fetch record '%s' on node %d\n", 
68                                 (const char *)key.dptr, ctdb_get_pnn(ctdb));
69                         talloc_free(tmp_ctx);
70                         continue;
71                 }
72
73                 talloc_free(tmp_ctx);
74                 lock_count++;
75         }
76 }
77
78 /*
79   main program
80 */
81 int main(int argc, const char *argv[])
82 {
83         struct ctdb_context *ctdb;
84
85         struct poptOption popt_options[] = {
86                 POPT_AUTOHELP
87                 POPT_CTDB_CMDLINE
88                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
89                 POPT_TABLEEND
90         };
91         int opt;
92         const char **extra_argv;
93         int extra_argc = 0;
94         poptContext pc;
95         struct event_context *ev;
96
97         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
98
99         while ((opt = poptGetNextOpt(pc)) != -1) {
100                 switch (opt) {
101                 default:
102                         fprintf(stderr, "Invalid option %s: %s\n", 
103                                 poptBadOption(pc, 0), poptStrerror(opt));
104                         exit(1);
105                 }
106         }
107
108         /* setup the remaining options for the main program to use */
109         extra_argv = poptGetArgs(pc);
110         if (extra_argv) {
111                 extra_argv++;
112                 while (extra_argv[extra_argc]) extra_argc++;
113         }
114
115         ev = event_context_init(NULL);
116
117         ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
118
119         if (ctdb == NULL) {
120                 printf("failed to connect to ctdb daemon.\n");
121                 exit(1);
122         }
123
124         /* attach to a specific database */
125         ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0), "test.tdb",
126                               false, 0);
127         if (!ctdb_db) {
128                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
129                 exit(1);
130         }
131
132         printf("Waiting for cluster\n");
133         while (1) {
134                 uint32_t recmode=1;
135                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
136                 if (recmode == 0) break;
137                 event_loop_once(ev);
138         }
139
140         signal(SIGALRM, alarm_handler);
141         alarm(1);
142
143         bench_fetch_one_loop(ctdb, ev);
144
145         return 0;
146 }