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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "lib/events/events.h"
22 #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_db_context *ctdb_db;
61 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
63 struct ctdb_record_handle *h;
65 key.dptr = discard_const(TESTKEY);
66 key.dsize = strlen(TESTKEY);
68 ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
70 h = 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_record_store(h, data);
93 printf("Failed to store record\n");
98 /* tell the next node to do the same */
102 dest = (ctdb_get_vnn(ctdb) + 1) % ctdb_get_num_nodes(ctdb);
103 ctdb_send_message(ctdb, dest, 0, nulldata);
107 handler for messages in bench_ring()
109 static void message_handler(struct ctdb_context *ctdb, uint32_t srvid,
110 TDB_DATA data, void *private_data)
113 bench_fetch_1node(ctdb);
118 benchmark the following:
121 store a expanded record
122 send a message to next node to tell it to do the same
125 static void bench_fetch(struct ctdb_context *ctdb, struct event_context *ev)
127 int vnn=ctdb_get_vnn(ctdb);
129 if (vnn == ctdb_get_num_nodes(ctdb)-1) {
130 bench_fetch_1node(ctdb);
135 while (end_timer() < timelimit) {
136 if (vnn == 0 && msg_count % 100 == 0) {
137 printf("Fetch: %.2f msgs/sec\r", msg_count/end_timer());
140 if (event_loop_once(ev) != 0) {
141 printf("Event loop failed!\n");
146 talloc_report_null_full();
150 printf("Fetch: %.2f msgs/sec\n", msg_count/end_timer());
153 enum my_functions {FUNC_FETCH=1};
156 ctdb call function to fetch a record
158 static int fetch_func(struct ctdb_call_info *call)
160 call->reply_data = &call->record_data;
167 int main(int argc, const char *argv[])
169 struct ctdb_context *ctdb;
170 struct ctdb_db_context *ctdb_db;
172 struct poptOption popt_options[] = {
175 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
176 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
177 { "num-msgs", 'n', POPT_ARG_INT, &num_msgs, 0, "num_msgs", "integer" },
181 const char **extra_argv;
185 struct event_context *ev;
186 struct ctdb_call call;
188 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
190 while ((opt = poptGetNextOpt(pc)) != -1) {
193 fprintf(stderr, "Invalid option %s: %s\n",
194 poptBadOption(pc, 0), poptStrerror(opt));
199 /* talloc_enable_leak_report_full(); */
201 /* setup the remaining options for the main program to use */
202 extra_argv = poptGetArgs(pc);
205 while (extra_argv[extra_argc]) extra_argc++;
208 ev = event_context_init(NULL);
210 ctdb = ctdb_cmdline_init(ev);
212 /* attach to a specific database */
213 ctdb_db = ctdb_attach(ctdb, "test.tdb", TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0666);
215 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
219 ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
221 /* start the protocol running */
222 ret = ctdb_start(ctdb);
224 ctdb_set_message_handler(ctdb, 0, message_handler, &msg_count);
226 /* wait until all nodes are connected (should not be needed
227 outside of test code) */
228 ctdb_connect_wait(ctdb);
230 bench_fetch(ctdb, ev);
233 call.key.dptr = discard_const(TESTKEY);
234 call.key.dsize = strlen(TESTKEY);
236 /* fetch the record */
237 call.call_id = FUNC_FETCH;
238 call.call_data.dptr = NULL;
239 call.call_data.dsize = 0;
241 ret = ctdb_call(ctdb_db, &call);
243 printf("ctdb_call FUNC_FETCH failed - %s\n", ctdb_errstr(ctdb));
247 printf("DATA:\n%s\n", (char *)call.reply_data.dptr);
249 /* go into a wait loop to allow other nodes to complete */