]>
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.
49 #define REPLY_RETCODE 1
52 #define CLIENT_CONNECTING 0
53 #define CLIENT_SENDQUERY 1
54 #define CLIENT_READREPLY 2
56 #define MAX_LATENCY 5000
58 #define REDIS_NOTUSED(V) ((void) V)
60 static struct config
{
68 int randomkeys_keyspacelen
;
81 typedef struct _client
{
86 int readlen
; /* readlen == -1 means read a single line */
87 unsigned int written
; /* bytes of 'obuf' already written */
89 long long start
; /* start time in milliseconds */
93 static void writeHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
94 static void createMissingClients(client c
);
97 static long long mstime(void) {
101 gettimeofday(&tv
, NULL
);
102 mst
= ((long)tv
.tv_sec
)*1000;
103 mst
+= tv
.tv_usec
/1000;
107 static void freeClient(client c
) {
110 aeDeleteFileEvent(config
.el
,c
->fd
,AE_WRITABLE
);
111 aeDeleteFileEvent(config
.el
,c
->fd
,AE_READABLE
);
116 config
.liveclients
--;
117 ln
= listSearchKey(config
.clients
,c
);
119 listDelNode(config
.clients
,ln
);
122 static void freeAllClients(void) {
123 listNode
*ln
= config
.clients
->head
, *next
;
127 freeClient(ln
->value
);
132 static void resetClient(client c
) {
133 aeDeleteFileEvent(config
.el
,c
->fd
,AE_WRITABLE
);
134 aeDeleteFileEvent(config
.el
,c
->fd
,AE_READABLE
);
135 aeCreateFileEvent(config
.el
,c
->fd
, AE_WRITABLE
,writeHandler
,c
,NULL
);
137 c
->ibuf
= sdsempty();
138 c
->readlen
= (c
->replytype
== REPLY_BULK
) ? -1 : 0;
140 c
->state
= CLIENT_SENDQUERY
;
142 createMissingClients(c
);
145 static void randomizeClientKey(client c
) {
150 p
= strstr(c
->obuf
, "_rand");
153 r
= random() % config
.randomkeys_keyspacelen
;
154 sprintf(buf
,"%ld",r
);
155 memcpy(p
,buf
,strlen(buf
));
158 static void clientDone(client c
) {
160 config
.donerequests
++;
161 latency
= mstime() - c
->start
;
162 if (latency
> MAX_LATENCY
) latency
= MAX_LATENCY
;
163 config
.latency
[latency
]++;
165 if (config
.donerequests
== config
.requests
) {
170 if (config
.keepalive
) {
172 if (config
.randomkeys
) randomizeClientKey(c
);
174 config
.liveclients
--;
175 createMissingClients(c
);
176 config
.liveclients
++;
181 static void readHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
)
190 nread
= read(c
->fd
, buf
, 1024);
192 fprintf(stderr
, "Reading from socket: %s\n", strerror(errno
));
197 fprintf(stderr
, "EOF from client\n");
201 c
->ibuf
= sdscatlen(c
->ibuf
,buf
,nread
);
203 if (c
->replytype
== REPLY_INT
||
204 c
->replytype
== REPLY_RETCODE
||
205 (c
->replytype
== REPLY_BULK
&& c
->readlen
== -1)) {
208 if ((p
= strchr(c
->ibuf
,'\n')) != NULL
) {
209 if (c
->replytype
== REPLY_BULK
) {
212 c
->readlen
= atoi(c
->ibuf
+1)+2;
213 if (c
->readlen
-2 == -1) {
217 c
->ibuf
= sdsrange(c
->ibuf
,(p
-c
->ibuf
)+1,-1);
219 c
->ibuf
= sdstrim(c
->ibuf
,"\r\n");
226 if ((unsigned)c
->readlen
== sdslen(c
->ibuf
))
230 static void writeHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
)
237 if (c
->state
== CLIENT_CONNECTING
) {
238 c
->state
= CLIENT_SENDQUERY
;
241 if (sdslen(c
->obuf
) > c
->written
) {
242 void *ptr
= c
->obuf
+c
->written
;
243 int len
= sdslen(c
->obuf
) - c
->written
;
244 int nwritten
= write(c
->fd
, ptr
, len
);
245 if (nwritten
== -1) {
246 fprintf(stderr
, "Writing to socket: %s\n", strerror(errno
));
250 c
->written
+= nwritten
;
251 if (sdslen(c
->obuf
) == c
->written
) {
252 aeDeleteFileEvent(config
.el
,c
->fd
,AE_WRITABLE
);
253 aeCreateFileEvent(config
.el
,c
->fd
,AE_READABLE
,readHandler
,c
,NULL
);
254 c
->state
= CLIENT_READREPLY
;
259 static client
createClient(void) {
260 client c
= zmalloc(sizeof(struct _client
));
261 char err
[ANET_ERR_LEN
];
263 c
->fd
= anetTcpNonBlockConnect(err
,config
.hostip
,config
.hostport
);
264 if (c
->fd
== ANET_ERR
) {
266 fprintf(stderr
,"Connect: %s\n",err
);
269 anetTcpNoDelay(NULL
,c
->fd
);
270 c
->obuf
= sdsempty();
271 c
->ibuf
= sdsempty();
274 c
->state
= CLIENT_CONNECTING
;
275 aeCreateFileEvent(config
.el
, c
->fd
, AE_WRITABLE
, writeHandler
, c
, NULL
);
276 config
.liveclients
++;
277 listAddNodeTail(config
.clients
,c
);
281 static void createMissingClients(client c
) {
282 while(config
.liveclients
< config
.numclients
) {
283 client
new = createClient();
286 new->obuf
= sdsdup(c
->obuf
);
287 if (config
.randomkeys
) randomizeClientKey(c
);
288 new->replytype
= c
->replytype
;
289 if (c
->replytype
== REPLY_BULK
)
294 static void showLatencyReport(char *title
) {
296 float perc
, reqpersec
;
298 reqpersec
= (float)config
.donerequests
/((float)config
.totlatency
/1000);
300 printf("====== %s ======\n", title
);
301 printf(" %d requests completed in %.2f seconds\n", config
.donerequests
,
302 (float)config
.totlatency
/1000);
303 printf(" %d parallel clients\n", config
.numclients
);
304 printf(" %d bytes payload\n", config
.datasize
);
305 printf(" keep alive: %d\n", config
.keepalive
);
307 for (j
= 0; j
<= MAX_LATENCY
; j
++) {
308 if (config
.latency
[j
]) {
309 seen
+= config
.latency
[j
];
310 perc
= ((float)seen
*100)/config
.donerequests
;
311 printf("%.2f%% <= %d milliseconds\n", perc
, j
);
314 printf("%.2f requests per second\n\n", reqpersec
);
316 printf("%s: %.2f requests per second\n", title
, reqpersec
);
320 static void prepareForBenchmark(void)
322 memset(config
.latency
,0,sizeof(int)*(MAX_LATENCY
+1));
323 config
.start
= mstime();
324 config
.donerequests
= 0;
327 static void endBenchmark(char *title
) {
328 config
.totlatency
= mstime()-config
.start
;
329 showLatencyReport(title
);
333 void parseOptions(int argc
, char **argv
) {
336 for (i
= 1; i
< argc
; i
++) {
337 int lastarg
= i
==argc
-1;
339 if (!strcmp(argv
[i
],"-c") && !lastarg
) {
340 config
.numclients
= atoi(argv
[i
+1]);
342 } else if (!strcmp(argv
[i
],"-n") && !lastarg
) {
343 config
.requests
= atoi(argv
[i
+1]);
345 } else if (!strcmp(argv
[i
],"-k") && !lastarg
) {
346 config
.keepalive
= atoi(argv
[i
+1]);
348 } else if (!strcmp(argv
[i
],"-h") && !lastarg
) {
349 char *ip
= zmalloc(32);
350 if (anetResolve(NULL
,argv
[i
+1],ip
) == ANET_ERR
) {
351 printf("Can't resolve %s\n", argv
[i
]);
356 } else if (!strcmp(argv
[i
],"-p") && !lastarg
) {
357 config
.hostport
= atoi(argv
[i
+1]);
359 } else if (!strcmp(argv
[i
],"-d") && !lastarg
) {
360 config
.datasize
= atoi(argv
[i
+1]);
362 if (config
.datasize
< 1) config
.datasize
=1;
363 if (config
.datasize
> 1024*1024) config
.datasize
= 1024*1024;
364 } else if (!strcmp(argv
[i
],"-r") && !lastarg
) {
365 config
.randomkeys
= 1;
366 config
.randomkeys_keyspacelen
= atoi(argv
[i
+1]);
367 if (config
.randomkeys_keyspacelen
< 0)
368 config
.randomkeys_keyspacelen
= 0;
370 } else if (!strcmp(argv
[i
],"-q")) {
372 } else if (!strcmp(argv
[i
],"-l")) {
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");
378 printf(" -p <hostname> Server port (default 6379)\n");
379 printf(" -c <clients> Number of parallel connections (default 50)\n");
380 printf(" -n <requests> Total number of requests (default 10000)\n");
381 printf(" -d <size> Data size of SET/GET value in bytes (default 2)\n");
382 printf(" -k <boolean> 1=keep alive 0=reconnect (default 1)\n");
383 printf(" -r <keyspacelen> Use random keys for SET/GET/INCR\n");
384 printf(" Using this option the benchmark will get/set keys\n");
385 printf(" in the form mykey_rand000000012456 instead of constant\n");
386 printf(" keys, the <keyspacelen> argument determines the max\n");
387 printf(" number of values for the random number. For instance\n");
388 printf(" if set to 10 only rand000000000000 - rand000000000009\n");
389 printf(" range will be allowed.\n");
390 printf(" -q Quiet. Just show query/sec values\n");
391 printf(" -l Loop. Run the tests forever\n");
397 int main(int argc
, char **argv
) {
400 signal(SIGHUP
, SIG_IGN
);
401 signal(SIGPIPE
, SIG_IGN
);
403 config
.numclients
= 50;
404 config
.requests
= 10000;
405 config
.liveclients
= 0;
406 config
.el
= aeCreateEventLoop();
407 config
.keepalive
= 1;
408 config
.donerequests
= 0;
410 config
.randomkeys
= 0;
411 config
.randomkeys_keyspacelen
= 0;
414 config
.latency
= NULL
;
415 config
.clients
= listCreate();
416 config
.latency
= zmalloc(sizeof(int)*(MAX_LATENCY
+1));
418 config
.hostip
= "127.0.0.1";
419 config
.hostport
= 6379;
421 parseOptions(argc
,argv
);
423 if (config
.keepalive
== 0) {
424 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");
428 prepareForBenchmark();
431 c
->obuf
= sdscatprintf(c
->obuf
,"SET foo_rand000000000000 %d\r\n",config
.datasize
);
433 char *data
= zmalloc(config
.datasize
+2);
434 memset(data
,'x',config
.datasize
);
435 data
[config
.datasize
] = '\r';
436 data
[config
.datasize
+1] = '\n';
437 c
->obuf
= sdscatlen(c
->obuf
,data
,config
.datasize
+2);
439 c
->replytype
= REPLY_RETCODE
;
440 createMissingClients(c
);
444 prepareForBenchmark();
447 c
->obuf
= sdscat(c
->obuf
,"GET foo_rand000000000000\r\n");
448 c
->replytype
= REPLY_BULK
;
450 createMissingClients(c
);
454 prepareForBenchmark();
457 c
->obuf
= sdscat(c
->obuf
,"INCR counter_rand000000000000\r\n");
458 c
->replytype
= REPLY_INT
;
459 createMissingClients(c
);
461 endBenchmark("INCR");
463 prepareForBenchmark();
466 c
->obuf
= sdscat(c
->obuf
,"LPUSH mylist 3\r\nbar\r\n");
467 c
->replytype
= REPLY_INT
;
468 createMissingClients(c
);
470 endBenchmark("LPUSH");
472 prepareForBenchmark();
475 c
->obuf
= sdscat(c
->obuf
,"LPOP mylist\r\n");
476 c
->replytype
= REPLY_BULK
;
478 createMissingClients(c
);
480 endBenchmark("LPOP");
482 prepareForBenchmark();
485 c
->obuf
= sdscat(c
->obuf
,"PING\r\n");
486 c
->replytype
= REPLY_RETCODE
;
487 createMissingClients(c
);
489 endBenchmark("PING");
492 } while(config
.loop
);