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