]> git.saurik.com Git - redis.git/blob - benchmark.c
multi-bulk reply support for redis-bench, and as a result LRANGE is not tested, provi...
[redis.git] / benchmark.c
1 /* Redis benchmark utility.
2 *
3 * Copyright (c) 2006-2009, 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 "anet.h"
44 #include "sds.h"
45 #include "adlist.h"
46 #include "zmalloc.h"
47
48 #define REPLY_INT 0
49 #define REPLY_RETCODE 1
50 #define REPLY_BULK 2
51 #define REPLY_MBULK 2
52
53 #define CLIENT_CONNECTING 0
54 #define CLIENT_SENDQUERY 1
55 #define CLIENT_READREPLY 2
56
57 #define MAX_LATENCY 5000
58
59 #define REDIS_NOTUSED(V) ((void) V)
60
61 static struct config {
62 int numclients;
63 int requests;
64 int liveclients;
65 int donerequests;
66 int keysize;
67 int datasize;
68 int randomkeys;
69 int randomkeys_keyspacelen;
70 aeEventLoop *el;
71 char *hostip;
72 int hostport;
73 int keepalive;
74 long long start;
75 long long totlatency;
76 int *latency;
77 list *clients;
78 int quiet;
79 int loop;
80 } config;
81
82 typedef struct _client {
83 int state;
84 int fd;
85 sds obuf;
86 sds ibuf;
87 int mbulk; /* Number of elements in an mbulk reply */
88 int readlen; /* readlen == -1 means read a single line */
89 unsigned int written; /* bytes of 'obuf' already written */
90 int replytype;
91 long long start; /* start time in milliseconds */
92 } *client;
93
94 /* Prototypes */
95 static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask);
96 static void createMissingClients(client c);
97
98 /* Implementation */
99 static long long mstime(void) {
100 struct timeval tv;
101 long long mst;
102
103 gettimeofday(&tv, NULL);
104 mst = ((long)tv.tv_sec)*1000;
105 mst += tv.tv_usec/1000;
106 return mst;
107 }
108
109 static void freeClient(client c) {
110 listNode *ln;
111
112 aeDeleteFileEvent(config.el,c->fd,AE_WRITABLE);
113 aeDeleteFileEvent(config.el,c->fd,AE_READABLE);
114 sdsfree(c->ibuf);
115 sdsfree(c->obuf);
116 close(c->fd);
117 zfree(c);
118 config.liveclients--;
119 ln = listSearchKey(config.clients,c);
120 assert(ln != NULL);
121 listDelNode(config.clients,ln);
122 }
123
124 static void freeAllClients(void) {
125 listNode *ln = config.clients->head, *next;
126
127 while(ln) {
128 next = ln->next;
129 freeClient(ln->value);
130 ln = next;
131 }
132 }
133
134 static void resetClient(client c) {
135 aeDeleteFileEvent(config.el,c->fd,AE_WRITABLE);
136 aeDeleteFileEvent(config.el,c->fd,AE_READABLE);
137 aeCreateFileEvent(config.el,c->fd, AE_WRITABLE,writeHandler,c,NULL);
138 sdsfree(c->ibuf);
139 c->ibuf = sdsempty();
140 c->readlen = (c->replytype == REPLY_BULK) ? -1 : 0;
141 c->mbulk = -1;
142 c->written = 0;
143 c->state = CLIENT_SENDQUERY;
144 c->start = mstime();
145 createMissingClients(c);
146 }
147
148 static void randomizeClientKey(client c) {
149 char *p;
150 char buf[32];
151 long r;
152
153 p = strstr(c->obuf, "_rand");
154 if (!p) return;
155 p += 5;
156 r = random() % config.randomkeys_keyspacelen;
157 sprintf(buf,"%ld",r);
158 memcpy(p,buf,strlen(buf));
159 }
160
161 static void prepareClientForReply(client c, int type) {
162 if (type == REPLY_BULK) {
163 c->replytype = REPLY_BULK;
164 c->readlen = -1;
165 } else if (type == REPLY_MBULK) {
166 c->replytype = REPLY_MBULK;
167 c->readlen = -1;
168 c->mbulk = -1;
169 } else {
170 c->replytype = type;
171 c->readlen = 0;
172 }
173 }
174
175 static void clientDone(client c) {
176 long long latency;
177 config.donerequests ++;
178 latency = mstime() - c->start;
179 if (latency > MAX_LATENCY) latency = MAX_LATENCY;
180 config.latency[latency]++;
181
182 if (config.donerequests == config.requests) {
183 freeClient(c);
184 aeStop(config.el);
185 return;
186 }
187 if (config.keepalive) {
188 resetClient(c);
189 if (config.randomkeys) randomizeClientKey(c);
190 } else {
191 config.liveclients--;
192 createMissingClients(c);
193 config.liveclients++;
194 freeClient(c);
195 }
196 }
197
198 static void readHandler(aeEventLoop *el, int fd, void *privdata, int mask)
199 {
200 char buf[1024];
201 int nread;
202 client c = privdata;
203 REDIS_NOTUSED(el);
204 REDIS_NOTUSED(fd);
205 REDIS_NOTUSED(mask);
206
207 nread = read(c->fd, buf, 1024);
208 if (nread == -1) {
209 fprintf(stderr, "Reading from socket: %s\n", strerror(errno));
210 freeClient(c);
211 return;
212 }
213 if (nread == 0) {
214 fprintf(stderr, "EOF from client\n");
215 freeClient(c);
216 return;
217 }
218 c->ibuf = sdscatlen(c->ibuf,buf,nread);
219
220 processdata:
221 if (c->replytype == REPLY_INT ||
222 c->replytype == REPLY_RETCODE ||
223 (c->replytype == REPLY_BULK && c->readlen == -1) ||
224 (c->replytype == REPLY_MBULK && c->readlen == -1) ||
225 (c->replytype == REPLY_MBULK && c->mbulk == -1)) {
226 char *p;
227
228 if ((p = strchr(c->ibuf,'\n')) != NULL) {
229 if (c->replytype == REPLY_BULK ||
230 (c->replytype == REPLY_MBULK && c->mbulk != -1))
231 {
232 *p = '\0';
233 *(p-1) = '\0';
234 c->readlen = atoi(c->ibuf+1)+2;
235 /* Handle null bulk reply "$-1" */
236 if (c->readlen-2 == -1) {
237 clientDone(c);
238 return;
239 }
240 /* Leave all the rest in the input buffer */
241 c->ibuf = sdsrange(c->ibuf,(p-c->ibuf)+1,-1);
242 } else if (c->replytype == REPLY_MBULK && c->mbulk == -1) {
243 *p = '\0';
244 *(p-1) = '\0';
245 c->mbulk = atoi(c->ibuf+1);
246 /* Handle null bulk reply "*-1" */
247 if (c->mbulk == -1) {
248 clientDone(c);
249 return;
250 }
251 /* Leave all the rest in the input buffer */
252 c->ibuf = sdsrange(c->ibuf,(p-c->ibuf)+1,-1);
253 goto processdata;
254 } else {
255 c->ibuf = sdstrim(c->ibuf,"\r\n");
256 clientDone(c);
257 return;
258 }
259 }
260 }
261 /* bulk read, did we read everything? */
262 if ((c->replytype == REPLY_BULK || c->replytype == REPLY_MBULK) &&
263 (unsigned)c->readlen <= sdslen(c->ibuf))
264 {
265 if (c->replytype == REPLY_BULK) {
266 clientDone(c);
267 } else if (c->replytype == REPLY_MBULK) {
268 if (--c->mbulk == 0) {
269 clientDone(c);
270 } else {
271 c->ibuf = sdsrange(c->ibuf,c->readlen,-1);
272 c->readlen = -1;
273 goto processdata;
274 }
275 }
276 }
277 }
278
279 static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask)
280 {
281 client c = privdata;
282 REDIS_NOTUSED(el);
283 REDIS_NOTUSED(fd);
284 REDIS_NOTUSED(mask);
285
286 if (c->state == CLIENT_CONNECTING) {
287 c->state = CLIENT_SENDQUERY;
288 c->start = mstime();
289 }
290 if (sdslen(c->obuf) > c->written) {
291 void *ptr = c->obuf+c->written;
292 int len = sdslen(c->obuf) - c->written;
293 int nwritten = write(c->fd, ptr, len);
294 if (nwritten == -1) {
295 fprintf(stderr, "Writing to socket: %s\n", strerror(errno));
296 freeClient(c);
297 return;
298 }
299 c->written += nwritten;
300 if (sdslen(c->obuf) == c->written) {
301 aeDeleteFileEvent(config.el,c->fd,AE_WRITABLE);
302 aeCreateFileEvent(config.el,c->fd,AE_READABLE,readHandler,c,NULL);
303 c->state = CLIENT_READREPLY;
304 }
305 }
306 }
307
308 static client createClient(void) {
309 client c = zmalloc(sizeof(struct _client));
310 char err[ANET_ERR_LEN];
311
312 c->fd = anetTcpNonBlockConnect(err,config.hostip,config.hostport);
313 if (c->fd == ANET_ERR) {
314 zfree(c);
315 fprintf(stderr,"Connect: %s\n",err);
316 return NULL;
317 }
318 anetTcpNoDelay(NULL,c->fd);
319 c->obuf = sdsempty();
320 c->ibuf = sdsempty();
321 c->mbulk = -1;
322 c->readlen = 0;
323 c->written = 0;
324 c->state = CLIENT_CONNECTING;
325 aeCreateFileEvent(config.el, c->fd, AE_WRITABLE, writeHandler, c, NULL);
326 config.liveclients++;
327 listAddNodeTail(config.clients,c);
328 return c;
329 }
330
331 static void createMissingClients(client c) {
332 while(config.liveclients < config.numclients) {
333 client new = createClient();
334 if (!new) continue;
335 sdsfree(new->obuf);
336 new->obuf = sdsdup(c->obuf);
337 if (config.randomkeys) randomizeClientKey(c);
338 new->replytype = c->replytype;
339 if (c->replytype == REPLY_BULK)
340 new->readlen = -1;
341 }
342 }
343
344 static void showLatencyReport(char *title) {
345 int j, seen = 0;
346 float perc, reqpersec;
347
348 reqpersec = (float)config.donerequests/((float)config.totlatency/1000);
349 if (!config.quiet) {
350 printf("====== %s ======\n", title);
351 printf(" %d requests completed in %.2f seconds\n", config.donerequests,
352 (float)config.totlatency/1000);
353 printf(" %d parallel clients\n", config.numclients);
354 printf(" %d bytes payload\n", config.datasize);
355 printf(" keep alive: %d\n", config.keepalive);
356 printf("\n");
357 for (j = 0; j <= MAX_LATENCY; j++) {
358 if (config.latency[j]) {
359 seen += config.latency[j];
360 perc = ((float)seen*100)/config.donerequests;
361 printf("%.2f%% <= %d milliseconds\n", perc, j);
362 }
363 }
364 printf("%.2f requests per second\n\n", reqpersec);
365 } else {
366 printf("%s: %.2f requests per second\n", title, reqpersec);
367 }
368 }
369
370 static void prepareForBenchmark(void)
371 {
372 memset(config.latency,0,sizeof(int)*(MAX_LATENCY+1));
373 config.start = mstime();
374 config.donerequests = 0;
375 }
376
377 static void endBenchmark(char *title) {
378 config.totlatency = mstime()-config.start;
379 showLatencyReport(title);
380 freeAllClients();
381 }
382
383 void parseOptions(int argc, char **argv) {
384 int i;
385
386 for (i = 1; i < argc; i++) {
387 int lastarg = i==argc-1;
388
389 if (!strcmp(argv[i],"-c") && !lastarg) {
390 config.numclients = atoi(argv[i+1]);
391 i++;
392 } else if (!strcmp(argv[i],"-n") && !lastarg) {
393 config.requests = atoi(argv[i+1]);
394 i++;
395 } else if (!strcmp(argv[i],"-k") && !lastarg) {
396 config.keepalive = atoi(argv[i+1]);
397 i++;
398 } else if (!strcmp(argv[i],"-h") && !lastarg) {
399 char *ip = zmalloc(32);
400 if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) {
401 printf("Can't resolve %s\n", argv[i]);
402 exit(1);
403 }
404 config.hostip = ip;
405 i++;
406 } else if (!strcmp(argv[i],"-p") && !lastarg) {
407 config.hostport = atoi(argv[i+1]);
408 i++;
409 } else if (!strcmp(argv[i],"-d") && !lastarg) {
410 config.datasize = atoi(argv[i+1]);
411 i++;
412 if (config.datasize < 1) config.datasize=1;
413 if (config.datasize > 1024*1024) config.datasize = 1024*1024;
414 } else if (!strcmp(argv[i],"-r") && !lastarg) {
415 config.randomkeys = 1;
416 config.randomkeys_keyspacelen = atoi(argv[i+1]);
417 if (config.randomkeys_keyspacelen < 0)
418 config.randomkeys_keyspacelen = 0;
419 i++;
420 } else if (!strcmp(argv[i],"-q")) {
421 config.quiet = 1;
422 } else if (!strcmp(argv[i],"-l")) {
423 config.loop = 1;
424 } else {
425 printf("Wrong option '%s' or option argument missing\n\n",argv[i]);
426 printf("Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]\n\n");
427 printf(" -h <hostname> Server hostname (default 127.0.0.1)\n");
428 printf(" -p <hostname> Server port (default 6379)\n");
429 printf(" -c <clients> Number of parallel connections (default 50)\n");
430 printf(" -n <requests> Total number of requests (default 10000)\n");
431 printf(" -d <size> Data size of SET/GET value in bytes (default 2)\n");
432 printf(" -k <boolean> 1=keep alive 0=reconnect (default 1)\n");
433 printf(" -r <keyspacelen> Use random keys for SET/GET/INCR\n");
434 printf(" Using this option the benchmark will get/set keys\n");
435 printf(" in the form mykey_rand000000012456 instead of constant\n");
436 printf(" keys, the <keyspacelen> argument determines the max\n");
437 printf(" number of values for the random number. For instance\n");
438 printf(" if set to 10 only rand000000000000 - rand000000000009\n");
439 printf(" range will be allowed.\n");
440 printf(" -q Quiet. Just show query/sec values\n");
441 printf(" -l Loop. Run the tests forever\n");
442 exit(1);
443 }
444 }
445 }
446
447 int main(int argc, char **argv) {
448 client c;
449
450 signal(SIGHUP, SIG_IGN);
451 signal(SIGPIPE, SIG_IGN);
452
453 config.numclients = 50;
454 config.requests = 10000;
455 config.liveclients = 0;
456 config.el = aeCreateEventLoop();
457 config.keepalive = 1;
458 config.donerequests = 0;
459 config.datasize = 3;
460 config.randomkeys = 0;
461 config.randomkeys_keyspacelen = 0;
462 config.quiet = 0;
463 config.loop = 0;
464 config.latency = NULL;
465 config.clients = listCreate();
466 config.latency = zmalloc(sizeof(int)*(MAX_LATENCY+1));
467
468 config.hostip = "127.0.0.1";
469 config.hostport = 6379;
470
471 parseOptions(argc,argv);
472
473 if (config.keepalive == 0) {
474 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");
475 }
476
477 do {
478 prepareForBenchmark();
479 c = createClient();
480 if (!c) exit(1);
481 c->obuf = sdscatprintf(c->obuf,"SET foo_rand000000000000 %d\r\n",config.datasize);
482 {
483 char *data = zmalloc(config.datasize+2);
484 memset(data,'x',config.datasize);
485 data[config.datasize] = '\r';
486 data[config.datasize+1] = '\n';
487 c->obuf = sdscatlen(c->obuf,data,config.datasize+2);
488 }
489 prepareClientForReply(c,REPLY_RETCODE);
490 createMissingClients(c);
491 aeMain(config.el);
492 endBenchmark("SET");
493
494 prepareForBenchmark();
495 c = createClient();
496 if (!c) exit(1);
497 c->obuf = sdscat(c->obuf,"GET foo_rand000000000000\r\n");
498 prepareClientForReply(c,REPLY_BULK);
499 createMissingClients(c);
500 aeMain(config.el);
501 endBenchmark("GET");
502
503 prepareForBenchmark();
504 c = createClient();
505 if (!c) exit(1);
506 c->obuf = sdscat(c->obuf,"INCR counter_rand000000000000\r\n");
507 prepareClientForReply(c,REPLY_INT);
508 createMissingClients(c);
509 aeMain(config.el);
510 endBenchmark("INCR");
511
512 prepareForBenchmark();
513 c = createClient();
514 if (!c) exit(1);
515 c->obuf = sdscat(c->obuf,"LPUSH mylist 3\r\nbar\r\n");
516 prepareClientForReply(c,REPLY_INT);
517 createMissingClients(c);
518 aeMain(config.el);
519 endBenchmark("LPUSH");
520
521 prepareForBenchmark();
522 c = createClient();
523 if (!c) exit(1);
524 c->obuf = sdscat(c->obuf,"LPOP mylist\r\n");
525 prepareClientForReply(c,REPLY_BULK);
526 createMissingClients(c);
527 aeMain(config.el);
528 endBenchmark("LPOP");
529
530 prepareForBenchmark();
531 c = createClient();
532 if (!c) exit(1);
533 c->obuf = sdscat(c->obuf,"PING\r\n");
534 prepareClientForReply(c,REPLY_RETCODE);
535 createMissingClients(c);
536 aeMain(config.el);
537 endBenchmark("PING");
538
539 prepareForBenchmark();
540 c = createClient();
541 if (!c) exit(1);
542 c->obuf = sdscat(c->obuf,"LPUSH mylist 3\r\nbar\r\n");
543 prepareClientForReply(c,REPLY_RETCODE);
544 createMissingClients(c);
545 aeMain(config.el);
546 endBenchmark("LPUSH (again, in order to bench LRANGE)");
547
548 prepareForBenchmark();
549 c = createClient();
550 if (!c) exit(1);
551 c->obuf = sdscat(c->obuf,"LRANGE mylist 0 99\r\n");
552 prepareClientForReply(c,REPLY_MBULK);
553 createMissingClients(c);
554 aeMain(config.el);
555 endBenchmark("LRANGE (first 100 elements)");
556
557 printf("\n");
558 } while(config.loop);
559
560 return 0;
561 }