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