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