4 Copyright (C) Andrew Tridgell 2006
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
28 static struct timeval tp1
,tp2
;
30 static void start_timer(void)
32 gettimeofday(&tp1
,NULL
);
35 static double end_timer(void)
37 gettimeofday(&tp2
,NULL
);
38 return (tp2
.tv_sec
+ (tp2
.tv_usec
*1.0e-6)) -
39 (tp1
.tv_sec
+ (tp1
.tv_usec
*1.0e-6));
43 static int timelimit
= 10;
44 static int num_records
= 10;
48 #define TESTKEY "testkey"
52 store a expanded record
53 send a message to next node to tell it to do the same
55 static void bench_fetch_1node(struct ctdb_context
*ctdb
)
57 TDB_DATA key
, data
, nulldata
;
58 struct ctdb_db_context
*ctdb_db
;
59 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
61 struct ctdb_record_handle
*h
;
63 key
.dptr
= discard_const(TESTKEY
);
64 key
.dsize
= strlen(TESTKEY
);
66 ctdb_db
= ctdb_db_handle(ctdb
, "test.tdb");
68 h
= ctdb_fetch_lock(ctdb_db
, tmp_ctx
, key
, &data
);
70 printf("Failed to fetch record '%s' on node %d\n",
71 (const char *)key
.dptr
, ctdb_get_pnn(ctdb
));
76 if (data
.dsize
> 1000) {
80 if (data
.dsize
== 0) {
81 data
.dptr
= (uint8_t *)talloc_asprintf(tmp_ctx
, "Test data\n");
83 data
.dptr
= (uint8_t *)talloc_asprintf_append((char *)data
.dptr
,
84 "msg_count=%d on node %d\n",
85 msg_count
, ctdb_get_pnn(ctdb
));
86 if (data
.dptr
== NULL
) {
87 printf("Failed to create record\n");
91 data
.dsize
= strlen((const char *)data
.dptr
)+1;
93 ret
= ctdb_record_store(h
, data
);
96 printf("Failed to store record\n");
101 /* tell the next node to do the same */
102 nulldata
.dptr
= NULL
;
105 dest
= (ctdb_get_pnn(ctdb
) + 1) % num_nodes
;
106 ctdb_client_send_message(ctdb
, dest
, 0, nulldata
);
110 handler for messages in bench_ring()
112 static void message_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
113 TDB_DATA data
, void *private_data
)
116 bench_fetch_1node(ctdb
);
121 * timeout handler - noop
123 static void timeout_handler(struct event_context
*ev
, struct timed_event
*timer
,
124 struct timeval curtime
, void *private_data
)
130 benchmark the following:
133 store a expanded record
134 send a message to next node to tell it to do the same
137 static void bench_fetch(struct ctdb_context
*ctdb
, struct event_context
*ev
)
139 int pnn
=ctdb_get_pnn(ctdb
);
141 if (pnn
== num_nodes
- 1) {
142 bench_fetch_1node(ctdb
);
146 event_add_timed(ev
, ctdb
, timeval_current_ofs(timelimit
,0), timeout_handler
, NULL
);
148 while (end_timer() < timelimit
) {
149 if (pnn
== 0 && msg_count
% 100 == 0 && end_timer() > 0) {
150 printf("Fetch: %.2f msgs/sec\r", msg_count
/end_timer());
153 if (event_loop_once(ev
) != 0) {
154 printf("Event loop failed!\n");
159 printf("Fetch: %.2f msgs/sec\n", msg_count
/end_timer());
163 handler for reconfigure message
165 static void reconfigure_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
166 TDB_DATA data
, void *private_data
)
168 int *ready
= (int *)private_data
;
175 int main(int argc
, const char *argv
[])
177 struct ctdb_context
*ctdb
;
178 struct ctdb_db_context
*ctdb_db
;
180 struct poptOption popt_options
[] = {
183 { "timelimit", 't', POPT_ARG_INT
, &timelimit
, 0, "timelimit", "integer" },
184 { "num-records", 'r', POPT_ARG_INT
, &num_records
, 0, "num_records", "integer" },
185 { NULL
, 'n', POPT_ARG_INT
, &num_nodes
, 0, "num_nodes", "integer" },
189 const char **extra_argv
;
192 struct event_context
*ev
;
194 struct ctdb_record_handle
*h
;
197 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
199 while ((opt
= poptGetNextOpt(pc
)) != -1) {
202 fprintf(stderr
, "Invalid option %s: %s\n",
203 poptBadOption(pc
, 0), poptStrerror(opt
));
208 /* talloc_enable_leak_report_full(); */
210 /* setup the remaining options for the main program to use */
211 extra_argv
= poptGetArgs(pc
);
214 while (extra_argv
[extra_argc
]) extra_argc
++;
217 if (num_nodes
== 0) {
218 printf("You must specify the number of nodes\n");
222 ev
= event_context_init(NULL
);
223 tevent_loop_allow_nesting(ev
);
225 ctdb
= ctdb_cmdline_client(ev
, timeval_current_ofs(3, 0));
228 printf("failed to connect to ctdb daemon.\n");
232 ctdb_client_set_message_handler(ctdb
, CTDB_SRVID_RECONFIGURE
, reconfigure_handler
,
235 /* attach to a specific database */
236 ctdb_db
= ctdb_attach(ctdb
, timeval_current_ofs(2, 0), "test.tdb",
239 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb
));
243 ctdb_client_set_message_handler(ctdb
, 0, message_handler
, &msg_count
);
245 printf("Waiting for cluster\n");
248 ctdb_ctrl_getrecmode(ctdb
, ctdb
, timeval_zero(), CTDB_CURRENT_NODE
, &recmode
);
249 if (recmode
== 0) break;
253 /* This test has a race condition. If CTDB receives the message from previous
254 * node, before this node has registered for that message, this node will never
255 * receive that message and will block on receive. Sleeping for some time will
256 * hopefully ensure that the test program on all the nodes register for messages.
258 printf("Sleeping for %d seconds\n", num_nodes
);
260 bench_fetch(ctdb
, ev
);
262 key
.dptr
= discard_const(TESTKEY
);
263 key
.dsize
= strlen(TESTKEY
);
265 printf("Fetching final record\n");
267 h
= ctdb_fetch_lock(ctdb_db
, ctdb
, key
, &data
);
270 printf("Failed to fetch record '%s' on node %d\n",
271 (const char *)key
.dptr
, ctdb_get_pnn(ctdb
));
275 printf("DATA:\n%s\n", (char *)data
.dptr
);