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/events/events.h"
22 #include "system/filesys.h"
25 #include "include/ctdb_protocol.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;
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_pnn(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_pnn(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_pnn(ctdb) + 1) % num_nodes;
103 ctdb_send_message(ctdb, dest, 0, nulldata);
107 handler for messages in bench_ring()
109 static void message_handler(struct ctdb_context *ctdb, uint64_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 pnn=ctdb_get_pnn(ctdb);
129 if (pnn == num_nodes - 1) {
130 bench_fetch_1node(ctdb);
135 while (end_timer() < timelimit) {
136 if (pnn == 0 && msg_count % 100 == 0 && end_timer() > 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 printf("Fetch: %.2f msgs/sec\n", msg_count/end_timer());
150 handler for reconfigure message
152 static void reconfigure_handler(struct ctdb_context *ctdb, uint64_t srvid,
153 TDB_DATA data, void *private_data)
155 int *ready = (int *)private_data;
162 int main(int argc, const char *argv[])
164 struct ctdb_context *ctdb;
165 struct ctdb_db_context *ctdb_db;
167 struct poptOption popt_options[] = {
170 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
171 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
172 { NULL, 'n', POPT_ARG_INT, &num_nodes, 0, "num_nodes", "integer" },
176 const char **extra_argv;
179 struct event_context *ev;
181 struct ctdb_record_handle *h;
184 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
186 while ((opt = poptGetNextOpt(pc)) != -1) {
189 fprintf(stderr, "Invalid option %s: %s\n",
190 poptBadOption(pc, 0), poptStrerror(opt));
195 /* talloc_enable_leak_report_full(); */
197 /* setup the remaining options for the main program to use */
198 extra_argv = poptGetArgs(pc);
201 while (extra_argv[extra_argc]) extra_argc++;
204 if (num_nodes == 0) {
205 printf("You must specify the number of nodes\n");
209 ev = event_context_init(NULL);
211 ctdb = ctdb_cmdline_client(ev);
213 ctdb_set_message_handler(ctdb, CTDB_SRVID_RECONFIGURE, reconfigure_handler,
216 /* attach to a specific database */
217 ctdb_db = ctdb_attach(ctdb, "test.tdb", false, 0);
219 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
223 ctdb_set_message_handler(ctdb, 0, message_handler, &msg_count);
225 printf("Waiting for cluster\n");
228 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
229 if (recmode == 0) break;
233 bench_fetch(ctdb, ev);
235 key.dptr = discard_const(TESTKEY);
236 key.dsize = strlen(TESTKEY);
238 printf("Fetching final record\n");
240 h = ctdb_fetch_lock(ctdb_db, ctdb, key, &data);
243 printf("Failed to fetch record '%s' on node %d\n",
244 (const char *)key.dptr, ctdb_get_pnn(ctdb));
248 printf("DATA:\n%s\n", (char *)data.dptr);