]>
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
{
77 typedef struct _client
{
82 int readlen
; /* readlen == -1 means read a single line */
83 unsigned int written
; /* bytes of 'obuf' already written */
85 long long start
; /* start time in milliseconds */
89 static void writeHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
90 static void createMissingClients(client c
);
93 static long long mstime(void) {
97 gettimeofday(&tv
, NULL
);
98 mst
= ((long)tv
.tv_sec
)*1000;
99 mst
+= tv
.tv_usec
/1000;
103 static void freeClient(client c
) {
106 aeDeleteFileEvent(config
.el
,c
->fd
,AE_WRITABLE
);
107 aeDeleteFileEvent(config
.el
,c
->fd
,AE_READABLE
);
112 config
.liveclients
--;
113 ln
= listSearchKey(config
.clients
,c
);
115 listDelNode(config
.clients
,ln
);
118 static void freeAllClients(void) {
119 listNode
*ln
= config
.clients
->head
, *next
;
123 freeClient(ln
->value
);
128 static void resetClient(client c
) {
129 aeDeleteFileEvent(config
.el
,c
->fd
,AE_WRITABLE
);
130 aeDeleteFileEvent(config
.el
,c
->fd
,AE_READABLE
);
131 aeCreateFileEvent(config
.el
,c
->fd
, AE_WRITABLE
,writeHandler
,c
,NULL
);
133 c
->ibuf
= sdsempty();
134 c
->readlen
= (c
->replytype
== REPLY_BULK
) ? -1 : 0;
136 c
->state
= CLIENT_SENDQUERY
;
140 static void clientDone(client c
) {
142 config
.donerequests
++;
143 latency
= mstime() - c
->start
;
144 if (latency
> MAX_LATENCY
) latency
= MAX_LATENCY
;
145 config
.latency
[latency
]++;
147 if (config
.donerequests
== config
.requests
) {
152 if (config
.keepalive
) {
155 config
.liveclients
--;
156 createMissingClients(c
);
157 config
.liveclients
++;
162 static void readHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
)
171 nread
= read(c
->fd
, buf
, 1024);
173 fprintf(stderr
, "Reading from socket: %s\n", strerror(errno
));
178 fprintf(stderr
, "EOF from client\n");
182 c
->ibuf
= sdscatlen(c
->ibuf
,buf
,nread
);
184 if (c
->replytype
== REPLY_INT
||
185 c
->replytype
== REPLY_RETCODE
||
186 (c
->replytype
== REPLY_BULK
&& c
->readlen
== -1)) {
189 if ((p
= strchr(c
->ibuf
,'\n')) != NULL
) {
190 if (c
->replytype
== REPLY_BULK
) {
193 c
->readlen
= atoi(c
->ibuf
+1)+2;
194 if (c
->readlen
== -1) {
198 c
->ibuf
= sdsrange(c
->ibuf
,(p
-c
->ibuf
)+1,-1);
200 c
->ibuf
= sdstrim(c
->ibuf
,"\r\n");
207 if ((unsigned)c
->readlen
== sdslen(c
->ibuf
))
211 static void writeHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
)
218 if (c
->state
== CLIENT_CONNECTING
) {
219 c
->state
= CLIENT_SENDQUERY
;
222 if (sdslen(c
->obuf
) > c
->written
) {
223 void *ptr
= c
->obuf
+c
->written
;
224 int len
= sdslen(c
->obuf
) - c
->written
;
225 int nwritten
= write(c
->fd
, ptr
, len
);
226 if (nwritten
== -1) {
227 fprintf(stderr
, "Writing to socket: %s\n", strerror(errno
));
231 c
->written
+= nwritten
;
232 if (sdslen(c
->obuf
) == c
->written
) {
233 aeDeleteFileEvent(config
.el
,c
->fd
,AE_WRITABLE
);
234 aeCreateFileEvent(config
.el
,c
->fd
,AE_READABLE
,readHandler
,c
,NULL
);
235 c
->state
= CLIENT_READREPLY
;
240 static client
createClient(void) {
241 client c
= zmalloc(sizeof(struct _client
));
242 char err
[ANET_ERR_LEN
];
244 c
->fd
= anetTcpNonBlockConnect(err
,config
.hostip
,config
.hostport
);
245 if (c
->fd
== ANET_ERR
) {
247 fprintf(stderr
,"Connect: %s\n",err
);
250 anetTcpNoDelay(NULL
,c
->fd
);
251 c
->obuf
= sdsempty();
252 c
->ibuf
= sdsempty();
255 c
->state
= CLIENT_CONNECTING
;
256 aeCreateFileEvent(config
.el
, c
->fd
, AE_WRITABLE
, writeHandler
, c
, NULL
);
257 config
.liveclients
++;
258 listAddNodeTail(config
.clients
,c
);
262 static void createMissingClients(client c
) {
263 while(config
.liveclients
< config
.numclients
) {
264 client
new = createClient();
267 new->obuf
= sdsdup(c
->obuf
);
268 new->replytype
= c
->replytype
;
269 if (c
->replytype
== REPLY_BULK
)
274 static void showLatencyReport(char *title
) {
276 float perc
, reqpersec
;
278 reqpersec
= (float)config
.donerequests
/((float)config
.totlatency
/1000);
280 printf("====== %s ======\n", title
);
281 printf(" %d requests completed in %.2f seconds\n", config
.donerequests
,
282 (float)config
.totlatency
/1000);
283 printf(" %d parallel clients\n", config
.numclients
);
284 printf(" %d bytes payload\n", config
.datasize
);
285 printf(" keep alive: %d\n", config
.keepalive
);
287 for (j
= 0; j
<= MAX_LATENCY
; j
++) {
288 if (config
.latency
[j
]) {
289 seen
+= config
.latency
[j
];
290 perc
= ((float)seen
*100)/config
.donerequests
;
291 printf("%.2f%% <= %d milliseconds\n", perc
, j
);
294 printf("%.2f requests per second\n\n", reqpersec
);
296 printf("%s: %.2f requests per second\n", title
, reqpersec
);
300 static void prepareForBenchmark(void)
302 memset(config
.latency
,0,sizeof(int)*(MAX_LATENCY
+1));
303 config
.start
= mstime();
304 config
.donerequests
= 0;
307 static void endBenchmark(char *title
) {
308 config
.totlatency
= mstime()-config
.start
;
309 showLatencyReport(title
);
313 void parseOptions(int argc
, char **argv
) {
316 for (i
= 1; i
< argc
; i
++) {
317 int lastarg
= i
==argc
-1;
319 if (!strcmp(argv
[i
],"-c") && !lastarg
) {
320 config
.numclients
= atoi(argv
[i
+1]);
322 } else if (!strcmp(argv
[i
],"-n") && !lastarg
) {
323 config
.requests
= atoi(argv
[i
+1]);
325 } else if (!strcmp(argv
[i
],"-k") && !lastarg
) {
326 config
.keepalive
= atoi(argv
[i
+1]);
328 } else if (!strcmp(argv
[i
],"-h") && !lastarg
) {
329 char *ip
= zmalloc(32);
330 if (anetResolve(NULL
,argv
[i
+1],ip
) == ANET_ERR
) {
331 printf("Can't resolve %s\n", argv
[i
]);
336 } else if (!strcmp(argv
[i
],"-p") && !lastarg
) {
337 config
.hostport
= atoi(argv
[i
+1]);
339 } else if (!strcmp(argv
[i
],"-d") && !lastarg
) {
340 config
.datasize
= atoi(argv
[i
+1]);
342 if (config
.datasize
< 1) config
.datasize
=1;
343 if (config
.datasize
> 1024*1024) config
.datasize
= 1024*1024;
344 } else if (!strcmp(argv
[i
],"-q")) {
346 } else if (!strcmp(argv
[i
],"-l")) {
349 printf("Wrong option '%s' or option argument missing\n\n",argv
[i
]);
350 printf("Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]\n\n");
351 printf(" -h <hostname> Server hostname (default 127.0.0.1)\n");
352 printf(" -p <hostname> Server port (default 6379)\n");
353 printf(" -c <clients> Number of parallel connections (default 50)\n");
354 printf(" -n <requests> Total number of requests (default 10000)\n");
355 printf(" -d <size> Data size of SET/GET value in bytes (default 2)\n");
356 printf(" -k <boolean> 1=keep alive 0=reconnect (default 1)\n");
357 printf(" -q Quiet. Just show query/sec values\n");
358 printf(" -l Loop. Run the tests forever\n");
364 int main(int argc
, char **argv
) {
367 signal(SIGHUP
, SIG_IGN
);
368 signal(SIGPIPE
, SIG_IGN
);
370 config
.numclients
= 50;
371 config
.requests
= 10000;
372 config
.liveclients
= 0;
373 config
.el
= aeCreateEventLoop();
374 config
.keepalive
= 1;
375 config
.donerequests
= 0;
379 config
.latency
= NULL
;
380 config
.clients
= listCreate();
381 config
.latency
= zmalloc(sizeof(int)*(MAX_LATENCY
+1));
383 config
.hostip
= "127.0.0.1";
384 config
.hostport
= 6379;
386 parseOptions(argc
,argv
);
388 if (config
.keepalive
== 0) {
389 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");
393 prepareForBenchmark();
396 c
->obuf
= sdscat(c
->obuf
,"PING\r\n");
397 c
->replytype
= REPLY_RETCODE
;
398 createMissingClients(c
);
400 endBenchmark("PING");
402 prepareForBenchmark();
405 c
->obuf
= sdscatprintf(c
->obuf
,"SET foo %d\r\n",config
.datasize
);
407 char *data
= zmalloc(config
.datasize
+2);
408 memset(data
,'x',config
.datasize
);
409 data
[config
.datasize
] = '\r';
410 data
[config
.datasize
+1] = '\n';
411 c
->obuf
= sdscatlen(c
->obuf
,data
,config
.datasize
+2);
413 c
->replytype
= REPLY_RETCODE
;
414 createMissingClients(c
);
418 prepareForBenchmark();
421 c
->obuf
= sdscat(c
->obuf
,"GET foo\r\n");
422 c
->replytype
= REPLY_BULK
;
424 createMissingClients(c
);
428 prepareForBenchmark();
431 c
->obuf
= sdscat(c
->obuf
,"INCR counter\r\n");
432 c
->replytype
= REPLY_INT
;
433 createMissingClients(c
);
435 endBenchmark("INCR");
437 prepareForBenchmark();
440 c
->obuf
= sdscat(c
->obuf
,"LPUSH mylist 3\r\nbar\r\n");
441 c
->replytype
= REPLY_INT
;
442 createMissingClients(c
);
444 endBenchmark("LPUSH");
446 prepareForBenchmark();
449 c
->obuf
= sdscat(c
->obuf
,"LPOP mylist\r\n");
450 c
->replytype
= REPLY_BULK
;
452 createMissingClients(c
);
454 endBenchmark("LPOP");
457 } while(config
.loop
);