Extended tests so that memcached starts up its own tests
[awesomized/libmemcached] / src / memslap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <sys/mman.h>
8 #include <fcntl.h>
9 #include <assert.h>
10 #include <sys/time.h>
11 #include <getopt.h>
12 #include <pthread.h>
13
14 #include <memcached.h>
15
16 #include "client_options.h"
17 #include "utilities.h"
18 #include "generator.h"
19 #include "execute.h"
20
21 #define DEFAULT_INITIAL_LOAD 10000
22 #define DEFAULT_EXECUTE_NUMBER 10000
23 #define DEFAULT_CONCURRENCY 1
24
25 #define PROGRAM_NAME "memslap"
26 #define PROGRAM_DESCRIPTION "Generates a load against a memcached custer of servers."
27
28 /* Global Thread counter */
29 unsigned int thread_counter;
30 pthread_mutex_t counter_mutex;
31 pthread_cond_t count_threshhold;
32 unsigned int master_wakeup;
33 pthread_mutex_t sleeper_mutex;
34 pthread_cond_t sleep_threshhold;
35
36 void *run_task(void *p);
37
38 /* Types */
39 typedef struct conclusions_st conclusions_st;
40 typedef struct thread_context_st thread_context_st;
41 typedef enum {
42 SET_TEST,
43 GET_TEST,
44 } test_type;
45
46 struct thread_context_st {
47 unsigned int key_count;
48 pairs_st *initial_pairs;
49 unsigned int initial_number;
50 pairs_st *execute_pairs;
51 unsigned int execute_number;
52 test_type test;
53 memcached_server_st *servers;
54 };
55
56 struct conclusions_st {
57 long int load_time;
58 long int read_time;
59 unsigned int rows_loaded;
60 unsigned int rows_read;
61 };
62
63 /* Prototypes */
64 void options_parse(int argc, char *argv[]);
65 void conclusions_print(conclusions_st *conclusion);
66 void scheduler(memcached_server_st *servers, conclusions_st *conclusion);
67 pairs_st *load_create_data(memcached_server_st *servers, unsigned int number_of,
68 unsigned int *actual_loaded);
69 void flush_all(memcached_server_st *servers);
70
71 static int opt_verbose= 0;
72 static int opt_flush= 0;
73 static int opt_non_blocking_io= 0;
74 static int opt_tcp_nodelay= 0;
75 static unsigned int opt_execute_number= 0;
76 static unsigned int opt_createial_load= 0;
77 static unsigned int opt_concurrency= 0;
78 static int opt_displayflag= 0;
79 static char *opt_servers= NULL;
80 test_type opt_test= SET_TEST;
81
82 int main(int argc, char *argv[])
83 {
84 conclusions_st conclusion;
85 memcached_server_st *servers;
86
87 memset(&conclusion, 0, sizeof(conclusions_st));
88
89 srandom(time(NULL));
90 options_parse(argc, argv);
91
92 if (!opt_servers)
93 {
94 char *temp;
95
96 if ((temp= getenv("MEMCACHED_SERVERS")))
97 opt_servers= strdup(temp);
98 else
99 exit(1);
100 }
101
102 servers= memcached_servers_parse(opt_servers);
103
104 pthread_mutex_init(&counter_mutex, NULL);
105 pthread_cond_init(&count_threshhold, NULL);
106 pthread_mutex_init(&sleeper_mutex, NULL);
107 pthread_cond_init(&sleep_threshhold, NULL);
108
109 scheduler(servers, &conclusion);
110
111 free(opt_servers);
112
113 (void)pthread_mutex_destroy(&counter_mutex);
114 (void)pthread_cond_destroy(&count_threshhold);
115 (void)pthread_mutex_destroy(&sleeper_mutex);
116 (void)pthread_cond_destroy(&sleep_threshhold);
117 conclusions_print(&conclusion);
118 memcached_server_list_free(servers);
119
120 return 0;
121 }
122
123 void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
124 {
125 unsigned int x;
126 unsigned int actual_loaded;
127
128 struct timeval start_time, end_time;
129 pthread_t mainthread; /* Thread descriptor */
130 pthread_attr_t attr; /* Thread attributes */
131 pairs_st *pairs= NULL;
132
133 pthread_attr_init(&attr);
134 pthread_attr_setdetachstate(&attr,
135 PTHREAD_CREATE_DETACHED);
136
137 if (opt_flush)
138 flush_all(servers);
139 if (opt_createial_load)
140 pairs= load_create_data(servers, opt_createial_load, &actual_loaded);
141
142 pthread_mutex_lock(&counter_mutex);
143 thread_counter= 0;
144
145 pthread_mutex_lock(&sleeper_mutex);
146 master_wakeup= 1;
147 pthread_mutex_unlock(&sleeper_mutex);
148
149 for (x= 0; x < opt_concurrency; x++)
150 {
151 thread_context_st *context;
152 context= (thread_context_st *)malloc(sizeof(thread_context_st));
153
154 context->servers= servers;
155 context->test= opt_test;
156
157 context->initial_pairs= pairs;
158 context->initial_number= actual_loaded;
159
160 if (opt_test == SET_TEST)
161 {
162 context->execute_pairs= pairs_generate(opt_execute_number);
163 context->execute_number= opt_execute_number;
164 }
165
166 /* now you create the thread */
167 if (pthread_create(&mainthread, &attr, run_task,
168 (void *)context) != 0)
169 {
170 fprintf(stderr,"Could not create thread\n");
171 exit(1);
172 }
173 thread_counter++;
174 }
175
176 pthread_mutex_unlock(&counter_mutex);
177 pthread_attr_destroy(&attr);
178
179 pthread_mutex_lock(&sleeper_mutex);
180 master_wakeup= 0;
181 pthread_mutex_unlock(&sleeper_mutex);
182 pthread_cond_broadcast(&sleep_threshhold);
183
184 gettimeofday(&start_time, NULL);
185 /*
186 We loop until we know that all children have cleaned up.
187 */
188 pthread_mutex_lock(&counter_mutex);
189 while (thread_counter)
190 {
191 struct timespec abstime;
192
193 memset(&abstime, 0, sizeof(struct timespec));
194 abstime.tv_sec= 1;
195
196 pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
197 }
198 pthread_mutex_unlock(&counter_mutex);
199
200 gettimeofday(&end_time, NULL);
201
202 conclusion->load_time= timedif(end_time, start_time);
203 conclusion->read_time= timedif(end_time, start_time);
204 pairs_free(pairs);
205 }
206
207 void options_parse(int argc, char *argv[])
208 {
209 memcached_programs_help_st help_options[]=
210 {
211 {0},
212 };
213
214 static struct option long_options[]=
215 {
216 {"concurrency", required_argument, NULL, OPT_SLAP_CONCURRENCY},
217 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
218 {"execute-number", required_argument, NULL, OPT_SLAP_EXECUTE_NUMBER},
219 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
220 {"flush", no_argument, &opt_flush, OPT_FLUSH},
221 {"help", no_argument, NULL, OPT_HELP},
222 {"initial-load", required_argument, NULL, OPT_SLAP_INITIAL_LOAD}, /* Number to load initially */
223 {"non-blocking", no_argument, &opt_non_blocking_io, OPT_SLAP_NON_BLOCK},
224 {"servers", required_argument, NULL, OPT_SERVERS},
225 {"tcp-nodelay", no_argument, &opt_tcp_nodelay, OPT_SLAP_TCP_NODELAY},
226 {"test", required_argument, NULL, OPT_SLAP_TEST},
227 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
228 {"version", no_argument, NULL, OPT_VERSION},
229 {0, 0, 0, 0},
230 };
231
232 int option_index= 0;
233 int option_rv;
234
235 while (1)
236 {
237 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
238 if (option_rv == -1) break;
239 switch (option_rv)
240 {
241 case 0:
242 break;
243 case OPT_VERBOSE: /* --verbose or -v */
244 opt_verbose = OPT_VERBOSE;
245 break;
246 case OPT_DEBUG: /* --debug or -d */
247 opt_verbose = OPT_DEBUG;
248 break;
249 case OPT_VERSION: /* --version or -V */
250 version_command(PROGRAM_NAME);
251 break;
252 case OPT_HELP: /* --help or -h */
253 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
254 break;
255 case OPT_SERVERS: /* --servers or -s */
256 opt_servers= strdup(optarg);
257 break;
258 case OPT_SLAP_TEST:
259 if (!strcmp(optarg, "get"))
260 opt_test= GET_TEST ;
261 else if (!strcmp(optarg, "set"))
262 opt_test= SET_TEST;
263 else
264 {
265 fprintf(stderr, "Your test, %s, is not a known test\n", optarg);
266 exit(1);
267 }
268 break;
269 case OPT_SLAP_CONCURRENCY:
270 opt_concurrency= strtol(optarg, (char **)NULL, 10);
271 case OPT_SLAP_EXECUTE_NUMBER:
272 opt_execute_number= strtol(optarg, (char **)NULL, 10);
273 break;
274 case OPT_SLAP_INITIAL_LOAD:
275 opt_createial_load= strtol(optarg, (char **)NULL, 10);
276 break;
277 case '?':
278 /* getopt_long already printed an error message. */
279 exit(1);
280 default:
281 abort();
282 }
283 }
284
285 if (opt_test == GET_TEST && opt_createial_load == 0)
286 opt_createial_load= DEFAULT_INITIAL_LOAD;
287
288 if (opt_execute_number == 0)
289 opt_execute_number= DEFAULT_EXECUTE_NUMBER;
290
291 if (opt_concurrency == 0)
292 opt_concurrency= DEFAULT_CONCURRENCY;
293 }
294
295 void conclusions_print(conclusions_st *conclusion)
296 {
297 printf("\tThreads connecting to servers %u\n", opt_concurrency);
298 #ifdef NOT_FINISHED
299 printf("\tLoaded %u rows\n", conclusion->rows_loaded);
300 printf("\tRead %u rows\n", conclusion->rows_read);
301 #endif
302 if (opt_test == SET_TEST)
303 printf("\tTook %ld.%03ld seconds to load data\n", conclusion->load_time / 1000,
304 conclusion->load_time % 1000);
305 else
306 printf("\tTook %ld.%03ld seconds to read data\n", conclusion->read_time / 1000,
307 conclusion->read_time % 1000);
308 }
309
310 void *run_task(void *p)
311 {
312 thread_context_st *context= (thread_context_st *)p;
313 memcached_st *memc;
314 unsigned int value= 1;
315
316 memc= memcached_create(NULL);
317 WATCHPOINT_ASSERT(memc);
318 if (opt_non_blocking_io)
319 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &value);
320 if (opt_tcp_nodelay)
321 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &value);
322
323 memcached_server_push(memc, context->servers);
324
325 pthread_mutex_lock(&sleeper_mutex);
326 while (master_wakeup)
327 {
328 pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
329 }
330 pthread_mutex_unlock(&sleeper_mutex);
331
332 /* Do Stuff */
333 switch (context->test)
334 {
335 case SET_TEST:
336 execute_set(memc, context->execute_pairs, context->execute_number);
337 break;
338 case GET_TEST:
339 execute_get(memc, context->initial_pairs, context->initial_number);
340 break;
341 }
342
343 memcached_free(memc);
344
345 if (context->execute_pairs)
346 pairs_free(context->execute_pairs);
347 free(context);
348
349 pthread_mutex_lock(&counter_mutex);
350 thread_counter--;
351 pthread_cond_signal(&count_threshhold);
352 pthread_mutex_unlock(&counter_mutex);
353
354 return NULL;
355 }
356
357 void flush_all(memcached_server_st *servers)
358 {
359 memcached_st *memc;
360
361 memc= memcached_create(NULL);
362
363 memcached_server_push(memc, servers);
364
365 memcached_flush(memc, 0);
366
367 memcached_free(memc);
368 }
369
370 pairs_st *load_create_data(memcached_server_st *servers, unsigned int number_of,
371 unsigned int *actual_loaded)
372 {
373 memcached_st *memc;
374 pairs_st *pairs;
375
376 memc= memcached_create(NULL);
377 /* We always used non-blocking IO for load since it is faster */
378 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL );
379 memcached_server_push(memc, servers);
380
381 pairs= pairs_generate(number_of);
382 *actual_loaded= execute_set(memc, pairs, number_of);
383
384 memcached_free(memc);
385
386 return pairs;
387 }