02fcc1f2d458234363c095505a9dde534e1c1206
[metze/samba/wip.git] / source4 / cluster / ctdb / tests / ctdb_bench.c
1 /* 
2    simple ctdb benchmark
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2 of the License, or (at your option) any later version.
10
11    This library 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 GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
49 enum my_functions {FUNC_INCR=1, FUNC_FETCH=2};
50
51 /*
52   ctdb call function to increment an integer
53 */
54 static int incr_func(struct ctdb_call_info *call)
55 {
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;
60                 }
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;
66         }
67         (*(uint32_t *)call->new_data->dptr)++;
68         return 0;
69 }
70
71 /*
72   ctdb call function to fetch a record
73 */
74 static int fetch_func(struct ctdb_call_info *call)
75 {
76         call->reply_data = &call->record_data;
77         return 0;
78 }
79
80
81 static int msg_count;
82 static int msg_plus, msg_minus;
83
84 /*
85   handler for messages in bench_ring()
86 */
87 static void ring_message_handler(struct ctdb_context *ctdb, uint32_t srvid, 
88                                  TDB_DATA data, void *private_data)
89 {
90         int incr = *(int *)data.dptr;
91         int *count = (int *)private_data;
92         int dest;
93         (*count)++;
94         dest = (ctdb_get_vnn(ctdb) + incr) % ctdb_get_num_nodes(ctdb);
95         ctdb_send_message(ctdb, dest, srvid, data);
96         if (incr == 1) {
97                 msg_plus++;
98         } else {
99                 msg_minus++;
100         }
101 }
102
103 /*
104   benchmark sending messages in a ring around the nodes
105 */
106 static void bench_ring(struct ctdb_context *ctdb, struct event_context *ev)
107 {
108         int vnn=ctdb_get_vnn(ctdb);
109
110         if (vnn == 0) {
111                 int i;
112                 /* two messages are injected into the ring, moving
113                    in opposite directions */
114                 int dest, incr;
115                 TDB_DATA data;
116                 
117                 data.dptr = (uint8_t *)&incr;
118                 data.dsize = sizeof(incr);
119
120                 for (i=0;i<num_msgs;i++) {
121                         incr = 1;
122                         dest = (ctdb_get_vnn(ctdb) + incr) % ctdb_get_num_nodes(ctdb);
123                         ctdb_send_message(ctdb, dest, 0, data);
124
125                         incr = -1;
126                         dest = (ctdb_get_vnn(ctdb) + incr) % ctdb_get_num_nodes(ctdb);
127                         ctdb_send_message(ctdb, dest, 0, data);
128                 }
129         }
130         
131         start_timer();
132
133         while (end_timer() < timelimit) {
134                 if (vnn == 0 && msg_count % 10000 == 0) {
135                         printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\r", 
136                                msg_count/end_timer(), msg_plus, msg_minus);
137                         fflush(stdout);
138                 }
139                 event_loop_once(ev);
140         }
141
142         printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\n", 
143                msg_count/end_timer(), msg_plus, msg_minus);
144 }
145
146
147 /*
148   main program
149 */
150 int main(int argc, const char *argv[])
151 {
152         struct ctdb_context *ctdb;
153         struct ctdb_db_context *ctdb_db;
154
155         struct poptOption popt_options[] = {
156                 POPT_AUTOHELP
157                 POPT_CTDB_CMDLINE
158                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
159                 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
160                 { "num-msgs", 'n', POPT_ARG_INT, &num_msgs, 0, "num_msgs", "integer" },
161                 POPT_TABLEEND
162         };
163         int opt;
164         const char **extra_argv;
165         int extra_argc = 0;
166         int ret;
167         poptContext pc;
168         struct event_context *ev;
169
170         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
171
172         while ((opt = poptGetNextOpt(pc)) != -1) {
173                 switch (opt) {
174                 default:
175                         fprintf(stderr, "Invalid option %s: %s\n", 
176                                 poptBadOption(pc, 0), poptStrerror(opt));
177                         exit(1);
178                 }
179         }
180
181         /* setup the remaining options for the main program to use */
182         extra_argv = poptGetArgs(pc);
183         if (extra_argv) {
184                 extra_argv++;
185                 while (extra_argv[extra_argc]) extra_argc++;
186         }
187
188         ev = event_context_init(NULL);
189
190         /* initialise ctdb */
191         ctdb = ctdb_cmdline_init(ev);
192
193         /* attach to a specific database */
194         ctdb_db = ctdb_attach(ctdb, "test.tdb", TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0666);
195         if (!ctdb_db) {
196                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
197                 exit(1);
198         }
199
200         /* setup a ctdb call function */
201         ret = ctdb_set_call(ctdb_db, incr_func,  FUNC_INCR);
202         ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
203
204         /* start the protocol running */
205         ret = ctdb_start(ctdb);
206
207         ctdb_set_message_handler(ctdb, 0, ring_message_handler,&msg_count);
208
209         /* wait until all nodes are connected (should not be needed
210            outside of test code) */
211         ctdb_connect_wait(ctdb);
212
213         bench_ring(ctdb, ev);
214        
215         /* shut it down */
216         ctdb_shutdown(ctdb);
217
218         return 0;
219 }