]>
git.saurik.com Git - redis.git/blob - benchmark.c
1 /* Redis benchmark utility.
3 * Copyright (c) 2006-2009, Salvatore Sanfilippo <antirez at gmail dot com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
47 #define REPLY_RETCODE 1
50 #define CLIENT_CONNECTING 0
51 #define CLIENT_SENDQUERY 1
52 #define CLIENT_READREPLY 2
54 #define MAX_LATENCY 5000
56 #define REDIS_NOTUSED(V) ((void) V)
58 static struct config
{
66 int randomkeys_keyspacelen
;
79 typedef struct _client
{
84 int readlen
; /* readlen == -1 means read a single line */
85 unsigned int written
; /* bytes of 'obuf' already written */
87 long long start
; /* start time in milliseconds */
91 static void writeHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
92 static void createMissingClients(client c
);
95 static long long mstime(void) {
99 gettimeofday(&tv
, NULL
);
100 mst
= ((long)tv
.tv_sec
)*1000;
101 mst
+= tv
.tv_usec
/1000;
105 static void freeClient(client c
) {
108 aeDeleteFileEvent(config
.el
,c
->fd
,AE_WRITABLE
);
109 aeDeleteFileEvent(config
.el
,c
->fd
,AE_READABLE
);
114 config
.liveclients
--;
115 ln
= listSearchKey(config
.clients
,c
);
117 listDelNode(config
.clients
,ln
);
120 static void freeAllClients(void) {
121 listNode
*ln
= config
.clients
->head
, *next
;
125 freeClient(ln
->value
);
130 static 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
);
135 c
->ibuf
= sdsempty();
136 c
->readlen
= (c
->replytype
== REPLY_BULK
) ? -1 : 0;
138 c
->state
= CLIENT_SENDQUERY
;
142 static void randomizeClientKey(client c
) {
147 p
= strstr(c
->obuf
, "_rand");
150 r
= random() % config
.randomkeys_keyspacelen
;
151 sprintf(buf
,"%ld",r
);
152 memcpy(p
,buf
,strlen(buf
));
155 static void clientDone(client c
) {
157 config
.donerequests
++;
158 latency
= mstime() - c
->start
;
159 if (latency
> MAX_LATENCY
) latency
= MAX_LATENCY
;
160 config
.latency
[latency
]++;
162 if (config
.donerequests
== config
.requests
) {
167 if (config
.keepalive
) {
169 if (config
.randomkeys
) randomizeClientKey(c
);
171 config
.liveclients
--;
172 createMissingClients(c
);
173 config
.liveclients
++;
178 static void readHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
)
187 nread
= read(c
->fd
, buf
, 1024);
189 fprintf(stderr
, "Reading from socket: %s\n", strerror(errno
));
194 fprintf(stderr
, "EOF from client\n");
198 c
->ibuf
= sdscatlen(c
->ibuf
,buf
,nread
);
200 if (c
->replytype
== REPLY_INT
||
201 c
->replytype
== REPLY_RETCODE
||
202 (c
->replytype
== REPLY_BULK
&& c
->readlen
== -1)) {
205 if ((p
= strchr(c
->ibuf
,'\n')) != NULL
) {
206 if (c
->replytype
== REPLY_BULK
) {
209 c
->readlen
= atoi(c
->ibuf
+1)+2;
210 if (c
->readlen
-2 == -1) {
214 c
->ibuf
= sdsrange(c
->ibuf
,(p
-c
->ibuf
)+1,-1);
216 c
->ibuf
= sdstrim(c
->ibuf
,"\r\n");
223 if ((unsigned)c
->readlen
== sdslen(c
->ibuf
))
227 static void writeHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
)
234 if (c
->state
== CLIENT_CONNECTING
) {
235 c
->state
= CLIENT_SENDQUERY
;
238 if (sdslen(c
->obuf
) > c
->written
) {
239 void *ptr
= c
->obuf
+c
->written
;
240 int len
= sdslen(c
->obuf
) - c
->written
;
241 int nwritten
= write(c
->fd
, ptr
, len
);
242 if (nwritten
== -1) {
243 fprintf(stderr
, "Writing to socket: %s\n", strerror(errno
));
247 c
->written
+= nwritten
;
248 if (sdslen(c
->obuf
) == c
->written
) {
249 aeDeleteFileEvent(config
.el
,c
->fd
,AE_WRITABLE
);
250 aeCreateFileEvent(config
.el
,c
->fd
,AE_READABLE
,readHandler
,c
,NULL
);
251 c
->state
= CLIENT_READREPLY
;
256 static client
createClient(void) {
257 client c
= zmalloc(sizeof(struct _client
));
258 char err
[ANET_ERR_LEN
];
260 c
->fd
= anetTcpNonBlockConnect(err
,config
.hostip
,config
.hostport
);
261 if (c
->fd
== ANET_ERR
) {
263 fprintf(stderr
,"Connect: %s\n",err
);
266 anetTcpNoDelay(NULL
,c
->fd
);
267 c
->obuf
= sdsempty();
268 c
->ibuf
= sdsempty();
271 c
->state
= CLIENT_CONNECTING
;
272 aeCreateFileEvent(config
.el
, c
->fd
, AE_WRITABLE
, writeHandler
, c
, NULL
);
273 config
.liveclients
++;
274 listAddNodeTail(config
.clients
,c
);
278 static void createMissingClients(client c
) {
279 while(config
.liveclients
< config
.numclients
) {
280 client
new = createClient();
283 new->obuf
= sdsdup(c
->obuf
);
284 if (config
.randomkeys
) randomizeClientKey(c
);
285 new->replytype
= c
->replytype
;
286 if (c
->replytype
== REPLY_BULK
)
291 static void showLatencyReport(char *title
) {
293 float perc
, reqpersec
;
295 reqpersec
= (float)config
.donerequests
/((float)config
.totlatency
/1000);
297 printf("====== %s ======\n", title
);
298 printf(" %d requests completed in %.2f seconds\n", config
.donerequests
,
299 (float)config
.totlatency
/1000);
300 printf(" %d parallel clients\n", config
.numclients
);
301 printf(" %d bytes payload\n", config
.datasize
);
302 printf(" keep alive: %d\n", config
.keepalive
);
304 for (j
= 0; j
<= MAX_LATENCY
; j
++) {
305 if (config
.latency
[j
]) {
306 seen
+= config
.latency
[j
];
307 perc
= ((float)seen
*100)/config
.donerequests
;
308 printf("%.2f%% <= %d milliseconds\n", perc
, j
);
311 printf("%.2f requests per second\n\n", reqpersec
);
313 printf("%s: %.2f requests per second\n", title
, reqpersec
);
317 static void prepareForBenchmark(void)
319 memset(config
.latency
,0,sizeof(int)*(MAX_LATENCY
+1));
320 config
.start
= mstime();
321 config
.donerequests
= 0;
324 static void endBenchmark(char *title
) {
325 config
.totlatency
= mstime()-config
.start
;
326 showLatencyReport(title
);
330 void parseOptions(int argc
, char **argv
) {
333 for (i
= 1; i
< argc
; i
++) {
334 int lastarg
= i
==argc
-1;
336 if (!strcmp(argv
[i
],"-c") && !lastarg
) {
337 config
.numclients
= atoi(argv
[i
+1]);
339 } else if (!strcmp(argv
[i
],"-n") && !lastarg
) {
340 config
.requests
= atoi(argv
[i
+1]);
342 } else if (!strcmp(argv
[i
],"-k") && !lastarg
) {
343 config
.keepalive
= atoi(argv
[i
+1]);
345 } else if (!strcmp(argv
[i
],"-h") && !lastarg
) {
346 char *ip
= zmalloc(32);
347 if (anetResolve(NULL
,argv
[i
+1],ip
) == ANET_ERR
) {
348 printf("Can't resolve %s\n", argv
[i
]);
353 } else if (!strcmp(argv
[i
],"-p") && !lastarg
) {
354 config
.hostport
= atoi(argv
[i
+1]);
356 } else if (!strcmp(argv
[i
],"-d") && !lastarg
) {
357 config
.datasize
= atoi(argv
[i
+1]);
359 if (config
.datasize
< 1) config
.datasize
=1;
360 if (config
.datasize
> 1024*1024) config
.datasize
= 1024*1024;
361 } else if (!strcmp(argv
[i
],"-r") && !lastarg
) {
362 config
.randomkeys
= 1;
363 config
.randomkeys_keyspacelen
= atoi(argv
[i
+1]);
364 if (config
.randomkeys_keyspacelen
< 0)
365 config
.randomkeys_keyspacelen
= 0;
367 } else if (!strcmp(argv
[i
],"-q")) {
369 } else if (!strcmp(argv
[i
],"-l")) {
372 printf("Wrong option '%s' or option argument missing\n\n",argv
[i
]);
373 printf("Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]\n\n");
374 printf(" -h <hostname> Server hostname (default 127.0.0.1)\n");
375 printf(" -p <hostname> Server port (default 6379)\n");
376 printf(" -c <clients> Number of parallel connections (default 50)\n");
377 printf(" -n <requests> Total number of requests (default 10000)\n");
378 printf(" -d <size> Data size of SET/GET value in bytes (default 2)\n");
379 printf(" -k <boolean> 1=keep alive 0=reconnect (default 1)\n");
380 printf(" -r <keyspacelen> Use random keys for SET/GET/INCR\n");
381 printf(" Using this option the benchmark will get/set keys\n");
382 printf(" in the form mykey_rand000000012456 instead of constant\n");
383 printf(" keys, the <keyspacelen> argument determines the max\n");
384 printf(" number of values for the random number. For instance\n");
385 printf(" if set to 10 only rand000000000000 - rand000000000009\n");
386 printf(" range will be allowed.\n");
387 printf(" -q Quiet. Just show query/sec values\n");
388 printf(" -l Loop. Run the tests forever\n");
394 int main(int argc
, char **argv
) {
397 signal(SIGHUP
, SIG_IGN
);
398 signal(SIGPIPE
, SIG_IGN
);
400 config
.numclients
= 50;
401 config
.requests
= 10000;
402 config
.liveclients
= 0;
403 config
.el
= aeCreateEventLoop();
404 config
.keepalive
= 1;
405 config
.donerequests
= 0;
407 config
.randomkeys
= 0;
408 config
.randomkeys_keyspacelen
= 0;
411 config
.latency
= NULL
;
412 config
.clients
= listCreate();
413 config
.latency
= zmalloc(sizeof(int)*(MAX_LATENCY
+1));
415 config
.hostip
= "127.0.0.1";
416 config
.hostport
= 6379;
418 parseOptions(argc
,argv
);
420 if (config
.keepalive
== 0) {
421 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");
425 prepareForBenchmark();
428 c
->obuf
= sdscat(c
->obuf
,"PING\r\n");
429 c
->replytype
= REPLY_RETCODE
;
430 createMissingClients(c
);
432 endBenchmark("PING");
434 prepareForBenchmark();
437 c
->obuf
= sdscatprintf(c
->obuf
,"SET foo_rand000000000000 %d\r\n",config
.datasize
);
439 char *data
= zmalloc(config
.datasize
+2);
440 memset(data
,'x',config
.datasize
);
441 data
[config
.datasize
] = '\r';
442 data
[config
.datasize
+1] = '\n';
443 c
->obuf
= sdscatlen(c
->obuf
,data
,config
.datasize
+2);
445 c
->replytype
= REPLY_RETCODE
;
446 createMissingClients(c
);
450 prepareForBenchmark();
453 c
->obuf
= sdscat(c
->obuf
,"GET foo_rand000000000000\r\n");
454 c
->replytype
= REPLY_BULK
;
456 createMissingClients(c
);
460 prepareForBenchmark();
463 c
->obuf
= sdscat(c
->obuf
,"INCR counter_rand000000000000\r\n");
464 c
->replytype
= REPLY_INT
;
465 createMissingClients(c
);
467 endBenchmark("INCR");
469 prepareForBenchmark();
472 c
->obuf
= sdscat(c
->obuf
,"LPUSH mylist 3\r\nbar\r\n");
473 c
->replytype
= REPLY_INT
;
474 createMissingClients(c
);
476 endBenchmark("LPUSH");
478 prepareForBenchmark();
481 c
->obuf
= sdscat(c
->obuf
,"LPOP mylist\r\n");
482 c
->replytype
= REPLY_BULK
;
484 createMissingClients(c
);
486 endBenchmark("LPOP");
489 } while(config
.loop
);