4 Copyright (C) Andrew Tridgell 2006
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
29 static struct timeval tp1,tp2;
31 static void start_timer(void)
33 gettimeofday(&tp1,NULL);
36 static double end_timer(void)
38 gettimeofday(&tp2,NULL);
39 return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) -
40 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
44 static int timelimit = 10;
45 static int num_records = 10;
46 static int num_msgs = 1;
50 #define TESTKEY "testkey"
54 store a expanded record
55 send a message to next node to tell it to do the same
57 static void bench_fetch_1node(struct ctdb_context *ctdb)
59 TDB_DATA key, data, nulldata;
60 struct ctdb_record_handle *rec;
61 struct ctdb_db_context *ctdb_db;
62 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
65 key.dptr = discard_const("testkey");
66 key.dsize = strlen((const char *)key.dptr);
68 ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
70 rec = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
72 printf("Failed to fetch record '%s' on node %d\n",
73 (const char *)key.dptr, ctdb_get_vnn(ctdb));
78 if (data.dsize > 1000) {
82 if (data.dsize == 0) {
83 data.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "Test data\n");
85 data.dptr = (uint8_t *)talloc_asprintf_append((char *)data.dptr,
86 "msg_count=%d on node %d\n",
87 msg_count, ctdb_get_vnn(ctdb));
88 data.dsize = strlen((const char *)data.dptr)+1;
90 ret = ctdb_store_unlock(rec, data);
92 printf("Failed to store record\n");
97 /* tell the next node to do the same */
101 dest = (ctdb_get_vnn(ctdb) + 1) % ctdb_get_num_nodes(ctdb);
102 ctdb_send_message(ctdb, dest, 0, nulldata);
106 handler for messages in bench_ring()
108 static void message_handler(struct ctdb_context *ctdb, uint32_t srvid,
109 TDB_DATA data, void *private_data)
112 bench_fetch_1node(ctdb);
117 benchmark the following:
120 store a expanded record
121 send a message to next node to tell it to do the same
124 static void bench_fetch(struct ctdb_context *ctdb, struct event_context *ev)
126 int vnn=ctdb_get_vnn(ctdb);
128 if (vnn == ctdb_get_num_nodes(ctdb)-1) {
129 bench_fetch_1node(ctdb);
134 while (end_timer() < timelimit) {
135 if (vnn == 0 && msg_count % 100 == 0) {
136 printf("Fetch: %.2f msgs/sec\r", msg_count/end_timer());
139 if (event_loop_once(ev) != 0) {
140 printf("Event loop failed!\n");
145 printf("Fetch: %.2f msgs/sec\n", msg_count/end_timer());
148 enum my_functions {FUNC_FETCH=1};
151 ctdb call function to fetch a record
153 static int fetch_func(struct ctdb_call_info *call)
155 call->reply_data = &call->record_data;
162 int main(int argc, const char *argv[])
164 struct ctdb_context *ctdb;
165 struct ctdb_db_context *ctdb_db;
166 const char *nlist = NULL;
167 const char *transport = "tcp";
168 const char *myaddress = NULL;
172 struct poptOption popt_options[] = {
174 { "nlist", 0, POPT_ARG_STRING, &nlist, 0, "node list file", "filename" },
175 { "listen", 0, POPT_ARG_STRING, &myaddress, 0, "address to listen on", "address" },
176 { "transport", 0, POPT_ARG_STRING, &transport, 0, "protocol transport", NULL },
177 { "self-connect", 0, POPT_ARG_NONE, &self_connect, 0, "enable self connect", "boolean" },
178 { "daemon", 0, POPT_ARG_NONE, &daemon_mode, 0, "spawn a ctdb daemon", "boolean" },
179 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
180 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
181 { "num-msgs", 'n', POPT_ARG_INT, &num_msgs, 0, "num_msgs", "integer" },
185 const char **extra_argv;
189 struct event_context *ev;
190 struct ctdb_call call;
192 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
194 while ((opt = poptGetNextOpt(pc)) != -1) {
197 fprintf(stderr, "Invalid option %s: %s\n",
198 poptBadOption(pc, 0), poptStrerror(opt));
203 /* setup the remaining options for the main program to use */
204 extra_argv = poptGetArgs(pc);
207 while (extra_argv[extra_argc]) extra_argc++;
210 if (nlist == NULL || myaddress == NULL) {
211 printf("You must provide a node list with --nlist and an address with --listen\n");
215 ev = event_context_init(NULL);
217 /* initialise ctdb */
218 ctdb = ctdb_init(ev);
220 printf("Failed to init ctdb\n");
225 ctdb_set_flags(ctdb, CTDB_FLAG_SELF_CONNECT);
228 ctdb_set_flags(ctdb, CTDB_FLAG_DAEMON_MODE);
231 ret = ctdb_set_transport(ctdb, transport);
233 printf("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb));
237 /* tell ctdb what address to listen on */
238 ret = ctdb_set_address(ctdb, myaddress);
240 printf("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb));
244 /* tell ctdb what nodes are available */
245 ret = ctdb_set_nlist(ctdb, nlist);
247 printf("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb));
251 /* attach to a specific database */
252 ctdb_db = ctdb_attach(ctdb, "test.tdb", TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0666);
254 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
258 ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
260 /* start the protocol running */
261 ret = ctdb_start(ctdb);
263 ctdb_set_message_handler(ctdb, 0, message_handler, &msg_count);
265 /* wait until all nodes are connected (should not be needed
266 outside of test code) */
267 ctdb_connect_wait(ctdb);
269 bench_fetch(ctdb, ev);
272 call.key.dptr = discard_const(TESTKEY);
273 call.key.dsize = strlen(TESTKEY);
275 /* fetch the record */
276 call.call_id = FUNC_FETCH;
277 call.call_data.dptr = NULL;
278 call.call_data.dsize = 0;
280 ret = ctdb_call(ctdb_db, &call);
282 printf("ctdb_call FUNC_FETCH failed - %s\n", ctdb_errstr(ctdb));
286 printf("DATA:\n%s\n", (char *)call.reply_data.dptr);