first step towards fixing "make test" with the new daemon system
[samba.git] / ctdb / tests / ctdb_bench.c
1 /* 
2    simple ctdb benchmark
3
4    Copyright (C) Andrew Tridgell  2006
5
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 2 of the License, or
9    (at your option) any later version.
10    
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.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
24 #include "popt.h"
25 #include "cmdline.h"
26
27 #include <sys/time.h>
28 #include <time.h>
29
30 static struct timeval tp1,tp2;
31
32 static void start_timer(void)
33 {
34         gettimeofday(&tp1,NULL);
35 }
36
37 static double end_timer(void)
38 {
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));
42 }
43
44
45 static int timelimit = 10;
46 static int num_records = 10;
47 static int num_msgs = 1;
48 static uint32_t num_nodes;
49
50 enum my_functions {FUNC_INCR=1, FUNC_FETCH=2};
51
52 /*
53   ctdb call function to increment an integer
54 */
55 static int incr_func(struct ctdb_call_info *call)
56 {
57         if (call->record_data.dsize == 0) {
58                 call->new_data = talloc(call, TDB_DATA);
59                 if (call->new_data == NULL) {
60                         return CTDB_ERR_NOMEM;
61                 }
62                 call->new_data->dptr = talloc_size(call, 4);
63                 call->new_data->dsize = 4;
64                 *(uint32_t *)call->new_data->dptr = 0;
65         } else {
66                 call->new_data = &call->record_data;
67         }
68         (*(uint32_t *)call->new_data->dptr)++;
69         return 0;
70 }
71
72 /*
73   ctdb call function to fetch a record
74 */
75 static int fetch_func(struct ctdb_call_info *call)
76 {
77         call->reply_data = &call->record_data;
78         return 0;
79 }
80
81
82 static int msg_count;
83 static int msg_plus, msg_minus;
84
85 /*
86   handler for messages in bench_ring()
87 */
88 static void ring_message_handler(struct ctdb_context *ctdb, uint64_t srvid, 
89                                  TDB_DATA data, void *private_data)
90 {
91         int incr = *(int *)data.dptr;
92         int *count = (int *)private_data;
93         int dest;
94         (*count)++;
95         dest = (ctdb_get_vnn(ctdb) + incr) % num_nodes;
96         ctdb_send_message(ctdb, dest, srvid, data);
97         if (incr == 1) {
98                 msg_plus++;
99         } else {
100                 msg_minus++;
101         }
102 }
103
104 /*
105   benchmark sending messages in a ring around the nodes
106 */
107 static void bench_ring(struct ctdb_context *ctdb, struct event_context *ev)
108 {
109         int vnn=ctdb_get_vnn(ctdb);
110
111         if (vnn == 0) {
112                 int i;
113                 /* two messages are injected into the ring, moving
114                    in opposite directions */
115                 int dest, incr;
116                 TDB_DATA data;
117                 
118                 data.dptr = (uint8_t *)&incr;
119                 data.dsize = sizeof(incr);
120
121                 for (i=0;i<num_msgs;i++) {
122                         incr = 1;
123                         dest = (ctdb_get_vnn(ctdb) + incr) % num_nodes;
124                         ctdb_send_message(ctdb, dest, 0, data);
125
126                         incr = -1;
127                         dest = (ctdb_get_vnn(ctdb) + incr) % num_nodes;
128                         ctdb_send_message(ctdb, dest, 0, data);
129                 }
130         }
131         
132         start_timer();
133
134         while (end_timer() < timelimit) {
135                 if (vnn == 0 && msg_count % 10000 == 0) {
136                         printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\r", 
137                                msg_count/end_timer(), msg_plus, msg_minus);
138                         fflush(stdout);
139                 }
140                 event_loop_once(ev);
141         }
142
143         printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\n", 
144                msg_count/end_timer(), msg_plus, msg_minus);
145 }
146
147 /*
148   handler for reconfigure message
149 */
150 static void reconfigure_handler(struct ctdb_context *ctdb, uint64_t srvid, 
151                                 TDB_DATA data, void *private_data)
152 {
153         int *ready = (int *)private_data;
154         *ready = 1;
155 }
156
157
158 /*
159   main program
160 */
161 int main(int argc, const char *argv[])
162 {
163         struct ctdb_context *ctdb;
164         struct ctdb_db_context *ctdb_db;
165
166         struct poptOption popt_options[] = {
167                 POPT_AUTOHELP
168                 POPT_CTDB_CMDLINE
169                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
170                 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
171                 { "num-msgs", 'n', POPT_ARG_INT, &num_msgs, 0, "num_msgs", "integer" },
172                 POPT_TABLEEND
173         };
174         int opt;
175         const char **extra_argv;
176         int extra_argc = 0;
177         int ret;
178         poptContext pc;
179         struct event_context *ev;
180         int cluster_ready=0;
181
182         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
183
184         while ((opt = poptGetNextOpt(pc)) != -1) {
185                 switch (opt) {
186                 default:
187                         fprintf(stderr, "Invalid option %s: %s\n", 
188                                 poptBadOption(pc, 0), poptStrerror(opt));
189                         exit(1);
190                 }
191         }
192
193         /* setup the remaining options for the main program to use */
194         extra_argv = poptGetArgs(pc);
195         if (extra_argv) {
196                 extra_argv++;
197                 while (extra_argv[extra_argc]) extra_argc++;
198         }
199
200         ev = event_context_init(NULL);
201
202         /* initialise ctdb */
203         ctdb = ctdb_cmdline_client(ev);
204
205         ctdb_set_message_handler(ctdb, CTDB_SRVID_RECONFIGURE, reconfigure_handler, 
206                                  &cluster_ready);
207
208         /* attach to a specific database */
209         ctdb_db = ctdb_attach(ctdb, "test.tdb");
210         if (!ctdb_db) {
211                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
212                 exit(1);
213         }
214
215         /* setup a ctdb call function */
216         ret = ctdb_set_call(ctdb_db, incr_func,  FUNC_INCR);
217         ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
218
219         if (ctdb_set_message_handler(ctdb, 0, ring_message_handler,&msg_count))
220                 goto error;
221
222         printf("Waiting for cluster\n");
223         while (!cluster_ready) {
224                 event_loop_once(ev);
225         }
226
227         ctdb_get_connected_nodes(ctdb, timeval_zero(), ctdb, &num_nodes);
228
229         bench_ring(ctdb, ev);
230        
231 error:
232         return 0;
233 }