]>
Commit | Line | Data |
---|---|---|
1 | /* Redis benchmark utility. | |
2 | * | |
3 | * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com> | |
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 | ||
31 | #include "fmacros.h" | |
32 | ||
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" | |
43 | #include "hiredis.h" | |
44 | #include "sds.h" | |
45 | #include "adlist.h" | |
46 | #include "zmalloc.h" | |
47 | ||
48 | #define CLIENT_CONNECTING 0 | |
49 | #define CLIENT_SENDQUERY 1 | |
50 | #define CLIENT_READREPLY 2 | |
51 | ||
52 | #define REDIS_NOTUSED(V) ((void) V) | |
53 | ||
54 | static struct config { | |
55 | int debug; | |
56 | int numclients; | |
57 | int requests; | |
58 | int liveclients; | |
59 | int donerequests; | |
60 | int keysize; | |
61 | int datasize; | |
62 | int randomkeys; | |
63 | int randomkeys_keyspacelen; | |
64 | aeEventLoop *el; | |
65 | char *hostip; | |
66 | int hostport; | |
67 | char *hostsocket; | |
68 | int keepalive; | |
69 | long long start; | |
70 | long long totlatency; | |
71 | long long *latency; | |
72 | char *title; | |
73 | list *clients; | |
74 | int quiet; | |
75 | int loop; | |
76 | int idlemode; | |
77 | } config; | |
78 | ||
79 | typedef struct _client { | |
80 | redisContext *context; | |
81 | int state; | |
82 | sds obuf; | |
83 | char *randptr; | |
84 | unsigned int written; /* bytes of 'obuf' already written */ | |
85 | long long start; /* start time of a request */ | |
86 | long long latency; /* request latency */ | |
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 */ | |
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 | ||
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; | |
116 | aeDeleteFileEvent(config.el,c->context->fd,AE_WRITABLE); | |
117 | aeDeleteFileEvent(config.el,c->context->fd,AE_READABLE); | |
118 | redisFree(c->context); | |
119 | sdsfree(c->obuf); | |
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) { | |
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); | |
141 | c->written = 0; | |
142 | c->state = CLIENT_SENDQUERY; | |
143 | c->start = ustime(); | |
144 | c->latency = -1; | |
145 | } | |
146 | ||
147 | static void randomizeClientKey(client c) { | |
148 | char *p, *newline; | |
149 | char buf[32]; | |
150 | long r; | |
151 | ||
152 | if (c->randptr == NULL) return; | |
153 | ||
154 | /* Check if we have to randomize (only once per connection) */ | |
155 | if (c->randptr == (void*)-1) { | |
156 | p = strstr(c->obuf,":rand:"); | |
157 | if (!p) { | |
158 | c->randptr = NULL; | |
159 | return; | |
160 | } else { | |
161 | newline = strstr(p,"\r\n"); | |
162 | assert(newline-(p+6) == 12); /* 12 chars for randomness */ | |
163 | c->randptr = p+6; | |
164 | } | |
165 | } | |
166 | ||
167 | /* Set random number in output buffer */ | |
168 | r = random() % config.randomkeys_keyspacelen; | |
169 | snprintf(buf,sizeof(buf),"%012ld",r); | |
170 | memcpy(c->randptr,buf,12); | |
171 | } | |
172 | ||
173 | static void clientDone(client c) { | |
174 | if (config.donerequests == config.requests) { | |
175 | freeClient(c); | |
176 | aeStop(config.el); | |
177 | return; | |
178 | } | |
179 | if (config.keepalive) { | |
180 | resetClient(c); | |
181 | if (config.randomkeys) randomizeClientKey(c); | |
182 | } else { | |
183 | config.liveclients--; | |
184 | createMissingClients(c); | |
185 | config.liveclients++; | |
186 | freeClient(c); | |
187 | } | |
188 | } | |
189 | ||
190 | static void readHandler(aeEventLoop *el, int fd, void *privdata, int mask) { | |
191 | client c = privdata; | |
192 | void *reply = NULL; | |
193 | REDIS_NOTUSED(el); | |
194 | REDIS_NOTUSED(fd); | |
195 | REDIS_NOTUSED(mask); | |
196 | ||
197 | /* Calculate latency only for the first read event. This means that the | |
198 | * server already sent the reply and we need to parse it. Parsing overhead | |
199 | * is not part of the latency, so calculate it only once, here. */ | |
200 | if (c->latency < 0) c->latency = ustime()-(c->start); | |
201 | ||
202 | if (redisBufferRead(c->context) != REDIS_OK) { | |
203 | fprintf(stderr,"Error: %s\n",c->context->errstr); | |
204 | exit(1); | |
205 | } else { | |
206 | if (redisGetReply(c->context,&reply) != REDIS_OK) { | |
207 | fprintf(stderr,"Error: %s\n",c->context->errstr); | |
208 | exit(1); | |
209 | } | |
210 | if (reply != NULL) { | |
211 | if (reply == (void*)REDIS_REPLY_ERROR) { | |
212 | fprintf(stderr,"Unexpected error reply, exiting...\n"); | |
213 | exit(1); | |
214 | } | |
215 | ||
216 | if (config.donerequests < config.requests) | |
217 | config.latency[config.donerequests++] = c->latency; | |
218 | clientDone(c); | |
219 | } | |
220 | } | |
221 | } | |
222 | ||
223 | static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask) { | |
224 | client c = privdata; | |
225 | REDIS_NOTUSED(el); | |
226 | REDIS_NOTUSED(fd); | |
227 | REDIS_NOTUSED(mask); | |
228 | ||
229 | if (c->state == CLIENT_CONNECTING) { | |
230 | c->state = CLIENT_SENDQUERY; | |
231 | c->start = ustime(); | |
232 | c->latency = -1; | |
233 | } | |
234 | if (sdslen(c->obuf) > c->written) { | |
235 | void *ptr = c->obuf+c->written; | |
236 | int nwritten = write(c->context->fd,ptr,sdslen(c->obuf)-c->written); | |
237 | if (nwritten == -1) { | |
238 | if (errno != EPIPE) | |
239 | fprintf(stderr, "Writing to socket: %s\n", strerror(errno)); | |
240 | freeClient(c); | |
241 | return; | |
242 | } | |
243 | c->written += nwritten; | |
244 | if (sdslen(c->obuf) == c->written) { | |
245 | aeDeleteFileEvent(config.el,c->context->fd,AE_WRITABLE); | |
246 | aeCreateFileEvent(config.el,c->context->fd,AE_READABLE,readHandler,c); | |
247 | c->state = CLIENT_READREPLY; | |
248 | } | |
249 | } | |
250 | } | |
251 | ||
252 | static client createClient() { | |
253 | client c = zmalloc(sizeof(struct _client)); | |
254 | if (config.hostsocket == NULL) { | |
255 | c->context = redisConnectNonBlock(config.hostip,config.hostport); | |
256 | } else { | |
257 | c->context = redisConnectUnixNonBlock(config.hostsocket); | |
258 | } | |
259 | if (c->context->err) { | |
260 | fprintf(stderr,"Could not connect to Redis at "); | |
261 | if (config.hostsocket == NULL) | |
262 | fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,c->context->errstr); | |
263 | else | |
264 | fprintf(stderr,"%s: %s\n",config.hostsocket,c->context->errstr); | |
265 | exit(1); | |
266 | } | |
267 | c->state = CLIENT_CONNECTING; | |
268 | c->obuf = NULL; | |
269 | c->randptr = (void*)-1; | |
270 | c->written = 0; | |
271 | redisSetReplyObjectFunctions(c->context,NULL); | |
272 | aeCreateFileEvent(config.el,c->context->fd,AE_WRITABLE,writeHandler,c); | |
273 | listAddNodeTail(config.clients,c); | |
274 | config.liveclients++; | |
275 | return c; | |
276 | } | |
277 | ||
278 | static void createMissingClients(client c) { | |
279 | int n = 0; | |
280 | ||
281 | while(config.liveclients < config.numclients) { | |
282 | client new = createClient(); | |
283 | new->obuf = sdsdup(c->obuf); | |
284 | if (config.randomkeys) randomizeClientKey(c); | |
285 | ||
286 | /* Listen backlog is quite limited on most systems */ | |
287 | if (++n > 64) { | |
288 | usleep(50000); | |
289 | n = 0; | |
290 | } | |
291 | } | |
292 | } | |
293 | ||
294 | static int compareLatency(const void *a, const void *b) { | |
295 | return (*(long long*)a)-(*(long long*)b); | |
296 | } | |
297 | ||
298 | static void showLatencyReport(void) { | |
299 | int i, curlat = 0; | |
300 | float perc, reqpersec; | |
301 | ||
302 | reqpersec = (float)config.donerequests/((float)config.totlatency/1000); | |
303 | if (!config.quiet) { | |
304 | printf("====== %s ======\n", config.title); | |
305 | printf(" %d requests completed in %.2f seconds\n", config.donerequests, | |
306 | (float)config.totlatency/1000); | |
307 | printf(" %d parallel clients\n", config.numclients); | |
308 | printf(" %d bytes payload\n", config.datasize); | |
309 | printf(" keep alive: %d\n", config.keepalive); | |
310 | printf("\n"); | |
311 | ||
312 | qsort(config.latency,config.requests,sizeof(long long),compareLatency); | |
313 | for (i = 0; i < config.requests; i++) { | |
314 | if (config.latency[i]/1000 != curlat || i == (config.requests-1)) { | |
315 | curlat = config.latency[i]/1000; | |
316 | perc = ((float)(i+1)*100)/config.requests; | |
317 | printf("%.2f%% <= %d milliseconds\n", perc, curlat); | |
318 | } | |
319 | } | |
320 | printf("%.2f requests per second\n\n", reqpersec); | |
321 | } else { | |
322 | printf("%s: %.2f requests per second\n", config.title, reqpersec); | |
323 | } | |
324 | } | |
325 | ||
326 | static void benchmark(char *title, char *cmd, int len) { | |
327 | client c; | |
328 | ||
329 | config.title = title; | |
330 | config.donerequests = 0; | |
331 | ||
332 | c = createClient(); | |
333 | c->obuf = sdsnewlen(cmd,len); | |
334 | createMissingClients(c); | |
335 | ||
336 | config.start = mstime(); | |
337 | aeMain(config.el); | |
338 | config.totlatency = mstime()-config.start; | |
339 | ||
340 | showLatencyReport(); | |
341 | freeAllClients(); | |
342 | } | |
343 | ||
344 | void parseOptions(int argc, char **argv) { | |
345 | int i; | |
346 | ||
347 | for (i = 1; i < argc; i++) { | |
348 | int lastarg = i==argc-1; | |
349 | ||
350 | if (!strcmp(argv[i],"-c") && !lastarg) { | |
351 | config.numclients = atoi(argv[i+1]); | |
352 | i++; | |
353 | } else if (!strcmp(argv[i],"-n") && !lastarg) { | |
354 | config.requests = atoi(argv[i+1]); | |
355 | i++; | |
356 | } else if (!strcmp(argv[i],"-k") && !lastarg) { | |
357 | config.keepalive = atoi(argv[i+1]); | |
358 | i++; | |
359 | } else if (!strcmp(argv[i],"-h") && !lastarg) { | |
360 | config.hostip = argv[i+1]; | |
361 | i++; | |
362 | } else if (!strcmp(argv[i],"-p") && !lastarg) { | |
363 | config.hostport = atoi(argv[i+1]); | |
364 | i++; | |
365 | } else if (!strcmp(argv[i],"-s") && !lastarg) { | |
366 | config.hostsocket = argv[i+1]; | |
367 | i++; | |
368 | } else if (!strcmp(argv[i],"-d") && !lastarg) { | |
369 | config.datasize = atoi(argv[i+1]); | |
370 | i++; | |
371 | if (config.datasize < 1) config.datasize=1; | |
372 | if (config.datasize > 1024*1024) config.datasize = 1024*1024; | |
373 | } else if (!strcmp(argv[i],"-r") && !lastarg) { | |
374 | config.randomkeys = 1; | |
375 | config.randomkeys_keyspacelen = atoi(argv[i+1]); | |
376 | if (config.randomkeys_keyspacelen < 0) | |
377 | config.randomkeys_keyspacelen = 0; | |
378 | i++; | |
379 | } else if (!strcmp(argv[i],"-q")) { | |
380 | config.quiet = 1; | |
381 | } else if (!strcmp(argv[i],"-l")) { | |
382 | config.loop = 1; | |
383 | } else if (!strcmp(argv[i],"-D")) { | |
384 | config.debug = 1; | |
385 | } else if (!strcmp(argv[i],"-I")) { | |
386 | config.idlemode = 1; | |
387 | } else { | |
388 | printf("Wrong option '%s' or option argument missing\n\n",argv[i]); | |
389 | printf("Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]\n\n"); | |
390 | printf(" -h <hostname> Server hostname (default 127.0.0.1)\n"); | |
391 | printf(" -p <port> Server port (default 6379)\n"); | |
392 | printf(" -s <socket> Server socket (overrides host and port)\n"); | |
393 | printf(" -c <clients> Number of parallel connections (default 50)\n"); | |
394 | printf(" -n <requests> Total number of requests (default 10000)\n"); | |
395 | printf(" -d <size> Data size of SET/GET value in bytes (default 2)\n"); | |
396 | printf(" -k <boolean> 1=keep alive 0=reconnect (default 1)\n"); | |
397 | printf(" -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD\n"); | |
398 | printf(" Using this option the benchmark will get/set keys\n"); | |
399 | printf(" in the form mykey_rand000000012456 instead of constant\n"); | |
400 | printf(" keys, the <keyspacelen> argument determines the max\n"); | |
401 | printf(" number of values for the random number. For instance\n"); | |
402 | printf(" if set to 10 only rand000000000000 - rand000000000009\n"); | |
403 | printf(" range will be allowed.\n"); | |
404 | printf(" -q Quiet. Just show query/sec values\n"); | |
405 | printf(" -l Loop. Run the tests forever\n"); | |
406 | printf(" -I Idle mode. Just open N idle connections and wait.\n"); | |
407 | printf(" -D Debug mode. more verbose.\n"); | |
408 | exit(1); | |
409 | } | |
410 | } | |
411 | } | |
412 | ||
413 | int showThroughput(struct aeEventLoop *eventLoop, long long id, void *clientData) { | |
414 | REDIS_NOTUSED(eventLoop); | |
415 | REDIS_NOTUSED(id); | |
416 | REDIS_NOTUSED(clientData); | |
417 | ||
418 | float dt = (float)(mstime()-config.start)/1000.0; | |
419 | float rps = (float)config.donerequests/dt; | |
420 | printf("%s: %.2f\r", config.title, rps); | |
421 | fflush(stdout); | |
422 | return 250; /* every 250ms */ | |
423 | } | |
424 | ||
425 | int main(int argc, char **argv) { | |
426 | int i; | |
427 | client c; | |
428 | ||
429 | signal(SIGHUP, SIG_IGN); | |
430 | signal(SIGPIPE, SIG_IGN); | |
431 | ||
432 | config.debug = 0; | |
433 | config.numclients = 50; | |
434 | config.requests = 10000; | |
435 | config.liveclients = 0; | |
436 | config.el = aeCreateEventLoop(); | |
437 | aeCreateTimeEvent(config.el,1,showThroughput,NULL,NULL); | |
438 | config.keepalive = 1; | |
439 | config.donerequests = 0; | |
440 | config.datasize = 3; | |
441 | config.randomkeys = 0; | |
442 | config.randomkeys_keyspacelen = 0; | |
443 | config.quiet = 0; | |
444 | config.loop = 0; | |
445 | config.idlemode = 0; | |
446 | config.latency = NULL; | |
447 | config.clients = listCreate(); | |
448 | config.hostip = "127.0.0.1"; | |
449 | config.hostport = 6379; | |
450 | config.hostsocket = NULL; | |
451 | ||
452 | parseOptions(argc,argv); | |
453 | config.latency = zmalloc(sizeof(long long)*config.requests); | |
454 | ||
455 | if (config.keepalive == 0) { | |
456 | 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"); | |
457 | } | |
458 | ||
459 | if (config.idlemode) { | |
460 | printf("Creating %d idle connections and waiting forever (Ctrl+C when done)\n", config.numclients); | |
461 | c = createClient(0); /* will never receive a reply */ | |
462 | c->obuf = sdsempty(); | |
463 | createMissingClients(c); | |
464 | aeMain(config.el); | |
465 | /* and will wait for every */ | |
466 | } | |
467 | ||
468 | do { | |
469 | char *data, *cmd; | |
470 | int len; | |
471 | ||
472 | data = zmalloc(config.datasize+1); | |
473 | memset(data,'x',config.datasize); | |
474 | data[config.datasize] = '\0'; | |
475 | ||
476 | benchmark("PING (inline)","PING\r\n",6); | |
477 | ||
478 | len = redisFormatCommand(&cmd,"PING"); | |
479 | benchmark("PING",cmd,len); | |
480 | free(cmd); | |
481 | ||
482 | const char *argv[21]; | |
483 | argv[0] = "MSET"; | |
484 | for (i = 1; i < 21; i += 2) { | |
485 | argv[i] = "foo:rand:000000000000"; | |
486 | argv[i+1] = data; | |
487 | } | |
488 | len = redisFormatCommandArgv(&cmd,21,argv,NULL); | |
489 | benchmark("MSET (10 keys)",cmd,len); | |
490 | free(cmd); | |
491 | ||
492 | len = redisFormatCommand(&cmd,"SET foo:rand:000000000000 %s",data); | |
493 | benchmark("SET",cmd,len); | |
494 | free(cmd); | |
495 | ||
496 | len = redisFormatCommand(&cmd,"GET foo:rand:000000000000"); | |
497 | benchmark("GET",cmd,len); | |
498 | free(cmd); | |
499 | ||
500 | len = redisFormatCommand(&cmd,"INCR counter:rand:000000000000"); | |
501 | benchmark("INCR",cmd,len); | |
502 | free(cmd); | |
503 | ||
504 | len = redisFormatCommand(&cmd,"LPUSH mylist %s",data); | |
505 | benchmark("LPUSH",cmd,len); | |
506 | free(cmd); | |
507 | ||
508 | len = redisFormatCommand(&cmd,"LPOP mylist"); | |
509 | benchmark("LPOP",cmd,len); | |
510 | free(cmd); | |
511 | ||
512 | len = redisFormatCommand(&cmd,"SADD myset counter:rand:000000000000"); | |
513 | benchmark("SADD",cmd,len); | |
514 | free(cmd); | |
515 | ||
516 | len = redisFormatCommand(&cmd,"SPOP myset"); | |
517 | benchmark("SPOP",cmd,len); | |
518 | free(cmd); | |
519 | ||
520 | len = redisFormatCommand(&cmd,"LPUSH mylist %s",data); | |
521 | benchmark("LPUSH (again, in order to bench LRANGE)",cmd,len); | |
522 | free(cmd); | |
523 | ||
524 | len = redisFormatCommand(&cmd,"LRANGE mylist 0 99"); | |
525 | benchmark("LRANGE (first 100 elements)",cmd,len); | |
526 | free(cmd); | |
527 | ||
528 | len = redisFormatCommand(&cmd,"LRANGE mylist 0 299"); | |
529 | benchmark("LRANGE (first 300 elements)",cmd,len); | |
530 | free(cmd); | |
531 | ||
532 | len = redisFormatCommand(&cmd,"LRANGE mylist 0 449"); | |
533 | benchmark("LRANGE (first 450 elements)",cmd,len); | |
534 | free(cmd); | |
535 | ||
536 | len = redisFormatCommand(&cmd,"LRANGE mylist 0 599"); | |
537 | benchmark("LRANGE (first 600 elements)",cmd,len); | |
538 | free(cmd); | |
539 | ||
540 | printf("\n"); | |
541 | } while(config.loop); | |
542 | ||
543 | return 0; | |
544 | } |