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"
30 static struct timeval tp1,tp2;
32 static void start_timer(void)
34 gettimeofday(&tp1,NULL);
37 static double end_timer(void)
39 gettimeofday(&tp2,NULL);
40 return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) -
41 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
45 static int timelimit = 10;
46 static int num_records = 10;
47 static int num_msgs = 1;
51 #define TESTKEY "testkey"
55 store a expanded record
56 send a message to next node to tell it to do the same
58 static void bench_fetch_1node(struct ctdb_context *ctdb)
60 TDB_DATA key, data, nulldata;
61 struct ctdb_db_context *ctdb_db;
62 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
64 struct ctdb_record_handle *h;
66 key.dptr = discard_const(TESTKEY);
67 key.dsize = strlen(TESTKEY);
69 ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
71 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
73 printf("Failed to fetch record '%s' on node %d\n",
74 (const char *)key.dptr, ctdb_get_vnn(ctdb));
79 if (data.dsize > 1000) {
83 if (data.dsize == 0) {
84 data.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "Test data\n");
86 data.dptr = (uint8_t *)talloc_asprintf_append((char *)data.dptr,
87 "msg_count=%d on node %d\n",
88 msg_count, ctdb_get_vnn(ctdb));
89 data.dsize = strlen((const char *)data.dptr)+1;
91 ret = ctdb_record_store(h, data);
94 printf("Failed to store record\n");
99 /* tell the next node to do the same */
100 nulldata.dptr = NULL;
103 dest = (ctdb_get_vnn(ctdb) + 1) % ctdb_get_num_nodes(ctdb);
104 ctdb_send_message(ctdb, dest, 0, nulldata);
108 handler for messages in bench_ring()
110 static void message_handler(struct ctdb_context *ctdb, uint32_t srvid,
111 TDB_DATA data, void *private_data)
114 bench_fetch_1node(ctdb);
119 benchmark the following:
122 store a expanded record
123 send a message to next node to tell it to do the same
126 static void bench_fetch(struct ctdb_context *ctdb, struct event_context *ev)
128 int vnn=ctdb_get_vnn(ctdb);
130 if (vnn == ctdb_get_num_nodes(ctdb)-1) {
131 bench_fetch_1node(ctdb);
136 while (end_timer() < timelimit) {
137 if (vnn == 0 && msg_count % 100 == 0) {
138 printf("Fetch: %.2f msgs/sec\r", msg_count/end_timer());
141 if (event_loop_once(ev) != 0) {
142 printf("Event loop failed!\n");
147 talloc_report_null_full();
151 printf("Fetch: %.2f msgs/sec\n", msg_count/end_timer());
154 enum my_functions {FUNC_FETCH=1};
157 ctdb call function to fetch a record
159 static int fetch_func(struct ctdb_call_info *call)
161 call->reply_data = &call->record_data;
168 int main(int argc, const char *argv[])
170 struct ctdb_context *ctdb;
171 struct ctdb_db_context *ctdb_db;
173 struct poptOption popt_options[] = {
176 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
177 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
178 { "num-msgs", 'n', POPT_ARG_INT, &num_msgs, 0, "num_msgs", "integer" },
182 const char **extra_argv;
186 struct event_context *ev;
187 struct ctdb_call call;
189 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
191 while ((opt = poptGetNextOpt(pc)) != -1) {
194 fprintf(stderr, "Invalid option %s: %s\n",
195 poptBadOption(pc, 0), poptStrerror(opt));
200 /* talloc_enable_leak_report_full(); */
202 /* setup the remaining options for the main program to use */
203 extra_argv = poptGetArgs(pc);
206 while (extra_argv[extra_argc]) extra_argc++;
209 ev = event_context_init(NULL);
211 ctdb = ctdb_cmdline_init(ev);
213 /* attach to a specific database */
214 ctdb_db = ctdb_attach(ctdb, "test.tdb", TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0666);
216 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
220 ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
222 /* start the protocol running */
223 ret = ctdb_start(ctdb);
225 ctdb_set_message_handler(ctdb, 0, message_handler, &msg_count);
227 /* wait until all nodes are connected (should not be needed
228 outside of test code) */
229 ctdb_connect_wait(ctdb);
231 bench_fetch(ctdb, ev);
234 call.key.dptr = discard_const(TESTKEY);
235 call.key.dsize = strlen(TESTKEY);
237 /* fetch the record */
238 call.call_id = FUNC_FETCH;
239 call.call_data.dptr = NULL;
240 call.call_data.dsize = 0;
242 ret = ctdb_call(ctdb_db, &call);
244 printf("ctdb_call FUNC_FETCH failed - %s\n", ctdb_errstr(ctdb));
248 printf("DATA:\n%s\n", (char *)call.reply_data.dptr);
250 /* go into a wait loop to allow other nodes to complete */