]>
Commit | Line | Data |
---|---|---|
ed9b544e | 1 | /* Redis benchmark utility. |
2 | * | |
12d090d2 | 3 | * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com> |
ed9b544e | 4 | * All rights reserved. |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * * Redistributions of source code must retain the above copyright notice, | |
10 | * this list of conditions and the following disclaimer. | |
11 | * * Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * * Neither the name of Redis nor the names of its contributors may be used | |
15 | * to endorse or promote products derived from this software without | |
16 | * specific prior written permission. | |
17 | * | |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
28 | * POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | ||
5f5b9840 | 31 | #include "fmacros.h" |
32 | ||
ed9b544e | 33 | #include <stdio.h> |
34 | #include <string.h> | |
35 | #include <stdlib.h> | |
36 | #include <unistd.h> | |
37 | #include <errno.h> | |
38 | #include <sys/time.h> | |
39 | #include <signal.h> | |
40 | #include <assert.h> | |
41 | ||
42 | #include "ae.h" | |
ec8f0667 | 43 | #include "hiredis.h" |
ed9b544e | 44 | #include "sds.h" |
45 | #include "adlist.h" | |
46 | #include "zmalloc.h" | |
47 | ||
ed9b544e | 48 | #define REDIS_NOTUSED(V) ((void) V) |
49 | ||
50 | static struct config { | |
fc05e8c8 PN |
51 | aeEventLoop *el; |
52 | const char *hostip; | |
53 | int hostport; | |
54 | const char *hostsocket; | |
ed9b544e | 55 | int numclients; |
ed9b544e | 56 | int liveclients; |
bdbf3acf PN |
57 | int requests; |
58 | int requests_issued; | |
59 | int requests_finished; | |
ed9b544e | 60 | int keysize; |
61 | int datasize; | |
57172ffb | 62 | int randomkeys; |
ecfaf6da | 63 | int randomkeys_keyspacelen; |
ed9b544e | 64 | int keepalive; |
65 | long long start; | |
66 | long long totlatency; | |
8146e316 | 67 | long long *latency; |
fc05e8c8 | 68 | const char *title; |
ed9b544e | 69 | list *clients; |
70 | int quiet; | |
7b86f5e6 | 71 | int csv; |
ed9b544e | 72 | int loop; |
266373b2 | 73 | int idlemode; |
d9747b49 | 74 | char *tests; |
ed9b544e | 75 | } config; |
76 | ||
77 | typedef struct _client { | |
ec8f0667 | 78 | redisContext *context; |
ed9b544e | 79 | sds obuf; |
3c49070b PN |
80 | char *randptr[10]; /* needed for MSET against 10 keys */ |
81 | size_t randlen; | |
8146e316 | 82 | unsigned int written; /* bytes of 'obuf' already written */ |
8146e316 PN |
83 | long long start; /* start time of a request */ |
84 | long long latency; /* request latency */ | |
ed9b544e | 85 | } *client; |
86 | ||
87 | /* Prototypes */ | |
88 | static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask); | |
89 | static void createMissingClients(client c); | |
90 | ||
91 | /* Implementation */ | |
8146e316 PN |
92 | static long long ustime(void) { |
93 | struct timeval tv; | |
94 | long long ust; | |
95 | ||
96 | gettimeofday(&tv, NULL); | |
97 | ust = ((long)tv.tv_sec)*1000000; | |
98 | ust += tv.tv_usec; | |
99 | return ust; | |
100 | } | |
101 | ||
ed9b544e | 102 | static long long mstime(void) { |
103 | struct timeval tv; | |
104 | long long mst; | |
105 | ||
106 | gettimeofday(&tv, NULL); | |
107 | mst = ((long)tv.tv_sec)*1000; | |
108 | mst += tv.tv_usec/1000; | |
109 | return mst; | |
110 | } | |
111 | ||
112 | static void freeClient(client c) { | |
113 | listNode *ln; | |
ec8f0667 PN |
114 | aeDeleteFileEvent(config.el,c->context->fd,AE_WRITABLE); |
115 | aeDeleteFileEvent(config.el,c->context->fd,AE_READABLE); | |
116 | redisFree(c->context); | |
ed9b544e | 117 | sdsfree(c->obuf); |
ed9b544e | 118 | zfree(c); |
119 | config.liveclients--; | |
120 | ln = listSearchKey(config.clients,c); | |
121 | assert(ln != NULL); | |
122 | listDelNode(config.clients,ln); | |
123 | } | |
124 | ||
125 | static void freeAllClients(void) { | |
126 | listNode *ln = config.clients->head, *next; | |
127 | ||
128 | while(ln) { | |
129 | next = ln->next; | |
130 | freeClient(ln->value); | |
131 | ln = next; | |
132 | } | |
133 | } | |
134 | ||
135 | static void resetClient(client c) { | |
ec8f0667 PN |
136 | aeDeleteFileEvent(config.el,c->context->fd,AE_WRITABLE); |
137 | aeDeleteFileEvent(config.el,c->context->fd,AE_READABLE); | |
138 | aeCreateFileEvent(config.el,c->context->fd,AE_WRITABLE,writeHandler,c); | |
ed9b544e | 139 | c->written = 0; |
ed9b544e | 140 | } |
141 | ||
ecfaf6da | 142 | static void randomizeClientKey(client c) { |
ecfaf6da | 143 | char buf[32]; |
3c49070b | 144 | size_t i, r; |
ecfaf6da | 145 | |
3c49070b PN |
146 | for (i = 0; i < c->randlen; i++) { |
147 | r = random() % config.randomkeys_keyspacelen; | |
9b45592c | 148 | snprintf(buf,sizeof(buf),"%012zu",r); |
3c49070b | 149 | memcpy(c->randptr[i],buf,12); |
1cd3c1e0 | 150 | } |
ecfaf6da | 151 | } |
152 | ||
ed9b544e | 153 | static void clientDone(client c) { |
bdbf3acf | 154 | if (config.requests_finished == config.requests) { |
ed9b544e | 155 | freeClient(c); |
156 | aeStop(config.el); | |
157 | return; | |
158 | } | |
159 | if (config.keepalive) { | |
160 | resetClient(c); | |
161 | } else { | |
162 | config.liveclients--; | |
163 | createMissingClients(c); | |
164 | config.liveclients++; | |
165 | freeClient(c); | |
166 | } | |
167 | } | |
168 | ||
ec8f0667 | 169 | static void readHandler(aeEventLoop *el, int fd, void *privdata, int mask) { |
ed9b544e | 170 | client c = privdata; |
ec8f0667 | 171 | void *reply = NULL; |
ed9b544e | 172 | REDIS_NOTUSED(el); |
173 | REDIS_NOTUSED(fd); | |
174 | REDIS_NOTUSED(mask); | |
175 | ||
8146e316 PN |
176 | /* Calculate latency only for the first read event. This means that the |
177 | * server already sent the reply and we need to parse it. Parsing overhead | |
178 | * is not part of the latency, so calculate it only once, here. */ | |
179 | if (c->latency < 0) c->latency = ustime()-(c->start); | |
180 | ||
ec8f0667 PN |
181 | if (redisBufferRead(c->context) != REDIS_OK) { |
182 | fprintf(stderr,"Error: %s\n",c->context->errstr); | |
183 | exit(1); | |
184 | } else { | |
185 | if (redisGetReply(c->context,&reply) != REDIS_OK) { | |
186 | fprintf(stderr,"Error: %s\n",c->context->errstr); | |
187 | exit(1); | |
2fd30952 | 188 | } |
8146e316 | 189 | if (reply != NULL) { |
53f1d817 PN |
190 | if (reply == (void*)REDIS_REPLY_ERROR) { |
191 | fprintf(stderr,"Unexpected error reply, exiting...\n"); | |
192 | exit(1); | |
193 | } | |
194 | ||
bdbf3acf PN |
195 | if (config.requests_finished < config.requests) |
196 | config.latency[config.requests_finished++] = c->latency; | |
ec8f0667 | 197 | clientDone(c); |
8146e316 | 198 | } |
2fd30952 | 199 | } |
ed9b544e | 200 | } |
201 | ||
ec8f0667 | 202 | static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask) { |
ed9b544e | 203 | client c = privdata; |
204 | REDIS_NOTUSED(el); | |
205 | REDIS_NOTUSED(fd); | |
206 | REDIS_NOTUSED(mask); | |
207 | ||
bdbf3acf | 208 | /* Initialize request when nothing was written. */ |
23803889 | 209 | if (c->written == 0) { |
bdbf3acf PN |
210 | /* Enforce upper bound to number of requests. */ |
211 | if (config.requests_issued++ >= config.requests) { | |
212 | freeClient(c); | |
213 | return; | |
214 | } | |
215 | ||
216 | /* Really initialize: randomize keys and set start time. */ | |
23803889 | 217 | if (config.randomkeys) randomizeClientKey(c); |
8146e316 PN |
218 | c->start = ustime(); |
219 | c->latency = -1; | |
ed9b544e | 220 | } |
23803889 | 221 | |
ed9b544e | 222 | if (sdslen(c->obuf) > c->written) { |
223 | void *ptr = c->obuf+c->written; | |
ec8f0667 | 224 | int nwritten = write(c->context->fd,ptr,sdslen(c->obuf)-c->written); |
ed9b544e | 225 | if (nwritten == -1) { |
61c47ecd | 226 | if (errno != EPIPE) |
227 | fprintf(stderr, "Writing to socket: %s\n", strerror(errno)); | |
ed9b544e | 228 | freeClient(c); |
229 | return; | |
230 | } | |
231 | c->written += nwritten; | |
232 | if (sdslen(c->obuf) == c->written) { | |
ec8f0667 PN |
233 | aeDeleteFileEvent(config.el,c->context->fd,AE_WRITABLE); |
234 | aeCreateFileEvent(config.el,c->context->fd,AE_READABLE,readHandler,c); | |
ed9b544e | 235 | } |
236 | } | |
237 | } | |
238 | ||
fc05e8c8 | 239 | static client createClient(const char *cmd, size_t len) { |
ed9b544e | 240 | client c = zmalloc(sizeof(struct _client)); |
ec8f0667 PN |
241 | if (config.hostsocket == NULL) { |
242 | c->context = redisConnectNonBlock(config.hostip,config.hostport); | |
243 | } else { | |
244 | c->context = redisConnectUnixNonBlock(config.hostsocket); | |
ed9b544e | 245 | } |
ec8f0667 PN |
246 | if (c->context->err) { |
247 | fprintf(stderr,"Could not connect to Redis at "); | |
248 | if (config.hostsocket == NULL) | |
249 | fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,c->context->errstr); | |
250 | else | |
251 | fprintf(stderr,"%s: %s\n",config.hostsocket,c->context->errstr); | |
252 | exit(1); | |
253 | } | |
3c49070b PN |
254 | c->obuf = sdsnewlen(cmd,len); |
255 | c->randlen = 0; | |
ed9b544e | 256 | c->written = 0; |
3c49070b PN |
257 | |
258 | /* Find substrings in the output buffer that need to be randomized. */ | |
259 | if (config.randomkeys) { | |
260 | char *p = c->obuf, *newline; | |
261 | while ((p = strstr(p,":rand:")) != NULL) { | |
262 | newline = strstr(p,"\r\n"); | |
263 | assert(newline-(p+6) == 12); /* 12 chars for randomness */ | |
264 | assert(c->randlen < (signed)(sizeof(c->randptr)/sizeof(char*))); | |
265 | c->randptr[c->randlen++] = p+6; | |
266 | p = newline+2; | |
267 | } | |
268 | } | |
269 | ||
ec8f0667 PN |
270 | redisSetReplyObjectFunctions(c->context,NULL); |
271 | aeCreateFileEvent(config.el,c->context->fd,AE_WRITABLE,writeHandler,c); | |
ed9b544e | 272 | listAddNodeTail(config.clients,c); |
ec8f0667 | 273 | config.liveclients++; |
ed9b544e | 274 | return c; |
275 | } | |
276 | ||
277 | static void createMissingClients(client c) { | |
f474a5bd DS |
278 | int n = 0; |
279 | ||
ed9b544e | 280 | while(config.liveclients < config.numclients) { |
23803889 | 281 | createClient(c->obuf,sdslen(c->obuf)); |
f474a5bd DS |
282 | |
283 | /* Listen backlog is quite limited on most systems */ | |
284 | if (++n > 64) { | |
285 | usleep(50000); | |
286 | n = 0; | |
287 | } | |
ed9b544e | 288 | } |
289 | } | |
290 | ||
8146e316 PN |
291 | static int compareLatency(const void *a, const void *b) { |
292 | return (*(long long*)a)-(*(long long*)b); | |
293 | } | |
294 | ||
ed0dd554 | 295 | static void showLatencyReport(void) { |
8146e316 | 296 | int i, curlat = 0; |
ed9b544e | 297 | float perc, reqpersec; |
298 | ||
bdbf3acf | 299 | reqpersec = (float)config.requests_finished/((float)config.totlatency/1000); |
7b86f5e6 | 300 | if (!config.quiet && !config.csv) { |
ed0dd554 | 301 | printf("====== %s ======\n", config.title); |
bdbf3acf | 302 | printf(" %d requests completed in %.2f seconds\n", config.requests_finished, |
ed9b544e | 303 | (float)config.totlatency/1000); |
304 | printf(" %d parallel clients\n", config.numclients); | |
305 | printf(" %d bytes payload\n", config.datasize); | |
306 | printf(" keep alive: %d\n", config.keepalive); | |
307 | printf("\n"); | |
8146e316 PN |
308 | |
309 | qsort(config.latency,config.requests,sizeof(long long),compareLatency); | |
310 | for (i = 0; i < config.requests; i++) { | |
311 | if (config.latency[i]/1000 != curlat || i == (config.requests-1)) { | |
312 | curlat = config.latency[i]/1000; | |
313 | perc = ((float)(i+1)*100)/config.requests; | |
314 | printf("%.2f%% <= %d milliseconds\n", perc, curlat); | |
ed9b544e | 315 | } |
316 | } | |
317 | printf("%.2f requests per second\n\n", reqpersec); | |
7b86f5e6 | 318 | } else if (config.csv) { |
319 | printf("\"%s\",\"%.2f\"\n", config.title, reqpersec); | |
ed9b544e | 320 | } else { |
ed0dd554 | 321 | printf("%s: %.2f requests per second\n", config.title, reqpersec); |
ed9b544e | 322 | } |
323 | } | |
324 | ||
fc05e8c8 | 325 | static void benchmark(const char *title, const char *cmd, int len) { |
f2f2424e PN |
326 | client c; |
327 | ||
ed0dd554 | 328 | config.title = title; |
bdbf3acf PN |
329 | config.requests_issued = 0; |
330 | config.requests_finished = 0; | |
ed9b544e | 331 | |
3c49070b | 332 | c = createClient(cmd,len); |
f2f2424e PN |
333 | createMissingClients(c); |
334 | ||
335 | config.start = mstime(); | |
336 | aeMain(config.el); | |
ed9b544e | 337 | config.totlatency = mstime()-config.start; |
f2f2424e | 338 | |
ed0dd554 | 339 | showLatencyReport(); |
ed9b544e | 340 | freeAllClients(); |
341 | } | |
342 | ||
39bf4402 PN |
343 | /* Returns number of consumed options. */ |
344 | int parseOptions(int argc, const char **argv) { | |
ed9b544e | 345 | int i; |
39bf4402 PN |
346 | int lastarg; |
347 | int exit_status = 1; | |
ed9b544e | 348 | |
349 | for (i = 1; i < argc; i++) { | |
39bf4402 PN |
350 | lastarg = (i == (argc-1)); |
351 | ||
352 | if (!strcmp(argv[i],"-c")) { | |
353 | if (lastarg) goto invalid; | |
354 | config.numclients = atoi(argv[++i]); | |
355 | } else if (!strcmp(argv[i],"-n")) { | |
356 | if (lastarg) goto invalid; | |
357 | config.requests = atoi(argv[++i]); | |
358 | } else if (!strcmp(argv[i],"-k")) { | |
359 | if (lastarg) goto invalid; | |
360 | config.keepalive = atoi(argv[++i]); | |
361 | } else if (!strcmp(argv[i],"-h")) { | |
362 | if (lastarg) goto invalid; | |
363 | config.hostip = strdup(argv[++i]); | |
364 | } else if (!strcmp(argv[i],"-p")) { | |
365 | if (lastarg) goto invalid; | |
366 | config.hostport = atoi(argv[++i]); | |
367 | } else if (!strcmp(argv[i],"-s")) { | |
368 | if (lastarg) goto invalid; | |
369 | config.hostsocket = strdup(argv[++i]); | |
370 | } else if (!strcmp(argv[i],"-d")) { | |
371 | if (lastarg) goto invalid; | |
372 | config.datasize = atoi(argv[++i]); | |
ed9b544e | 373 | if (config.datasize < 1) config.datasize=1; |
826b5beb | 374 | if (config.datasize > 1024*1024*1024) config.datasize = 1024*1024*1024; |
39bf4402 PN |
375 | } else if (!strcmp(argv[i],"-r")) { |
376 | if (lastarg) goto invalid; | |
57172ffb | 377 | config.randomkeys = 1; |
39bf4402 | 378 | config.randomkeys_keyspacelen = atoi(argv[++i]); |
ecfaf6da | 379 | if (config.randomkeys_keyspacelen < 0) |
380 | config.randomkeys_keyspacelen = 0; | |
ed9b544e | 381 | } else if (!strcmp(argv[i],"-q")) { |
382 | config.quiet = 1; | |
7b86f5e6 | 383 | } else if (!strcmp(argv[i],"--csv")) { |
384 | config.csv = 1; | |
ed9b544e | 385 | } else if (!strcmp(argv[i],"-l")) { |
386 | config.loop = 1; | |
266373b2 | 387 | } else if (!strcmp(argv[i],"-I")) { |
388 | config.idlemode = 1; | |
d9747b49 | 389 | } else if (!strcmp(argv[i],"-t")) { |
390 | if (lastarg) goto invalid; | |
391 | /* We get the list of tests to run as a string in the form | |
392 | * get,set,lrange,...,test_N. Then we add a comma before and | |
393 | * after the string in order to make sure that searching | |
394 | * for ",testname," will always get a match if the test is | |
395 | * enabled. */ | |
396 | config.tests = sdsnew(","); | |
397 | config.tests = sdscat(config.tests,(char*)argv[++i]); | |
398 | config.tests = sdscat(config.tests,","); | |
399 | sdstolower(config.tests); | |
39bf4402 PN |
400 | } else if (!strcmp(argv[i],"--help")) { |
401 | exit_status = 0; | |
402 | goto usage; | |
ed9b544e | 403 | } else { |
39bf4402 PN |
404 | /* Assume the user meant to provide an option when the arg starts |
405 | * with a dash. We're done otherwise and should use the remainder | |
406 | * as the command and arguments for running the benchmark. */ | |
407 | if (argv[i][0] == '-') goto invalid; | |
408 | return i; | |
ed9b544e | 409 | } |
410 | } | |
39bf4402 PN |
411 | |
412 | return i; | |
413 | ||
414 | invalid: | |
415 | printf("Invalid option \"%s\" or option argument missing\n\n",argv[i]); | |
416 | ||
417 | usage: | |
d9747b49 | 418 | printf( |
419 | "Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]\n\n" | |
420 | " -h <hostname> Server hostname (default 127.0.0.1)\n" | |
421 | " -p <port> Server port (default 6379)\n" | |
422 | " -s <socket> Server socket (overrides host and port)\n" | |
423 | " -c <clients> Number of parallel connections (default 50)\n" | |
424 | " -n <requests> Total number of requests (default 10000)\n" | |
425 | " -d <size> Data size of SET/GET value in bytes (default 2)\n" | |
426 | " -k <boolean> 1=keep alive 0=reconnect (default 1)\n" | |
427 | " -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD\n" | |
428 | " Using this option the benchmark will get/set keys\n" | |
8dd19d81 | 429 | " in the form mykey_rand:000000012456 instead of constant\n" |
d9747b49 | 430 | " keys, the <keyspacelen> argument determines the max\n" |
431 | " number of values for the random number. For instance\n" | |
8dd19d81 | 432 | " if set to 10 only rand:000000000000 - rand:000000000009\n" |
d9747b49 | 433 | " range will be allowed.\n" |
434 | " -q Quiet. Just show query/sec values\n" | |
435 | " --csv Output in CSV format\n" | |
436 | " -l Loop. Run the tests forever\n" | |
437 | " -t <tests> Only run the comma separated list of tests. The test\n" | |
438 | " names are the same as the ones produced as output.\n" | |
439 | " -I Idle mode. Just open N idle connections and wait.\n\n" | |
440 | "Examples:\n\n" | |
441 | " Run the benchmark with the default configuration against 127.0.0.1:6379:\n" | |
442 | " $ redis-benchmark\n\n" | |
443 | " Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1:\n" | |
444 | " $ redis-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20\n\n" | |
445 | " Fill 127.0.0.1:6379 with about 1 million keys only using the SET test:\n" | |
446 | " $ redis-benchmark -t set -n 1000000 -r 100000000\n\n" | |
447 | " Benchmark 127.0.0.1:6379 for a few commands producing CSV output:\n" | |
448 | " $ redis-benchmark -t ping,set,get -n 100000 --csv\n\n" | |
8dd19d81 | 449 | " Fill a list with 10000 random elements:\n" |
450 | " $ redis-benchmark -r 10000 -n 10000 lpush mylist ele:rand:000000000000\n\n" | |
d9747b49 | 451 | ); |
39bf4402 | 452 | exit(exit_status); |
ed9b544e | 453 | } |
454 | ||
ed0dd554 PN |
455 | int showThroughput(struct aeEventLoop *eventLoop, long long id, void *clientData) { |
456 | REDIS_NOTUSED(eventLoop); | |
457 | REDIS_NOTUSED(id); | |
458 | REDIS_NOTUSED(clientData); | |
459 | ||
7b86f5e6 | 460 | if (config.csv) return 250; |
ed0dd554 | 461 | float dt = (float)(mstime()-config.start)/1000.0; |
bdbf3acf | 462 | float rps = (float)config.requests_finished/dt; |
ed0dd554 PN |
463 | printf("%s: %.2f\r", config.title, rps); |
464 | fflush(stdout); | |
465 | return 250; /* every 250ms */ | |
466 | } | |
467 | ||
d9747b49 | 468 | /* Return true if the named test was selected using the -t command line |
469 | * switch, or if all the tests are selected (no -t passed by user). */ | |
470 | int test_is_selected(char *name) { | |
471 | char buf[256]; | |
472 | int l = strlen(name); | |
473 | ||
474 | if (config.tests == NULL) return 1; | |
475 | buf[0] = ','; | |
476 | memcpy(buf+1,name,l); | |
477 | buf[l+1] = ','; | |
478 | buf[l+2] = '\0'; | |
479 | return strstr(config.tests,buf) != NULL; | |
480 | } | |
481 | ||
fc05e8c8 | 482 | int main(int argc, const char **argv) { |
174df6fe | 483 | int i; |
39bf4402 PN |
484 | char *data, *cmd; |
485 | int len; | |
486 | ||
ed9b544e | 487 | client c; |
488 | ||
489 | signal(SIGHUP, SIG_IGN); | |
490 | signal(SIGPIPE, SIG_IGN); | |
491 | ||
492 | config.numclients = 50; | |
493 | config.requests = 10000; | |
494 | config.liveclients = 0; | |
e074416b | 495 | config.el = aeCreateEventLoop(1024*10); |
ed0dd554 | 496 | aeCreateTimeEvent(config.el,1,showThroughput,NULL,NULL); |
ed9b544e | 497 | config.keepalive = 1; |
ed9b544e | 498 | config.datasize = 3; |
57172ffb | 499 | config.randomkeys = 0; |
ecfaf6da | 500 | config.randomkeys_keyspacelen = 0; |
ed9b544e | 501 | config.quiet = 0; |
7b86f5e6 | 502 | config.csv = 0; |
ed9b544e | 503 | config.loop = 0; |
266373b2 | 504 | config.idlemode = 0; |
ed9b544e | 505 | config.latency = NULL; |
506 | config.clients = listCreate(); | |
ed9b544e | 507 | config.hostip = "127.0.0.1"; |
508 | config.hostport = 6379; | |
c61e6925 | 509 | config.hostsocket = NULL; |
d9747b49 | 510 | config.tests = NULL; |
ed9b544e | 511 | |
39bf4402 PN |
512 | i = parseOptions(argc,argv); |
513 | argc -= i; | |
514 | argv += i; | |
515 | ||
8146e316 | 516 | config.latency = zmalloc(sizeof(long long)*config.requests); |
ed9b544e | 517 | |
518 | if (config.keepalive == 0) { | |
c3251497 | 519 | printf("WARNING: keepalive disabled, you probably need 'echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse' for Linux and 'sudo sysctl -w net.inet.tcp.msl=1000' for Mac OS X in order to use a lot of clients/requests\n"); |
ed9b544e | 520 | } |
521 | ||
266373b2 | 522 | if (config.idlemode) { |
523 | printf("Creating %d idle connections and waiting forever (Ctrl+C when done)\n", config.numclients); | |
3c49070b | 524 | c = createClient("",0); /* will never receive a reply */ |
266373b2 | 525 | createMissingClients(c); |
526 | aeMain(config.el); | |
527 | /* and will wait for every */ | |
528 | } | |
529 | ||
39bf4402 PN |
530 | /* Run benchmark with command in the remainder of the arguments. */ |
531 | if (argc) { | |
532 | sds title = sdsnew(argv[0]); | |
533 | for (i = 1; i < argc; i++) { | |
534 | title = sdscatlen(title, " ", 1); | |
294cd536 | 535 | title = sdscatlen(title, (char*)argv[i], strlen(argv[i])); |
39bf4402 PN |
536 | } |
537 | ||
538 | do { | |
539 | len = redisFormatCommandArgv(&cmd,argc,argv,NULL); | |
540 | benchmark(title,cmd,len); | |
541 | free(cmd); | |
542 | } while(config.loop); | |
1cd3c1e0 | 543 | |
39bf4402 PN |
544 | return 0; |
545 | } | |
546 | ||
547 | /* Run default benchmark suite. */ | |
548 | do { | |
1cd3c1e0 | 549 | data = zmalloc(config.datasize+1); |
174df6fe PN |
550 | memset(data,'x',config.datasize); |
551 | data[config.datasize] = '\0'; | |
552 | ||
d9747b49 | 553 | if (test_is_selected("ping_inline") || test_is_selected("ping")) |
554 | benchmark("PING_INLINE","PING\r\n",6); | |
6766f45e | 555 | |
d9747b49 | 556 | if (test_is_selected("ping_mbulk") || test_is_selected("ping")) { |
557 | len = redisFormatCommand(&cmd,"PING"); | |
558 | benchmark("PING_BULK",cmd,len); | |
559 | free(cmd); | |
560 | } | |
f2f2424e | 561 | |
d9747b49 | 562 | if (test_is_selected("set")) { |
563 | len = redisFormatCommand(&cmd,"SET foo:rand:000000000000 %s",data); | |
564 | benchmark("SET",cmd,len); | |
565 | free(cmd); | |
d69a4835 | 566 | } |
ea5b7092 | 567 | |
d9747b49 | 568 | if (test_is_selected("get")) { |
569 | len = redisFormatCommand(&cmd,"GET foo:rand:000000000000"); | |
570 | benchmark("GET",cmd,len); | |
571 | free(cmd); | |
572 | } | |
ed9b544e | 573 | |
d9747b49 | 574 | if (test_is_selected("incr")) { |
575 | len = redisFormatCommand(&cmd,"INCR counter:rand:000000000000"); | |
576 | benchmark("INCR",cmd,len); | |
577 | free(cmd); | |
578 | } | |
ed9b544e | 579 | |
d9747b49 | 580 | if (test_is_selected("lpush")) { |
581 | len = redisFormatCommand(&cmd,"LPUSH mylist %s",data); | |
582 | benchmark("LPUSH",cmd,len); | |
583 | free(cmd); | |
584 | } | |
ed9b544e | 585 | |
d9747b49 | 586 | if (test_is_selected("lpop")) { |
587 | len = redisFormatCommand(&cmd,"LPOP mylist"); | |
588 | benchmark("LPOP",cmd,len); | |
589 | free(cmd); | |
590 | } | |
ed9b544e | 591 | |
d9747b49 | 592 | if (test_is_selected("sadd")) { |
593 | len = redisFormatCommand(&cmd, | |
594 | "SADD myset counter:rand:000000000000"); | |
595 | benchmark("SADD",cmd,len); | |
596 | free(cmd); | |
597 | } | |
ed9b544e | 598 | |
d9747b49 | 599 | if (test_is_selected("spop")) { |
600 | len = redisFormatCommand(&cmd,"SPOP myset"); | |
601 | benchmark("SPOP",cmd,len); | |
602 | free(cmd); | |
603 | } | |
b1ad58ed | 604 | |
d9747b49 | 605 | if (test_is_selected("lrange") || |
606 | test_is_selected("lrange_100") || | |
607 | test_is_selected("lrange_300") || | |
608 | test_is_selected("lrange_500") || | |
609 | test_is_selected("lrange_600")) | |
610 | { | |
611 | len = redisFormatCommand(&cmd,"LPUSH mylist %s",data); | |
612 | benchmark("LPUSH (needed to benchmark LRANGE)",cmd,len); | |
613 | free(cmd); | |
614 | } | |
b1ad58ed | 615 | |
d9747b49 | 616 | if (test_is_selected("lrange") || test_is_selected("lrange_100")) { |
617 | len = redisFormatCommand(&cmd,"LRANGE mylist 0 99"); | |
618 | benchmark("LRANGE_100 (first 100 elements)",cmd,len); | |
619 | free(cmd); | |
620 | } | |
2fd30952 | 621 | |
d9747b49 | 622 | if (test_is_selected("lrange") || test_is_selected("lrange_300")) { |
623 | len = redisFormatCommand(&cmd,"LRANGE mylist 0 299"); | |
624 | benchmark("LRANGE_300 (first 300 elements)",cmd,len); | |
625 | free(cmd); | |
626 | } | |
2fd30952 | 627 | |
d9747b49 | 628 | if (test_is_selected("lrange") || test_is_selected("lrange_500")) { |
629 | len = redisFormatCommand(&cmd,"LRANGE mylist 0 449"); | |
630 | benchmark("LRANGE_500 (first 450 elements)",cmd,len); | |
631 | free(cmd); | |
632 | } | |
ccb5332c | 633 | |
d9747b49 | 634 | if (test_is_selected("lrange") || test_is_selected("lrange_600")) { |
635 | len = redisFormatCommand(&cmd,"LRANGE mylist 0 599"); | |
636 | benchmark("LRANGE_600 (first 600 elements)",cmd,len); | |
637 | free(cmd); | |
638 | } | |
cc30e368 | 639 | |
d9747b49 | 640 | if (test_is_selected("mset")) { |
641 | const char *argv[21]; | |
642 | argv[0] = "MSET"; | |
643 | for (i = 1; i < 21; i += 2) { | |
644 | argv[i] = "foo:rand:000000000000"; | |
645 | argv[i+1] = data; | |
646 | } | |
647 | len = redisFormatCommandArgv(&cmd,21,argv,NULL); | |
648 | benchmark("MSET (10 keys)",cmd,len); | |
649 | free(cmd); | |
650 | } | |
cc30e368 | 651 | |
7b86f5e6 | 652 | if (!config.csv) printf("\n"); |
ed9b544e | 653 | } while(config.loop); |
654 | ||
655 | return 0; | |
656 | } |