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