lib/tls: Change default supported TLS versions.
[Samba.git] / ctdb / tests / src / ctdb_fetch.c
blobb900efa7c3cdcd3c0a97d26eb5fc0e58f5ac79d9
1 /*
2 simple ctdb benchmark
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/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "popt.h"
23 #include "cmdline.h"
25 #include <sys/time.h>
26 #include <time.h>
28 static struct timeval tp1,tp2;
30 static void start_timer(void)
32 gettimeofday(&tp1,NULL);
35 static double end_timer(void)
37 gettimeofday(&tp2,NULL);
38 return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) -
39 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
43 static int timelimit = 10;
44 static int num_records = 10;
45 static int num_nodes;
46 static int msg_count;
48 #define TESTKEY "testkey"
51 fetch a record
52 store a expanded record
53 send a message to next node to tell it to do the same
55 static void bench_fetch_1node(struct ctdb_context *ctdb)
57 TDB_DATA key, data, nulldata;
58 struct ctdb_db_context *ctdb_db;
59 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
60 int dest, ret;
61 struct ctdb_record_handle *h;
63 key.dptr = discard_const(TESTKEY);
64 key.dsize = strlen(TESTKEY);
66 ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
68 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
69 if (h == NULL) {
70 printf("Failed to fetch record '%s' on node %d\n",
71 (const char *)key.dptr, ctdb_get_pnn(ctdb));
72 talloc_free(tmp_ctx);
73 return;
76 if (data.dsize > 1000) {
77 data.dsize = 0;
80 if (data.dsize == 0) {
81 data.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "Test data\n");
83 data.dptr = (uint8_t *)talloc_asprintf_append((char *)data.dptr,
84 "msg_count=%d on node %d\n",
85 msg_count, ctdb_get_pnn(ctdb));
86 if (data.dptr == NULL) {
87 printf("Failed to create record\n");
88 talloc_free(tmp_ctx);
89 return;
91 data.dsize = strlen((const char *)data.dptr)+1;
93 ret = ctdb_record_store(h, data);
94 talloc_free(h);
95 if (ret != 0) {
96 printf("Failed to store record\n");
99 talloc_free(tmp_ctx);
101 /* tell the next node to do the same */
102 nulldata.dptr = NULL;
103 nulldata.dsize = 0;
105 dest = (ctdb_get_pnn(ctdb) + 1) % num_nodes;
106 ctdb_client_send_message(ctdb, dest, 0, nulldata);
110 handler for messages in bench_ring()
112 static void message_handler(struct ctdb_context *ctdb, uint64_t srvid,
113 TDB_DATA data, void *private_data)
115 msg_count++;
116 bench_fetch_1node(ctdb);
121 * timeout handler - noop
123 static void timeout_handler(struct event_context *ev, struct timed_event *timer,
124 struct timeval curtime, void *private_data)
126 return;
130 benchmark the following:
132 fetch a record
133 store a expanded record
134 send a message to next node to tell it to do the same
137 static void bench_fetch(struct ctdb_context *ctdb, struct event_context *ev)
139 int pnn=ctdb_get_pnn(ctdb);
141 if (pnn == num_nodes - 1) {
142 bench_fetch_1node(ctdb);
145 start_timer();
146 event_add_timed(ev, ctdb, timeval_current_ofs(timelimit,0), timeout_handler, NULL);
148 while (end_timer() < timelimit) {
149 if (pnn == 0 && msg_count % 100 == 0 && end_timer() > 0) {
150 printf("Fetch: %.2f msgs/sec\r", msg_count/end_timer());
151 fflush(stdout);
153 if (event_loop_once(ev) != 0) {
154 printf("Event loop failed!\n");
155 break;
159 printf("Fetch: %.2f msgs/sec\n", msg_count/end_timer());
163 handler for reconfigure message
165 static void reconfigure_handler(struct ctdb_context *ctdb, uint64_t srvid,
166 TDB_DATA data, void *private_data)
168 int *ready = (int *)private_data;
169 *ready = 1;
173 main program
175 int main(int argc, const char *argv[])
177 struct ctdb_context *ctdb;
178 struct ctdb_db_context *ctdb_db;
180 struct poptOption popt_options[] = {
181 POPT_AUTOHELP
182 POPT_CTDB_CMDLINE
183 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
184 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
185 { NULL, 'n', POPT_ARG_INT, &num_nodes, 0, "num_nodes", "integer" },
186 POPT_TABLEEND
188 int opt;
189 const char **extra_argv;
190 int extra_argc = 0;
191 poptContext pc;
192 struct event_context *ev;
193 TDB_DATA key, data;
194 struct ctdb_record_handle *h;
195 int cluster_ready=0;
197 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
199 while ((opt = poptGetNextOpt(pc)) != -1) {
200 switch (opt) {
201 default:
202 fprintf(stderr, "Invalid option %s: %s\n",
203 poptBadOption(pc, 0), poptStrerror(opt));
204 exit(1);
208 /* talloc_enable_leak_report_full(); */
210 /* setup the remaining options for the main program to use */
211 extra_argv = poptGetArgs(pc);
212 if (extra_argv) {
213 extra_argv++;
214 while (extra_argv[extra_argc]) extra_argc++;
217 if (num_nodes == 0) {
218 printf("You must specify the number of nodes\n");
219 exit(1);
222 ev = event_context_init(NULL);
223 tevent_loop_allow_nesting(ev);
225 ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
227 if (ctdb == NULL) {
228 printf("failed to connect to ctdb daemon.\n");
229 exit(1);
232 ctdb_client_set_message_handler(ctdb, CTDB_SRVID_RECONFIGURE, reconfigure_handler,
233 &cluster_ready);
235 /* attach to a specific database */
236 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0), "test.tdb",
237 false, 0);
238 if (!ctdb_db) {
239 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
240 exit(1);
243 ctdb_client_set_message_handler(ctdb, 0, message_handler, &msg_count);
245 printf("Waiting for cluster\n");
246 while (1) {
247 uint32_t recmode=1;
248 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
249 if (recmode == 0) break;
250 event_loop_once(ev);
253 /* This test has a race condition. If CTDB receives the message from previous
254 * node, before this node has registered for that message, this node will never
255 * receive that message and will block on receive. Sleeping for some time will
256 * hopefully ensure that the test program on all the nodes register for messages.
258 printf("Sleeping for %d seconds\n", num_nodes);
259 sleep(num_nodes);
260 bench_fetch(ctdb, ev);
262 key.dptr = discard_const(TESTKEY);
263 key.dsize = strlen(TESTKEY);
265 printf("Fetching final record\n");
267 h = ctdb_fetch_lock(ctdb_db, ctdb, key, &data);
269 if (h == NULL) {
270 printf("Failed to fetch record '%s' on node %d\n",
271 (const char *)key.dptr, ctdb_get_pnn(ctdb));
272 exit(1);
275 printf("DATA:\n%s\n", (char *)data.dptr);
277 return 0;