c08494c7a5e34b692dc10061456fa84fff075455
[ctdb.git] / tests / src / ctdb_fetch.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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/tevent/tevent.h"
22 #include "system/filesys.h"
23 #include "popt.h"
24 #include "cmdline.h"
25
26 #include <sys/time.h>
27 #include <time.h>
28
29 static struct timeval tp1,tp2;
30
31 static void start_timer(void)
32 {
33         gettimeofday(&tp1,NULL);
34 }
35
36 static double end_timer(void)
37 {
38         gettimeofday(&tp2,NULL);
39         return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
40                 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
41 }
42
43
44 static int timelimit = 10;
45 static int num_records = 10;
46 static int num_nodes;
47 static int msg_count;
48
49 #define TESTKEY "testkey"
50
51 /*
52   fetch a record
53   store a expanded record
54   send a message to next node to tell it to do the same
55 */
56 static void bench_fetch_1node(struct ctdb_context *ctdb)
57 {
58         TDB_DATA key, data, nulldata;
59         struct ctdb_db_context *ctdb_db;
60         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
61         int dest, ret;
62         struct ctdb_record_handle *h;
63
64         key.dptr = discard_const(TESTKEY);
65         key.dsize = strlen(TESTKEY);
66
67         ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
68
69         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
70         if (h == NULL) {
71                 printf("Failed to fetch record '%s' on node %d\n", 
72                        (const char *)key.dptr, ctdb_get_pnn(ctdb));
73                 talloc_free(tmp_ctx);
74                 return;
75         }
76
77         if (data.dsize > 1000) {
78                 data.dsize = 0;
79         }
80
81         if (data.dsize == 0) {
82                 data.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "Test data\n");
83         }
84         data.dptr = (uint8_t *)talloc_asprintf_append((char *)data.dptr, 
85                                                       "msg_count=%d on node %d\n",
86                                                       msg_count, ctdb_get_pnn(ctdb));
87         data.dsize = strlen((const char *)data.dptr)+1;
88
89         ret = ctdb_record_store(h, data);
90         talloc_free(h);
91         if (ret != 0) {
92                 printf("Failed to store record\n");
93         }
94
95         talloc_free(tmp_ctx);
96
97         /* tell the next node to do the same */
98         nulldata.dptr = NULL;
99         nulldata.dsize = 0;
100
101         dest = (ctdb_get_pnn(ctdb) + 1) % num_nodes;
102         ctdb_client_send_message(ctdb, dest, 0, nulldata);
103 }
104
105 /*
106   handler for messages in bench_ring()
107 */
108 static void message_handler(struct ctdb_context *ctdb, uint64_t srvid, 
109                             TDB_DATA data, void *private_data)
110 {
111         msg_count++;
112         bench_fetch_1node(ctdb);
113 }
114
115
116 /*
117   benchmark the following:
118
119   fetch a record
120   store a expanded record
121   send a message to next node to tell it to do the same
122
123 */
124 static void bench_fetch(struct ctdb_context *ctdb, struct event_context *ev)
125 {
126         int pnn=ctdb_get_pnn(ctdb);
127
128         if (pnn == num_nodes - 1) {
129                 bench_fetch_1node(ctdb);
130         }
131         
132         start_timer();
133
134         while (end_timer() < timelimit) {
135                 if (pnn == 0 && msg_count % 100 == 0 && end_timer() > 0) {
136                         printf("Fetch: %.2f msgs/sec\r", msg_count/end_timer());
137                         fflush(stdout);
138                 }
139                 if (event_loop_once(ev) != 0) {
140                         printf("Event loop failed!\n");
141                         break;
142                 }
143         }
144
145         printf("Fetch: %.2f msgs/sec\n", msg_count/end_timer());
146 }
147
148 /*
149   handler for reconfigure message
150 */
151 static void reconfigure_handler(struct ctdb_context *ctdb, uint64_t srvid, 
152                                 TDB_DATA data, void *private_data)
153 {
154         int *ready = (int *)private_data;
155         *ready = 1;
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                 { NULL, 'n', POPT_ARG_INT, &num_nodes, 0, "num_nodes", "integer" },
172                 POPT_TABLEEND
173         };
174         int opt;
175         const char **extra_argv;
176         int extra_argc = 0;
177         poptContext pc;
178         struct event_context *ev;
179         TDB_DATA key, data;
180         struct ctdb_record_handle *h;
181         int cluster_ready=0;
182
183         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
184
185         while ((opt = poptGetNextOpt(pc)) != -1) {
186                 switch (opt) {
187                 default:
188                         fprintf(stderr, "Invalid option %s: %s\n", 
189                                 poptBadOption(pc, 0), poptStrerror(opt));
190                         exit(1);
191                 }
192         }
193
194         /* talloc_enable_leak_report_full(); */
195
196         /* setup the remaining options for the main program to use */
197         extra_argv = poptGetArgs(pc);
198         if (extra_argv) {
199                 extra_argv++;
200                 while (extra_argv[extra_argc]) extra_argc++;
201         }
202
203         if (num_nodes == 0) {
204                 printf("You must specify the number of nodes\n");
205                 exit(1);
206         }
207
208         ev = event_context_init(NULL);
209
210         ctdb = ctdb_cmdline_client(ev);
211
212         ctdb_client_set_message_handler(ctdb, CTDB_SRVID_RECONFIGURE, reconfigure_handler, 
213                                  &cluster_ready);
214
215         /* attach to a specific database */
216         ctdb_db = ctdb_attach(ctdb, "test.tdb", false, 0);
217         if (!ctdb_db) {
218                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
219                 exit(1);
220         }
221
222         ctdb_client_set_message_handler(ctdb, 0, message_handler, &msg_count);
223
224         printf("Waiting for cluster\n");
225         while (1) {
226                 uint32_t recmode=1;
227                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
228                 if (recmode == 0) break;
229                 event_loop_once(ev);
230         }
231
232         bench_fetch(ctdb, ev);
233
234         key.dptr = discard_const(TESTKEY);
235         key.dsize = strlen(TESTKEY);
236
237         printf("Fetching final record\n");
238
239         h = ctdb_fetch_lock(ctdb_db, ctdb, key, &data);
240
241         if (h == NULL) {
242                 printf("Failed to fetch record '%s' on node %d\n", 
243                        (const char *)key.dptr, ctdb_get_pnn(ctdb));
244                 exit(1);
245         }
246
247         printf("DATA:\n%s\n", (char *)data.dptr);
248
249         return 0;
250 }