ctdb-util: Rename db_wrap to tdb_wrap and make it a build subsystem
[Samba.git] / ctdb / tests / src / ctdb_fetch_one.c
blobba0e183fe57b814b0b5b07cf9b93f18d8cc5663f
1 /*
2 simple ctdb benchmark
3 This test just fetch_locks a record and releases it in a loop.
5 Copyright (C) Ronnie Sahlberg 2009
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.
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.
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/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "popt.h"
24 #include "cmdline.h"
26 #include <sys/time.h>
27 #include <time.h>
29 static int timelimit = 10;
30 static int lock_count = 0;
32 static struct ctdb_db_context *ctdb_db;
34 #define TESTKEY "testkey"
37 static void alarm_handler(int sig)
39 printf("Locks:%d\n", lock_count);
40 lock_count=0;
42 timelimit--;
43 if (timelimit <= 0) {
44 exit(0);
46 alarm(1);
50 Just try locking/unlocking the same record over and over
52 static void bench_fetch_one_loop(struct ctdb_context *ctdb, struct event_context *ev)
54 TDB_DATA key, data;
56 key.dptr = discard_const(TESTKEY);
57 key.dsize = strlen(TESTKEY);
60 while (1) {
61 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
62 struct ctdb_record_handle *h;
64 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
65 if (h == NULL) {
66 printf("Failed to fetch record '%s' on node %d\n",
67 (const char *)key.dptr, ctdb_get_pnn(ctdb));
68 talloc_free(tmp_ctx);
69 continue;
72 talloc_free(tmp_ctx);
73 lock_count++;
78 main program
80 int main(int argc, const char *argv[])
82 struct ctdb_context *ctdb;
84 struct poptOption popt_options[] = {
85 POPT_AUTOHELP
86 POPT_CTDB_CMDLINE
87 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
88 POPT_TABLEEND
90 int opt;
91 const char **extra_argv;
92 int extra_argc = 0;
93 poptContext pc;
94 struct event_context *ev;
96 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
98 while ((opt = poptGetNextOpt(pc)) != -1) {
99 switch (opt) {
100 default:
101 fprintf(stderr, "Invalid option %s: %s\n",
102 poptBadOption(pc, 0), poptStrerror(opt));
103 exit(1);
107 /* setup the remaining options for the main program to use */
108 extra_argv = poptGetArgs(pc);
109 if (extra_argv) {
110 extra_argv++;
111 while (extra_argv[extra_argc]) extra_argc++;
114 ev = event_context_init(NULL);
116 ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
118 if (ctdb == NULL) {
119 printf("failed to connect to ctdb daemon.\n");
120 exit(1);
123 /* attach to a specific database */
124 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0), "test.tdb",
125 false, 0);
126 if (!ctdb_db) {
127 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
128 exit(1);
131 printf("Waiting for cluster\n");
132 while (1) {
133 uint32_t recmode=1;
134 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
135 if (recmode == 0) break;
136 event_loop_once(ev);
139 signal(SIGALRM, alarm_handler);
140 alarm(1);
142 bench_fetch_one_loop(ctdb, ev);
144 return 0;