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