lib/tls: Change default supported TLS versions.
[Samba.git] / ctdb / tests / src / ctdb_bench.c
blob3323589b34ce9f210665addcf7fa66d065805fa7
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"
24 #include "ctdb_client.h"
25 #include "ctdb_private.h"
27 #include <sys/time.h>
28 #include <time.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;
47 static int num_nodes;
49 enum my_functions {FUNC_INCR=1, FUNC_FETCH=2};
52 ctdb call function to increment an integer
54 static int incr_func(struct ctdb_call_info *call)
56 if (call->record_data.dsize == 0) {
57 call->new_data = talloc(call, TDB_DATA);
58 if (call->new_data == NULL) {
59 return CTDB_ERR_NOMEM;
61 call->new_data->dptr = talloc_size(call, 4);
62 call->new_data->dsize = 4;
63 *(uint32_t *)call->new_data->dptr = 0;
64 } else {
65 call->new_data = &call->record_data;
67 (*(uint32_t *)call->new_data->dptr)++;
68 return 0;
72 ctdb call function to fetch a record
74 static int fetch_func(struct ctdb_call_info *call)
76 call->reply_data = &call->record_data;
77 return 0;
81 static int msg_count;
82 static int msg_plus, msg_minus;
85 handler for messages in bench_ring()
87 static void ring_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
88 TDB_DATA data, void *private_data)
90 int incr = *(int *)data.dptr;
91 int *count = (int *)private_data;
92 int dest;
94 (*count)++;
95 dest = (ctdb_get_pnn(ctdb) + num_nodes + incr) % num_nodes;
96 ctdb_client_send_message(ctdb, dest, srvid, data);
97 if (incr == 1) {
98 msg_plus++;
99 } else {
100 msg_minus++;
105 static void send_start_messages(struct ctdb_context *ctdb, int incr)
107 /* two messages are injected into the ring, moving
108 in opposite directions */
109 int dest;
110 TDB_DATA data;
112 data.dptr = (uint8_t *)&incr;
113 data.dsize = sizeof(incr);
115 dest = (ctdb_get_pnn(ctdb) + num_nodes + incr) % num_nodes;
116 ctdb_client_send_message(ctdb, dest, 0, data);
119 static void each_second(struct event_context *ev, struct timed_event *te,
120 struct timeval t, void *private_data)
122 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
124 /* we kickstart the ring into action by inserting messages from node
125 with pnn 0.
126 it may happen that some other node does not yet have ctdb_bench
127 running in which case the ring is broken and the messages are lost.
128 if so, once every second try again to restart the ring
130 if (msg_plus == 0) {
131 // printf("no messages recevied, try again to kickstart the ring in forward direction...\n");
132 send_start_messages(ctdb, 1);
134 if (msg_minus == 0) {
135 // printf("no messages recevied, try again to kickstart the ring in reverse direction...\n");
136 send_start_messages(ctdb, -1);
138 event_add_timed(ctdb->ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
141 static void dummy_event(struct event_context *ev, struct timed_event *te,
142 struct timeval t, void *private_data)
144 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
145 event_add_timed(ctdb->ev, ctdb, timeval_current_ofs(1, 0), dummy_event, ctdb);
149 benchmark sending messages in a ring around the nodes
151 static void bench_ring(struct ctdb_context *ctdb, struct event_context *ev)
153 int pnn=ctdb_get_pnn(ctdb);
155 if (pnn == 0) {
156 event_add_timed(ctdb->ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
157 } else {
158 event_add_timed(ctdb->ev, ctdb, timeval_current_ofs(1, 0), dummy_event, ctdb);
161 start_timer();
162 while (end_timer() < timelimit) {
163 if (pnn == 0 && msg_count % 10000 == 0 && end_timer() > 0) {
164 printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\r",
165 msg_count/end_timer(), msg_plus, msg_minus);
166 fflush(stdout);
168 event_loop_once(ev);
171 printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\n",
172 msg_count/end_timer(), msg_plus, msg_minus);
176 main program
178 int main(int argc, const char *argv[])
180 struct ctdb_context *ctdb;
181 struct ctdb_db_context *ctdb_db;
183 struct poptOption popt_options[] = {
184 POPT_AUTOHELP
185 POPT_CTDB_CMDLINE
186 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
187 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
188 { NULL, 'n', POPT_ARG_INT, &num_nodes, 0, "num_nodes", "integer" },
189 POPT_TABLEEND
191 int opt;
192 const char **extra_argv;
193 int extra_argc = 0;
194 int ret;
195 poptContext pc;
196 struct event_context *ev;
198 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
200 while ((opt = poptGetNextOpt(pc)) != -1) {
201 switch (opt) {
202 default:
203 fprintf(stderr, "Invalid option %s: %s\n",
204 poptBadOption(pc, 0), poptStrerror(opt));
205 exit(1);
209 /* setup the remaining options for the main program to use */
210 extra_argv = poptGetArgs(pc);
211 if (extra_argv) {
212 extra_argv++;
213 while (extra_argv[extra_argc]) extra_argc++;
216 if (num_nodes == 0) {
217 printf("You must specify the number of nodes\n");
218 exit(1);
221 ev = event_context_init(NULL);
223 /* initialise ctdb */
224 ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
225 if (ctdb == NULL) {
226 exit(1);
229 /* attach to a specific database */
230 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0), "test.tdb",
231 false, 0);
232 if (!ctdb_db) {
233 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
234 exit(1);
237 /* setup a ctdb call function */
238 ret = ctdb_set_call(ctdb_db, incr_func, FUNC_INCR);
239 if (ret != 0) {
240 DEBUG(DEBUG_DEBUG,("ctdb_set_call() failed, ignoring return code %d\n", ret));
242 ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
243 if (ret != 0) {
244 DEBUG(DEBUG_DEBUG,("ctdb_set_call() failed, ignoring return code %d\n", ret));
247 if (ctdb_client_set_message_handler(ctdb, 0, ring_message_handler,&msg_count))
248 goto error;
250 printf("Waiting for cluster\n");
251 while (1) {
252 uint32_t recmode=1;
253 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
254 if (recmode == 0) break;
255 event_loop_once(ev);
258 bench_ring(ctdb, ev);
260 error:
261 return 0;