]>
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
{
78 typedef struct _client
{
83 int readlen
; /* readlen == -1 means read a single line */
84 unsigned int written
; /* bytes of 'obuf' already written */
86 long long start
; /* start time in milliseconds */
90 static void writeHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
91 static void createMissingClients(client c
);
94 static long long mstime(void) {
98 gettimeofday(&tv
, NULL
);
99 mst
= ((long)tv
.tv_sec
)*1000;
100 mst
+= tv
.tv_usec
/1000;
104 static void freeClient(client c
) {
107 aeDeleteFileEvent(config
.el
,c
->fd
,AE_WRITABLE
);
108 aeDeleteFileEvent(config
.el
,c
->fd
,AE_READABLE
);
113 config
.liveclients
--;
114 ln
= listSearchKey(config
.clients
,c
);
116 listDelNode(config
.clients
,ln
);
119 static void freeAllClients(void) {
120 listNode
*ln
= config
.clients
->head
, *next
;
124 freeClient(ln
->value
);
129 static void resetClient(client c
) {
130 aeDeleteFileEvent(config
.el
,c
->fd
,AE_WRITABLE
);
131 aeDeleteFileEvent(config
.el
,c
->fd
,AE_READABLE
);
132 aeCreateFileEvent(config
.el
,c
->fd
, AE_WRITABLE
,writeHandler
,c
,NULL
);
134 c
->ibuf
= sdsempty();
135 c
->readlen
= (c
->replytype
== REPLY_BULK
) ? -1 : 0;
137 c
->state
= CLIENT_SENDQUERY
;
141 static void clientDone(client c
) {
143 config
.donerequests
++;
144 latency
= mstime() - c
->start
;
145 if (latency
> MAX_LATENCY
) latency
= MAX_LATENCY
;
146 config
.latency
[latency
]++;
148 if (config
.donerequests
== config
.requests
) {
153 if (config
.keepalive
) {
156 config
.liveclients
--;
157 createMissingClients(c
);
158 config
.liveclients
++;
163 static void readHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
)
172 nread
= read(c
->fd
, buf
, 1024);
174 fprintf(stderr
, "Reading from socket: %s\n", strerror(errno
));
179 fprintf(stderr
, "EOF from client\n");
183 c
->ibuf
= sdscatlen(c
->ibuf
,buf
,nread
);
185 if (c
->replytype
== REPLY_INT
||
186 c
->replytype
== REPLY_RETCODE
||
187 (c
->replytype
== REPLY_BULK
&& c
->readlen
== -1)) {
190 if ((p
= strchr(c
->ibuf
,'\n')) != NULL
) {
191 if (c
->replytype
== REPLY_BULK
) {
194 c
->readlen
= atoi(c
->ibuf
+1)+2;
195 if (c
->readlen
== -1) {
199 c
->ibuf
= sdsrange(c
->ibuf
,(p
-c
->ibuf
)+1,-1);
201 c
->ibuf
= sdstrim(c
->ibuf
,"\r\n");
208 if ((unsigned)c
->readlen
== sdslen(c
->ibuf
))
212 static void writeHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
)
219 if (c
->state
== CLIENT_CONNECTING
) {
220 c
->state
= CLIENT_SENDQUERY
;
223 if (sdslen(c
->obuf
) > c
->written
) {
224 void *ptr
= c
->obuf
+c
->written
;
225 int len
= sdslen(c
->obuf
) - c
->written
;
226 int nwritten
= write(c
->fd
, ptr
, len
);
227 if (nwritten
== -1) {
228 fprintf(stderr
, "Writing to socket: %s\n", strerror(errno
));
232 c
->written
+= nwritten
;
233 if (sdslen(c
->obuf
) == c
->written
) {
234 aeDeleteFileEvent(config
.el
,c
->fd
,AE_WRITABLE
);
235 aeCreateFileEvent(config
.el
,c
->fd
,AE_READABLE
,readHandler
,c
,NULL
);
236 c
->state
= CLIENT_READREPLY
;
241 static client
createClient(void) {
242 client c
= zmalloc(sizeof(struct _client
));
243 char err
[ANET_ERR_LEN
];
245 c
->fd
= anetTcpNonBlockConnect(err
,config
.hostip
,config
.hostport
);
246 if (c
->fd
== ANET_ERR
) {
248 fprintf(stderr
,"Connect: %s\n",err
);
251 anetTcpNoDelay(NULL
,c
->fd
);
252 c
->obuf
= sdsempty();
253 c
->ibuf
= sdsempty();
256 c
->state
= CLIENT_CONNECTING
;
257 aeCreateFileEvent(config
.el
, c
->fd
, AE_WRITABLE
, writeHandler
, c
, NULL
);
258 config
.liveclients
++;
259 listAddNodeTail(config
.clients
,c
);
263 static void createMissingClients(client c
) {
264 while(config
.liveclients
< config
.numclients
) {
265 client
new = createClient();
268 new->obuf
= sdsdup(c
->obuf
);
269 new->replytype
= c
->replytype
;
270 if (c
->replytype
== REPLY_BULK
)
275 static void showLatencyReport(char *title
) {
277 float perc
, reqpersec
;
279 reqpersec
= (float)config
.donerequests
/((float)config
.totlatency
/1000);
281 printf("====== %s ======\n", title
);
282 printf(" %d requests completed in %.2f seconds\n", config
.donerequests
,
283 (float)config
.totlatency
/1000);
284 printf(" %d parallel clients\n", config
.numclients
);
285 printf(" %d bytes payload\n", config
.datasize
);
286 printf(" keep alive: %d\n", config
.keepalive
);
288 for (j
= 0; j
<= MAX_LATENCY
; j
++) {
289 if (config
.latency
[j
]) {
290 seen
+= config
.latency
[j
];
291 perc
= ((float)seen
*100)/config
.donerequests
;
292 printf("%.2f%% <= %d milliseconds\n", perc
, j
);
295 printf("%.2f requests per second\n\n", reqpersec
);
297 printf("%s: %.2f requests per second\n", title
, reqpersec
);
301 static void prepareForBenchmark(void)
303 memset(config
.latency
,0,sizeof(int)*(MAX_LATENCY
+1));
304 config
.start
= mstime();
305 config
.donerequests
= 0;
308 static void endBenchmark(char *title
) {
309 config
.totlatency
= mstime()-config
.start
;
310 showLatencyReport(title
);
314 void parseOptions(int argc
, char **argv
) {
317 for (i
= 1; i
< argc
; i
++) {
318 int lastarg
= i
==argc
-1;
320 if (!strcmp(argv
[i
],"-c") && !lastarg
) {
321 config
.numclients
= atoi(argv
[i
+1]);
323 } else if (!strcmp(argv
[i
],"-n") && !lastarg
) {
324 config
.requests
= atoi(argv
[i
+1]);
326 } else if (!strcmp(argv
[i
],"-k") && !lastarg
) {
327 config
.keepalive
= atoi(argv
[i
+1]);
329 } else if (!strcmp(argv
[i
],"-h") && !lastarg
) {
330 char *ip
= zmalloc(32);
331 if (anetResolve(NULL
,argv
[i
+1],ip
) == ANET_ERR
) {
332 printf("Can't resolve %s\n", argv
[i
]);
337 } else if (!strcmp(argv
[i
],"-p") && !lastarg
) {
338 config
.hostport
= atoi(argv
[i
+1]);
340 } else if (!strcmp(argv
[i
],"-d") && !lastarg
) {
341 config
.datasize
= atoi(argv
[i
+1]);
343 if (config
.datasize
< 1) config
.datasize
=1;
344 if (config
.datasize
> 1024*1024) config
.datasize
= 1024*1024;
345 } else if (!strcmp(argv
[i
],"-r")) {
346 config
.randomkeys
= 1;
347 } else if (!strcmp(argv
[i
],"-q")) {
349 } else if (!strcmp(argv
[i
],"-l")) {
352 printf("Wrong option '%s' or option argument missing\n\n",argv
[i
]);
353 printf("Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]\n\n");
354 printf(" -h <hostname> Server hostname (default 127.0.0.1)\n");
355 printf(" -p <hostname> Server port (default 6379)\n");
356 printf(" -c <clients> Number of parallel connections (default 50)\n");
357 printf(" -n <requests> Total number of requests (default 10000)\n");
358 printf(" -d <size> Data size of SET/GET value in bytes (default 2)\n");
359 printf(" -k <boolean> 1=keep alive 0=reconnect (default 1)\n");
360 printf(" -r Use random keys for SET/GET/INCR\n");
361 printf(" -q Quiet. Just show query/sec values\n");
362 printf(" -l Loop. Run the tests forever\n");
368 int main(int argc
, char **argv
) {
371 signal(SIGHUP
, SIG_IGN
);
372 signal(SIGPIPE
, SIG_IGN
);
374 config
.numclients
= 50;
375 config
.requests
= 10000;
376 config
.liveclients
= 0;
377 config
.el
= aeCreateEventLoop();
378 config
.keepalive
= 1;
379 config
.donerequests
= 0;
381 config
.randomkeys
= 0;
384 config
.latency
= NULL
;
385 config
.clients
= listCreate();
386 config
.latency
= zmalloc(sizeof(int)*(MAX_LATENCY
+1));
388 config
.hostip
= "127.0.0.1";
389 config
.hostport
= 6379;
391 parseOptions(argc
,argv
);
393 if (config
.keepalive
== 0) {
394 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");
398 prepareForBenchmark();
401 c
->obuf
= sdscat(c
->obuf
,"PING\r\n");
402 c
->replytype
= REPLY_RETCODE
;
403 createMissingClients(c
);
405 endBenchmark("PING");
407 prepareForBenchmark();
410 c
->obuf
= sdscatprintf(c
->obuf
,"SET foo_rand000000000000 %d\r\n",config
.datasize
);
412 char *data
= zmalloc(config
.datasize
+2);
413 memset(data
,'x',config
.datasize
);
414 data
[config
.datasize
] = '\r';
415 data
[config
.datasize
+1] = '\n';
416 c
->obuf
= sdscatlen(c
->obuf
,data
,config
.datasize
+2);
418 c
->replytype
= REPLY_RETCODE
;
419 createMissingClients(c
);
423 prepareForBenchmark();
426 c
->obuf
= sdscat(c
->obuf
,"GET foo_rand000000000000\r\n");
427 c
->replytype
= REPLY_BULK
;
429 createMissingClients(c
);
433 prepareForBenchmark();
436 c
->obuf
= sdscat(c
->obuf
,"INCR counter_rand000000000000\r\n");
437 c
->replytype
= REPLY_INT
;
438 createMissingClients(c
);
440 endBenchmark("INCR");
442 prepareForBenchmark();
445 c
->obuf
= sdscat(c
->obuf
,"LPUSH mylist 3\r\nbar\r\n");
446 c
->replytype
= REPLY_INT
;
447 createMissingClients(c
);
449 endBenchmark("LPUSH");
451 prepareForBenchmark();
454 c
->obuf
= sdscat(c
->obuf
,"LPOP mylist\r\n");
455 c
->replytype
= REPLY_BULK
;
457 createMissingClients(c
);
459 endBenchmark("LPOP");
462 } while(config
.loop
);