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"
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_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) {
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;
160
handler for reconfigure message
162
static void reconfigure_handler(struct ctdb_context *ctdb, uint64_t srvid,
163
TDB_DATA data, void *private_data)
165
int *ready = (int *)private_data;
172
int main(int argc, const char *argv[])
174
struct ctdb_context *ctdb;
175
struct ctdb_db_context *ctdb_db;
177
struct poptOption popt_options[] = {
180
{ "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
181
{ "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
182
{ NULL, 'n', POPT_ARG_INT, &num_nodes, 0, "num_nodes", "integer" },
186
const char **extra_argv;
190
struct event_context *ev;
191
struct ctdb_call call;
194
pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
196
while ((opt = poptGetNextOpt(pc)) != -1) {
199
fprintf(stderr, "Invalid option %s: %s\n",
200
poptBadOption(pc, 0), poptStrerror(opt));
205
/* talloc_enable_leak_report_full(); */
207
/* setup the remaining options for the main program to use */
208
extra_argv = poptGetArgs(pc);
211
while (extra_argv[extra_argc]) extra_argc++;
214
ev = event_context_init(NULL);
216
ctdb = ctdb_cmdline_client(ev);
218
ctdb_set_message_handler(ctdb, CTDB_SRVID_RECONFIGURE, reconfigure_handler,
221
/* attach to a specific database */
222
ctdb_db = ctdb_attach(ctdb, "test.tdb", false);
224
printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
228
ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
230
ctdb_set_message_handler(ctdb, 0, message_handler, &msg_count);
232
printf("Waiting for cluster\n");
235
ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
236
if (recmode == 0) break;
240
bench_fetch(ctdb, ev);
243
call.key.dptr = discard_const(TESTKEY);
244
call.key.dsize = strlen(TESTKEY);
246
printf("Fetching final record\n");
248
/* fetch the record */
249
call.call_id = FUNC_FETCH;
250
call.call_data.dptr = NULL;
251
call.call_data.dsize = 0;
253
ret = ctdb_call(ctdb_db, &call);
255
printf("ctdb_call FUNC_FETCH failed - %s\n", ctdb_errstr(ctdb));
259
printf("DATA:\n%s\n", (char *)call.reply_data.dptr);