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 "lib/tevent/tevent.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;
49 #define TESTKEY "testkey"
53 store a expanded record
54 send a message to next node to tell it to do the same
56 static void bench_fetch_1node(struct ctdb_context *ctdb)
58 TDB_DATA key, data, nulldata;
59 struct ctdb_db_context *ctdb_db;
60 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
62 struct ctdb_record_handle *h;
64 key.dptr = discard_const(TESTKEY);
65 key.dsize = strlen(TESTKEY);
67 ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
69 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
71 printf("Failed to fetch record '%s' on node %d\n",
72 (const char *)key.dptr, ctdb_get_pnn(ctdb));
77 if (data.dsize > 1000) {
81 if (data.dsize == 0) {
82 data.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "Test data\n");
84 data.dptr = (uint8_t *)talloc_asprintf_append((char *)data.dptr,
85 "msg_count=%d on node %d\n",
86 msg_count, ctdb_get_pnn(ctdb));
87 data.dsize = strlen((const char *)data.dptr)+1;
89 ret = ctdb_record_store(h, data);
92 printf("Failed to store record\n");
97 /* tell the next node to do the same */
101 dest = (ctdb_get_pnn(ctdb) + 1) % num_nodes;
102 ctdb_client_send_message(ctdb, dest, 0, nulldata);
106 handler for messages in bench_ring()
108 static void message_handler(struct ctdb_context *ctdb, uint64_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 pnn=ctdb_get_pnn(ctdb);
128 if (pnn == num_nodes - 1) {
129 bench_fetch_1node(ctdb);
134 while (end_timer() < timelimit) {
135 if (pnn == 0 && msg_count % 100 == 0 && end_timer() > 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());
149 handler for reconfigure message
151 static void reconfigure_handler(struct ctdb_context *ctdb, uint64_t srvid,
152 TDB_DATA data, void *private_data)
154 int *ready = (int *)private_data;
161 int main(int argc, const char *argv[])
163 struct ctdb_context *ctdb;
164 struct ctdb_db_context *ctdb_db;
166 struct poptOption popt_options[] = {
169 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
170 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
171 { NULL, 'n', POPT_ARG_INT, &num_nodes, 0, "num_nodes", "integer" },
175 const char **extra_argv;
178 struct event_context *ev;
180 struct ctdb_record_handle *h;
183 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
185 while ((opt = poptGetNextOpt(pc)) != -1) {
188 fprintf(stderr, "Invalid option %s: %s\n",
189 poptBadOption(pc, 0), poptStrerror(opt));
194 /* talloc_enable_leak_report_full(); */
196 /* setup the remaining options for the main program to use */
197 extra_argv = poptGetArgs(pc);
200 while (extra_argv[extra_argc]) extra_argc++;
203 if (num_nodes == 0) {
204 printf("You must specify the number of nodes\n");
208 ev = event_context_init(NULL);
210 ctdb = ctdb_cmdline_client(ev);
212 ctdb_client_set_message_handler(ctdb, CTDB_SRVID_RECONFIGURE, reconfigure_handler,
215 /* attach to a specific database */
216 ctdb_db = ctdb_attach(ctdb, "test.tdb", false, 0);
218 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
222 ctdb_client_set_message_handler(ctdb, 0, message_handler, &msg_count);
224 printf("Waiting for cluster\n");
227 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
228 if (recmode == 0) break;
232 bench_fetch(ctdb, ev);
234 key.dptr = discard_const(TESTKEY);
235 key.dsize = strlen(TESTKEY);
237 printf("Fetching final record\n");
239 h = ctdb_fetch_lock(ctdb_db, ctdb, key, &data);
242 printf("Failed to fetch record '%s' on node %d\n",
243 (const char *)key.dptr, ctdb_get_pnn(ctdb));
247 printf("DATA:\n%s\n", (char *)data.dptr);