39bd861fc1d589af70acc8c1ddb704524d53b7e6
[kamenim/samba.git] / source4 / cluster / ctdb / tests / ctdb_fetch.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 static int msg_count;
50
51 #define TESTKEY "testkey"
52
53 /*
54   fetch a record
55   store a expanded record
56   send a message to next node to tell it to do the same
57 */
58 static void bench_fetch_1node(struct ctdb_context *ctdb)
59 {
60         TDB_DATA key, data, nulldata;
61         struct ctdb_db_context *ctdb_db;
62         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
63         int dest, ret;
64         struct ctdb_record_handle *h;
65
66         key.dptr = discard_const(TESTKEY);
67         key.dsize = strlen(TESTKEY);
68
69         ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
70
71         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
72         if (h == NULL) {
73                 printf("Failed to fetch record '%s' on node %d\n", 
74                        (const char *)key.dptr, ctdb_get_vnn(ctdb));
75                 talloc_free(tmp_ctx);
76                 return;
77         }
78
79         if (data.dsize > 1000) {
80                 data.dsize = 0;
81         }
82
83         if (data.dsize == 0) {
84                 data.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "Test data\n");
85         }
86         data.dptr = (uint8_t *)talloc_asprintf_append((char *)data.dptr, 
87                                                       "msg_count=%d on node %d\n",
88                                                       msg_count, ctdb_get_vnn(ctdb));
89         data.dsize = strlen((const char *)data.dptr)+1;
90
91         ret = ctdb_record_store(h, data);
92         talloc_free(h);
93         if (ret != 0) {
94                 printf("Failed to store record\n");
95         }
96
97         talloc_free(tmp_ctx);
98
99         /* tell the next node to do the same */
100         nulldata.dptr = NULL;
101         nulldata.dsize = 0;
102
103         dest = (ctdb_get_vnn(ctdb) + 1) % ctdb_get_num_nodes(ctdb);
104         ctdb_send_message(ctdb, dest, 0, nulldata);
105 }
106
107 /*
108   handler for messages in bench_ring()
109 */
110 static void message_handler(struct ctdb_context *ctdb, uint32_t srvid, 
111                             TDB_DATA data, void *private_data)
112 {
113         msg_count++;
114         bench_fetch_1node(ctdb);
115 }
116
117
118 /*
119   benchmark the following:
120
121   fetch a record
122   store a expanded record
123   send a message to next node to tell it to do the same
124
125 */
126 static void bench_fetch(struct ctdb_context *ctdb, struct event_context *ev)
127 {
128         int vnn=ctdb_get_vnn(ctdb);
129
130         if (vnn == ctdb_get_num_nodes(ctdb)-1) {
131                 bench_fetch_1node(ctdb);
132         }
133         
134         start_timer();
135
136         while (end_timer() < timelimit) {
137                 if (vnn == 0 && msg_count % 100 == 0) {
138                         printf("Fetch: %.2f msgs/sec\r", msg_count/end_timer());
139                         fflush(stdout);
140                 }
141                 if (event_loop_once(ev) != 0) {
142                         printf("Event loop failed!\n");
143                         break;
144                 }
145
146                 if (LogLevel > 9) {
147                         talloc_report_null_full();
148                 }
149         }
150
151         printf("Fetch: %.2f msgs/sec\n", msg_count/end_timer());
152 }
153
154 enum my_functions {FUNC_FETCH=1};
155
156 /*
157   ctdb call function to fetch a record
158 */
159 static int fetch_func(struct ctdb_call_info *call)
160 {
161         call->reply_data = &call->record_data;
162         return 0;
163 }
164
165 /*
166   main program
167 */
168 int main(int argc, const char *argv[])
169 {
170         struct ctdb_context *ctdb;
171         struct ctdb_db_context *ctdb_db;
172
173         struct poptOption popt_options[] = {
174                 POPT_AUTOHELP
175                 POPT_CTDB_CMDLINE
176                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
177                 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
178                 { "num-msgs", 'n', POPT_ARG_INT, &num_msgs, 0, "num_msgs", "integer" },
179                 POPT_TABLEEND
180         };
181         int opt;
182         const char **extra_argv;
183         int extra_argc = 0;
184         int ret;
185         poptContext pc;
186         struct event_context *ev;
187         struct ctdb_call call;
188
189         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
190
191         while ((opt = poptGetNextOpt(pc)) != -1) {
192                 switch (opt) {
193                 default:
194                         fprintf(stderr, "Invalid option %s: %s\n", 
195                                 poptBadOption(pc, 0), poptStrerror(opt));
196                         exit(1);
197                 }
198         }
199
200         /* talloc_enable_leak_report_full(); */
201
202         /* setup the remaining options for the main program to use */
203         extra_argv = poptGetArgs(pc);
204         if (extra_argv) {
205                 extra_argv++;
206                 while (extra_argv[extra_argc]) extra_argc++;
207         }
208
209         ev = event_context_init(NULL);
210
211         ctdb = ctdb_cmdline_init(ev);
212
213         /* attach to a specific database */
214         ctdb_db = ctdb_attach(ctdb, "test.tdb", TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0666);
215         if (!ctdb_db) {
216                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
217                 exit(1);
218         }
219
220         ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
221
222         /* start the protocol running */
223         ret = ctdb_start(ctdb);
224
225         ctdb_set_message_handler(ctdb, 0, message_handler, &msg_count);
226
227         /* wait until all nodes are connected (should not be needed
228            outside of test code) */
229         ctdb_connect_wait(ctdb);
230
231         bench_fetch(ctdb, ev);
232
233         ZERO_STRUCT(call);
234         call.key.dptr = discard_const(TESTKEY);
235         call.key.dsize = strlen(TESTKEY);
236
237         /* fetch the record */
238         call.call_id = FUNC_FETCH;
239         call.call_data.dptr = NULL;
240         call.call_data.dsize = 0;
241
242         ret = ctdb_call(ctdb_db, &call);
243         if (ret == -1) {
244                 printf("ctdb_call FUNC_FETCH failed - %s\n", ctdb_errstr(ctdb));
245                 exit(1);
246         }
247
248         printf("DATA:\n%s\n", (char *)call.reply_data.dptr);
249
250         /* go into a wait loop to allow other nodes to complete */
251         ctdb_shutdown(ctdb);
252
253         return 0;
254 }