]> git.saurik.com Git - redis.git/blame - benchmark.c
redis-benchmark multi bulk reply support hopefully fixed
[redis.git] / benchmark.c
CommitLineData
ed9b544e 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
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"
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
6c4e61b3 51#define REPLY_MBULK 3
ed9b544e 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
61static struct config {
62 int numclients;
63 int requests;
64 int liveclients;
65 int donerequests;
66 int keysize;
67 int datasize;
57172ffb 68 int randomkeys;
ecfaf6da 69 int randomkeys_keyspacelen;
ed9b544e 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
82typedef struct _client {
83 int state;
84 int fd;
85 sds obuf;
86 sds ibuf;
2fd30952 87 int mbulk; /* Number of elements in an mbulk reply */
ed9b544e 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 */
95static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask);
96static void createMissingClients(client c);
97
98/* Implementation */
99static 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
109static 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
124static 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
134static 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;
2fd30952 141 c->mbulk = -1;
ed9b544e 142 c->written = 0;
143 c->state = CLIENT_SENDQUERY;
144 c->start = mstime();
74077975 145 createMissingClients(c);
ed9b544e 146}
147
ecfaf6da 148static 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
2fd30952 161static 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
ed9b544e 175static 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);
ecfaf6da 189 if (config.randomkeys) randomizeClientKey(c);
ed9b544e 190 } else {
191 config.liveclients--;
192 createMissingClients(c);
193 config.liveclients++;
194 freeClient(c);
195 }
196}
197
198static 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
2fd30952 220processdata:
6c4e61b3 221 /* Are we waiting for the first line of the command of for sdf
222 * count in bulk or multi bulk operations? */
ed9b544e 223 if (c->replytype == REPLY_INT ||
224 c->replytype == REPLY_RETCODE ||
2fd30952 225 (c->replytype == REPLY_BULK && c->readlen == -1) ||
226 (c->replytype == REPLY_MBULK && c->readlen == -1) ||
227 (c->replytype == REPLY_MBULK && c->mbulk == -1)) {
ed9b544e 228 char *p;
229
6c4e61b3 230 /* Check if the first line is complete. This is only true if
231 * there is a newline inside the buffer. */
ed9b544e 232 if ((p = strchr(c->ibuf,'\n')) != NULL) {
2fd30952 233 if (c->replytype == REPLY_BULK ||
234 (c->replytype == REPLY_MBULK && c->mbulk != -1))
235 {
6c4e61b3 236 /* Read the count of a bulk reply (being it a single bulk or
237 * a multi bulk reply). "$<count>" for the protocol spec. */
ed9b544e 238 *p = '\0';
239 *(p-1) = '\0';
4c26a79a 240 c->readlen = atoi(c->ibuf+1)+2;
2fd30952 241 /* Handle null bulk reply "$-1" */
ecfaf6da 242 if (c->readlen-2 == -1) {
ed9b544e 243 clientDone(c);
244 return;
245 }
2fd30952 246 /* Leave all the rest in the input buffer */
247 c->ibuf = sdsrange(c->ibuf,(p-c->ibuf)+1,-1);
6c4e61b3 248 /* fall through to reach the point where the code will try
249 * to check if the bulk reply is complete. */
2fd30952 250 } else if (c->replytype == REPLY_MBULK && c->mbulk == -1) {
6c4e61b3 251 /* Read the count of a multi bulk reply. That is, how many
252 * bulk replies we have to read next. "*<count>" protocol. */
2fd30952 253 *p = '\0';
254 *(p-1) = '\0';
255 c->mbulk = atoi(c->ibuf+1);
256 /* Handle null bulk reply "*-1" */
257 if (c->mbulk == -1) {
258 clientDone(c);
259 return;
260 }
261 /* Leave all the rest in the input buffer */
ed9b544e 262 c->ibuf = sdsrange(c->ibuf,(p-c->ibuf)+1,-1);
2fd30952 263 goto processdata;
ed9b544e 264 } else {
265 c->ibuf = sdstrim(c->ibuf,"\r\n");
266 clientDone(c);
267 return;
268 }
269 }
270 }
2fd30952 271 /* bulk read, did we read everything? */
6c4e61b3 272 if (((c->replytype == REPLY_MBULK && c->mbulk != -1) ||
273 (c->replytype == REPLY_BULK)) && c->readlen != -1 &&
274 (unsigned)c->readlen <= sdslen(c->ibuf))
2fd30952 275 {
276 if (c->replytype == REPLY_BULK) {
277 clientDone(c);
278 } else if (c->replytype == REPLY_MBULK) {
279 if (--c->mbulk == 0) {
280 clientDone(c);
281 } else {
282 c->ibuf = sdsrange(c->ibuf,c->readlen,-1);
283 c->readlen = -1;
284 goto processdata;
285 }
286 }
287 }
ed9b544e 288}
289
290static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask)
291{
292 client c = privdata;
293 REDIS_NOTUSED(el);
294 REDIS_NOTUSED(fd);
295 REDIS_NOTUSED(mask);
296
297 if (c->state == CLIENT_CONNECTING) {
298 c->state = CLIENT_SENDQUERY;
299 c->start = mstime();
300 }
301 if (sdslen(c->obuf) > c->written) {
302 void *ptr = c->obuf+c->written;
303 int len = sdslen(c->obuf) - c->written;
304 int nwritten = write(c->fd, ptr, len);
305 if (nwritten == -1) {
306 fprintf(stderr, "Writing to socket: %s\n", strerror(errno));
307 freeClient(c);
308 return;
309 }
310 c->written += nwritten;
311 if (sdslen(c->obuf) == c->written) {
312 aeDeleteFileEvent(config.el,c->fd,AE_WRITABLE);
313 aeCreateFileEvent(config.el,c->fd,AE_READABLE,readHandler,c,NULL);
314 c->state = CLIENT_READREPLY;
315 }
316 }
317}
318
319static client createClient(void) {
320 client c = zmalloc(sizeof(struct _client));
321 char err[ANET_ERR_LEN];
322
323 c->fd = anetTcpNonBlockConnect(err,config.hostip,config.hostport);
324 if (c->fd == ANET_ERR) {
325 zfree(c);
326 fprintf(stderr,"Connect: %s\n",err);
327 return NULL;
328 }
329 anetTcpNoDelay(NULL,c->fd);
330 c->obuf = sdsempty();
331 c->ibuf = sdsempty();
2fd30952 332 c->mbulk = -1;
ed9b544e 333 c->readlen = 0;
334 c->written = 0;
335 c->state = CLIENT_CONNECTING;
336 aeCreateFileEvent(config.el, c->fd, AE_WRITABLE, writeHandler, c, NULL);
337 config.liveclients++;
338 listAddNodeTail(config.clients,c);
339 return c;
340}
341
342static void createMissingClients(client c) {
343 while(config.liveclients < config.numclients) {
344 client new = createClient();
345 if (!new) continue;
346 sdsfree(new->obuf);
347 new->obuf = sdsdup(c->obuf);
ecfaf6da 348 if (config.randomkeys) randomizeClientKey(c);
ed9b544e 349 new->replytype = c->replytype;
350 if (c->replytype == REPLY_BULK)
351 new->readlen = -1;
352 }
353}
354
355static void showLatencyReport(char *title) {
356 int j, seen = 0;
357 float perc, reqpersec;
358
359 reqpersec = (float)config.donerequests/((float)config.totlatency/1000);
360 if (!config.quiet) {
361 printf("====== %s ======\n", title);
362 printf(" %d requests completed in %.2f seconds\n", config.donerequests,
363 (float)config.totlatency/1000);
364 printf(" %d parallel clients\n", config.numclients);
365 printf(" %d bytes payload\n", config.datasize);
366 printf(" keep alive: %d\n", config.keepalive);
367 printf("\n");
368 for (j = 0; j <= MAX_LATENCY; j++) {
369 if (config.latency[j]) {
370 seen += config.latency[j];
371 perc = ((float)seen*100)/config.donerequests;
372 printf("%.2f%% <= %d milliseconds\n", perc, j);
373 }
374 }
375 printf("%.2f requests per second\n\n", reqpersec);
376 } else {
377 printf("%s: %.2f requests per second\n", title, reqpersec);
378 }
379}
380
381static void prepareForBenchmark(void)
382{
383 memset(config.latency,0,sizeof(int)*(MAX_LATENCY+1));
384 config.start = mstime();
385 config.donerequests = 0;
386}
387
388static void endBenchmark(char *title) {
389 config.totlatency = mstime()-config.start;
390 showLatencyReport(title);
391 freeAllClients();
392}
393
394void parseOptions(int argc, char **argv) {
395 int i;
396
397 for (i = 1; i < argc; i++) {
398 int lastarg = i==argc-1;
399
400 if (!strcmp(argv[i],"-c") && !lastarg) {
401 config.numclients = atoi(argv[i+1]);
402 i++;
403 } else if (!strcmp(argv[i],"-n") && !lastarg) {
404 config.requests = atoi(argv[i+1]);
405 i++;
406 } else if (!strcmp(argv[i],"-k") && !lastarg) {
407 config.keepalive = atoi(argv[i+1]);
408 i++;
409 } else if (!strcmp(argv[i],"-h") && !lastarg) {
410 char *ip = zmalloc(32);
411 if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) {
412 printf("Can't resolve %s\n", argv[i]);
413 exit(1);
414 }
415 config.hostip = ip;
416 i++;
417 } else if (!strcmp(argv[i],"-p") && !lastarg) {
418 config.hostport = atoi(argv[i+1]);
419 i++;
420 } else if (!strcmp(argv[i],"-d") && !lastarg) {
421 config.datasize = atoi(argv[i+1]);
422 i++;
423 if (config.datasize < 1) config.datasize=1;
424 if (config.datasize > 1024*1024) config.datasize = 1024*1024;
ecfaf6da 425 } else if (!strcmp(argv[i],"-r") && !lastarg) {
57172ffb 426 config.randomkeys = 1;
ecfaf6da 427 config.randomkeys_keyspacelen = atoi(argv[i+1]);
428 if (config.randomkeys_keyspacelen < 0)
429 config.randomkeys_keyspacelen = 0;
430 i++;
ed9b544e 431 } else if (!strcmp(argv[i],"-q")) {
432 config.quiet = 1;
433 } else if (!strcmp(argv[i],"-l")) {
434 config.loop = 1;
435 } else {
436 printf("Wrong option '%s' or option argument missing\n\n",argv[i]);
437 printf("Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]\n\n");
438 printf(" -h <hostname> Server hostname (default 127.0.0.1)\n");
439 printf(" -p <hostname> Server port (default 6379)\n");
440 printf(" -c <clients> Number of parallel connections (default 50)\n");
441 printf(" -n <requests> Total number of requests (default 10000)\n");
442 printf(" -d <size> Data size of SET/GET value in bytes (default 2)\n");
443 printf(" -k <boolean> 1=keep alive 0=reconnect (default 1)\n");
ecfaf6da 444 printf(" -r <keyspacelen> Use random keys for SET/GET/INCR\n");
445 printf(" Using this option the benchmark will get/set keys\n");
446 printf(" in the form mykey_rand000000012456 instead of constant\n");
447 printf(" keys, the <keyspacelen> argument determines the max\n");
448 printf(" number of values for the random number. For instance\n");
449 printf(" if set to 10 only rand000000000000 - rand000000000009\n");
450 printf(" range will be allowed.\n");
ed9b544e 451 printf(" -q Quiet. Just show query/sec values\n");
452 printf(" -l Loop. Run the tests forever\n");
453 exit(1);
454 }
455 }
456}
457
458int main(int argc, char **argv) {
459 client c;
460
461 signal(SIGHUP, SIG_IGN);
462 signal(SIGPIPE, SIG_IGN);
463
464 config.numclients = 50;
465 config.requests = 10000;
466 config.liveclients = 0;
467 config.el = aeCreateEventLoop();
468 config.keepalive = 1;
469 config.donerequests = 0;
470 config.datasize = 3;
57172ffb 471 config.randomkeys = 0;
ecfaf6da 472 config.randomkeys_keyspacelen = 0;
ed9b544e 473 config.quiet = 0;
474 config.loop = 0;
475 config.latency = NULL;
476 config.clients = listCreate();
477 config.latency = zmalloc(sizeof(int)*(MAX_LATENCY+1));
478
479 config.hostip = "127.0.0.1";
480 config.hostport = 6379;
481
482 parseOptions(argc,argv);
483
484 if (config.keepalive == 0) {
c3251497 485 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 486 }
487
488 do {
ed9b544e 489 prepareForBenchmark();
490 c = createClient();
491 if (!c) exit(1);
57172ffb 492 c->obuf = sdscatprintf(c->obuf,"SET foo_rand000000000000 %d\r\n",config.datasize);
ed9b544e 493 {
494 char *data = zmalloc(config.datasize+2);
495 memset(data,'x',config.datasize);
496 data[config.datasize] = '\r';
497 data[config.datasize+1] = '\n';
498 c->obuf = sdscatlen(c->obuf,data,config.datasize+2);
499 }
2fd30952 500 prepareClientForReply(c,REPLY_RETCODE);
ed9b544e 501 createMissingClients(c);
502 aeMain(config.el);
503 endBenchmark("SET");
504
505 prepareForBenchmark();
506 c = createClient();
507 if (!c) exit(1);
57172ffb 508 c->obuf = sdscat(c->obuf,"GET foo_rand000000000000\r\n");
2fd30952 509 prepareClientForReply(c,REPLY_BULK);
ed9b544e 510 createMissingClients(c);
511 aeMain(config.el);
512 endBenchmark("GET");
513
514 prepareForBenchmark();
515 c = createClient();
516 if (!c) exit(1);
57172ffb 517 c->obuf = sdscat(c->obuf,"INCR counter_rand000000000000\r\n");
2fd30952 518 prepareClientForReply(c,REPLY_INT);
ed9b544e 519 createMissingClients(c);
520 aeMain(config.el);
521 endBenchmark("INCR");
522
523 prepareForBenchmark();
524 c = createClient();
525 if (!c) exit(1);
526 c->obuf = sdscat(c->obuf,"LPUSH mylist 3\r\nbar\r\n");
2fd30952 527 prepareClientForReply(c,REPLY_INT);
ed9b544e 528 createMissingClients(c);
529 aeMain(config.el);
530 endBenchmark("LPUSH");
531
532 prepareForBenchmark();
533 c = createClient();
534 if (!c) exit(1);
535 c->obuf = sdscat(c->obuf,"LPOP mylist\r\n");
2fd30952 536 prepareClientForReply(c,REPLY_BULK);
ed9b544e 537 createMissingClients(c);
538 aeMain(config.el);
539 endBenchmark("LPOP");
540
9f3c422c 541 prepareForBenchmark();
542 c = createClient();
543 if (!c) exit(1);
544 c->obuf = sdscat(c->obuf,"PING\r\n");
2fd30952 545 prepareClientForReply(c,REPLY_RETCODE);
9f3c422c 546 createMissingClients(c);
547 aeMain(config.el);
548 endBenchmark("PING");
549
2fd30952 550 prepareForBenchmark();
551 c = createClient();
552 if (!c) exit(1);
553 c->obuf = sdscat(c->obuf,"LPUSH mylist 3\r\nbar\r\n");
554 prepareClientForReply(c,REPLY_RETCODE);
555 createMissingClients(c);
556 aeMain(config.el);
557 endBenchmark("LPUSH (again, in order to bench LRANGE)");
558
559 prepareForBenchmark();
560 c = createClient();
561 if (!c) exit(1);
562 c->obuf = sdscat(c->obuf,"LRANGE mylist 0 99\r\n");
563 prepareClientForReply(c,REPLY_MBULK);
564 createMissingClients(c);
565 aeMain(config.el);
566 endBenchmark("LRANGE (first 100 elements)");
567
ed9b544e 568 printf("\n");
569 } while(config.loop);
570
571 return 0;
572}