]>
git.saurik.com Git - redis.git/blob - src/redis-benchmark.c
1 /* Redis benchmark utility.
3 * Copyright (c) 2009-2010, 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
53 #define CLIENT_CONNECTING 0
54 #define CLIENT_SENDQUERY 1
55 #define CLIENT_READREPLY 2
57 #define MAX_LATENCY 5000
59 #define REDIS_NOTUSED(V) ((void) V)
61 static struct config
{
70 int randomkeys_keyspacelen
;
86 typedef struct _client
{
91 int mbulk
; /* Number of elements in an mbulk reply */
92 int readlen
; /* readlen == -1 means read a single line */
94 unsigned int written
; /* bytes of 'obuf' already written */
96 long long start
; /* start time in milliseconds */
100 static void writeHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
);
101 static void createMissingClients ( client c
);
104 static long long mstime ( void ) {
108 gettimeofday (& tv
, NULL
);
109 mst
= (( long ) tv
. tv_sec
)* 1000 ;
110 mst
+= tv
. tv_usec
/ 1000 ;
114 static void freeClient ( client c
) {
117 aeDeleteFileEvent ( config
. el
, c
-> fd
, AE_WRITABLE
);
118 aeDeleteFileEvent ( config
. el
, c
-> fd
, AE_READABLE
);
123 config
. liveclients
--;
124 ln
= listSearchKey ( config
. clients
, c
);
126 listDelNode ( config
. clients
, ln
);
129 static void freeAllClients ( void ) {
130 listNode
* ln
= config
. clients
-> head
, * next
;
134 freeClient ( ln
-> value
);
139 static void resetClient ( client c
) {
140 aeDeleteFileEvent ( config
. el
, c
-> fd
, AE_WRITABLE
);
141 aeDeleteFileEvent ( config
. el
, c
-> fd
, AE_READABLE
);
142 aeCreateFileEvent ( config
. el
, c
-> fd
, AE_WRITABLE
, writeHandler
, c
);
144 c
-> ibuf
= sdsempty ();
145 c
-> readlen
= ( c
-> replytype
== REPLY_BULK
||
146 c
-> replytype
== REPLY_MBULK
) ? - 1 : 0 ;
150 c
-> state
= CLIENT_SENDQUERY
;
152 createMissingClients ( c
);
155 static void randomizeClientKey ( client c
) {
160 p
= strstr ( c
-> obuf
, "_rand" );
163 r
= random () % config
. randomkeys_keyspacelen
;
164 sprintf ( buf
, " %l d" , r
);
165 memcpy ( p
, buf
, strlen ( buf
));
168 static void prepareClientForReply ( client c
, int type
) {
169 if ( type
== REPLY_BULK
) {
170 c
-> replytype
= REPLY_BULK
;
172 } else if ( type
== REPLY_MBULK
) {
173 c
-> replytype
= REPLY_MBULK
;
182 static void clientDone ( client c
) {
183 static int last_tot_received
= 1 ;
186 config
. donerequests
++;
187 latency
= mstime () - c
-> start
;
188 if ( latency
> MAX_LATENCY
) latency
= MAX_LATENCY
;
189 config
. latency
[ latency
]++;
191 if ( config
. debug
&& last_tot_received
!= c
-> totreceived
) {
192 printf ( "Tot bytes received: %d \n " , c
-> totreceived
);
193 last_tot_received
= c
-> totreceived
;
195 if ( config
. donerequests
== config
. requests
) {
200 if ( config
. keepalive
) {
202 if ( config
. randomkeys
) randomizeClientKey ( c
);
204 config
. liveclients
--;
205 createMissingClients ( c
);
206 config
. liveclients
++;
211 /* Read a length from the buffer pointed to by *p, store the length in *len,
212 * and return the number of bytes that the cursor advanced. */
213 static int readLen ( char * p
, int * len
) {
214 char * tail
= strstr ( p
, " \r\n " );
222 static void readHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
)
225 int nread
, pos
= 0 , len
= 0 ;
231 nread
= read ( c
-> fd
, buf
, sizeof ( buf
));
233 fprintf ( stderr
, "Reading from socket: %s \n " , strerror ( errno
));
238 fprintf ( stderr
, "EOF from client \n " );
242 c
-> totreceived
+= nread
;
243 c
-> ibuf
= sdscatlen ( c
-> ibuf
, buf
, nread
);
244 len
= sdslen ( c
-> ibuf
);
246 if ( c
-> replytype
== REPLY_INT
||
247 c
-> replytype
== REPLY_RETCODE
)
249 /* Check if the first line is complete. This is everything we need
250 * when waiting for an integer or status code reply.*/
251 if (( p
= strstr ( c
-> ibuf
, " \r\n " )) != NULL
)
253 } else if ( c
-> replytype
== REPLY_BULK
) {
255 if ( c
-> readlen
< 0 ) {
256 advance
= readLen ( c
-> ibuf
+ pos
,& c
-> readlen
);
259 if ( c
-> readlen
== - 1 ) {
262 /* include the trailing \r\n */
271 if ( c
-> readlen
> 0 ) {
272 canconsume
= c
-> readlen
> ( len
- pos
) ? ( len
- pos
) : c
-> readlen
;
273 c
-> readlen
-= canconsume
;
279 } else if ( c
-> replytype
== REPLY_MBULK
) {
281 if ( c
-> mbulk
== - 1 ) {
282 advance
= readLen ( c
-> ibuf
+ pos
,& c
-> mbulk
);
293 while ( c
-> mbulk
> 0 && pos
< len
) {
294 if ( c
-> readlen
> 0 ) {
295 canconsume
= c
-> readlen
> ( len
- pos
) ? ( len
- pos
) : c
-> readlen
;
296 c
-> readlen
-= canconsume
;
301 advance
= readLen ( c
-> ibuf
+ pos
,& c
-> readlen
);
304 if ( c
-> readlen
== - 1 ) {
308 /* include the trailing \r\n */
322 c
-> ibuf
= sdsrange ( c
-> ibuf
, pos
,- 1 );
329 static void writeHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
)
336 if ( c
-> state
== CLIENT_CONNECTING
) {
337 c
-> state
= CLIENT_SENDQUERY
;
340 if ( sdslen ( c
-> obuf
) > c
-> written
) {
341 void * ptr
= c
-> obuf
+ c
-> written
;
342 int len
= sdslen ( c
-> obuf
) - c
-> written
;
343 int nwritten
= write ( c
-> fd
, ptr
, len
);
344 if ( nwritten
== - 1 ) {
346 fprintf ( stderr
, "Writing to socket: %s \n " , strerror ( errno
));
350 c
-> written
+= nwritten
;
351 if ( sdslen ( c
-> obuf
) == c
-> written
) {
352 aeDeleteFileEvent ( config
. el
, c
-> fd
, AE_WRITABLE
);
353 aeCreateFileEvent ( config
. el
, c
-> fd
, AE_READABLE
, readHandler
, c
);
354 c
-> state
= CLIENT_READREPLY
;
359 static client
createClient ( void ) {
360 client c
= zmalloc ( sizeof ( struct _client
));
361 char err
[ ANET_ERR_LEN
];
363 if ( config
. hostsocket
== NULL
)
364 c
-> fd
= anetTcpNonBlockConnect ( err
, config
. hostip
, config
. hostport
);
366 c
-> fd
= anetUnixNonBlockConnect ( err
, config
. hostsocket
);
368 if ( c
-> fd
== ANET_ERR
) {
370 fprintf ( stderr
, "Connect: %s \n " , err
);
373 anetTcpNoDelay ( NULL
, c
-> fd
);
374 c
-> obuf
= sdsempty ();
375 c
-> ibuf
= sdsempty ();
380 c
-> state
= CLIENT_CONNECTING
;
381 aeCreateFileEvent ( config
. el
, c
-> fd
, AE_WRITABLE
, writeHandler
, c
);
382 config
. liveclients
++;
383 listAddNodeTail ( config
. clients
, c
);
387 static void createMissingClients ( client c
) {
388 while ( config
. liveclients
< config
. numclients
) {
389 client
new = createClient ();
392 new -> obuf
= sdsdup ( c
-> obuf
);
393 if ( config
. randomkeys
) randomizeClientKey ( c
);
394 prepareClientForReply ( new , c
-> replytype
);
398 static void showLatencyReport ( void ) {
400 float perc
, reqpersec
;
402 reqpersec
= ( float ) config
. donerequests
/(( float ) config
. totlatency
/ 1000 );
404 printf ( "====== %s ====== \n " , config
. title
);
405 printf ( " %d requests completed in %.2f seconds \n " , config
. donerequests
,
406 ( float ) config
. totlatency
/ 1000 );
407 printf ( " %d parallel clients \n " , config
. numclients
);
408 printf ( " %d bytes payload \n " , config
. datasize
);
409 printf ( " keep alive: %d \n " , config
. keepalive
);
411 for ( j
= 0 ; j
<= MAX_LATENCY
; j
++) {
412 if ( config
. latency
[ j
]) {
413 seen
+= config
. latency
[ j
];
414 perc
= (( float ) seen
* 100 )/ config
. donerequests
;
415 printf ( "%.2f%% <= %d milliseconds \n " , perc
, j
);
418 printf ( "%.2f requests per second \n\n " , reqpersec
);
420 printf ( " %s : %.2f requests per second \n " , config
. title
, reqpersec
);
424 static void prepareForBenchmark ( char * title
) {
425 memset ( config
. latency
, 0 , sizeof ( int )*( MAX_LATENCY
+ 1 ));
426 config
. title
= title
;
427 config
. start
= mstime ();
428 config
. donerequests
= 0 ;
431 static void endBenchmark ( void ) {
432 config
. totlatency
= mstime ()- config
. start
;
437 void parseOptions ( int argc
, char ** argv
) {
440 for ( i
= 1 ; i
< argc
; i
++) {
441 int lastarg
= i
== argc
- 1 ;
443 if (! strcmp ( argv
[ i
], "-c" ) && ! lastarg
) {
444 config
. numclients
= atoi ( argv
[ i
+ 1 ]);
446 } else if (! strcmp ( argv
[ i
], "-n" ) && ! lastarg
) {
447 config
. requests
= atoi ( argv
[ i
+ 1 ]);
449 } else if (! strcmp ( argv
[ i
], "-k" ) && ! lastarg
) {
450 config
. keepalive
= atoi ( argv
[ i
+ 1 ]);
452 } else if (! strcmp ( argv
[ i
], "-h" ) && ! lastarg
) {
453 char * ip
= zmalloc ( 32 );
454 if ( anetResolve ( NULL
, argv
[ i
+ 1 ], ip
) == ANET_ERR
) {
455 printf ( "Can't resolve %s \n " , argv
[ i
]);
460 } else if (! strcmp ( argv
[ i
], "-p" ) && ! lastarg
) {
461 config
. hostport
= atoi ( argv
[ i
+ 1 ]);
463 } else if (! strcmp ( argv
[ i
], "-s" ) && ! lastarg
) {
464 config
. hostsocket
= argv
[ i
+ 1 ];
466 } else if (! strcmp ( argv
[ i
], "-d" ) && ! lastarg
) {
467 config
. datasize
= atoi ( argv
[ i
+ 1 ]);
469 if ( config
. datasize
< 1 ) config
. datasize
= 1 ;
470 if ( config
. datasize
> 1024 * 1024 ) config
. datasize
= 1024 * 1024 ;
471 } else if (! strcmp ( argv
[ i
], "-r" ) && ! lastarg
) {
472 config
. randomkeys
= 1 ;
473 config
. randomkeys_keyspacelen
= atoi ( argv
[ i
+ 1 ]);
474 if ( config
. randomkeys_keyspacelen
< 0 )
475 config
. randomkeys_keyspacelen
= 0 ;
477 } else if (! strcmp ( argv
[ i
], "-q" )) {
479 } else if (! strcmp ( argv
[ i
], "-l" )) {
481 } else if (! strcmp ( argv
[ i
], "-D" )) {
483 } else if (! strcmp ( argv
[ i
], "-I" )) {
486 printf ( "Wrong option ' %s ' or option argument missing \n\n " , argv
[ i
]);
487 printf ( "Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>] \n\n " );
488 printf ( " -h <hostname> Server hostname (default 127.0.0.1) \n " );
489 printf ( " -p <port> Server port (default 6379) \n " );
490 printf ( " -s <socket> Server socket (overrides host and port) \n " );
491 printf ( " -c <clients> Number of parallel connections (default 50) \n " );
492 printf ( " -n <requests> Total number of requests (default 10000) \n " );
493 printf ( " -d <size> Data size of SET/GET value in bytes (default 2) \n " );
494 printf ( " -k <boolean> 1=keep alive 0=reconnect (default 1) \n " );
495 printf ( " -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD \n " );
496 printf ( " Using this option the benchmark will get/set keys \n " );
497 printf ( " in the form mykey_rand000000012456 instead of constant \n " );
498 printf ( " keys, the <keyspacelen> argument determines the max \n " );
499 printf ( " number of values for the random number. For instance \n " );
500 printf ( " if set to 10 only rand000000000000 - rand000000000009 \n " );
501 printf ( " range will be allowed. \n " );
502 printf ( " -q Quiet. Just show query/sec values \n " );
503 printf ( " -l Loop. Run the tests forever \n " );
504 printf ( " -I Idle mode. Just open N idle connections and wait. \n " );
505 printf ( " -D Debug mode. more verbose. \n " );
511 int showThroughput ( struct aeEventLoop
* eventLoop
, long long id
, void * clientData
) {
512 REDIS_NOTUSED ( eventLoop
);
514 REDIS_NOTUSED ( clientData
);
516 float dt
= ( float )( mstime ()- config
. start
)/ 1000.0 ;
517 float rps
= ( float ) config
. donerequests
/ dt
;
518 printf ( " %s : %.2f \r " , config
. title
, rps
);
520 return 250 ; /* every 250ms */
523 int main ( int argc
, char ** argv
) {
526 signal ( SIGHUP
, SIG_IGN
);
527 signal ( SIGPIPE
, SIG_IGN
);
530 config
. numclients
= 50 ;
531 config
. requests
= 10000 ;
532 config
. liveclients
= 0 ;
533 config
. el
= aeCreateEventLoop ();
534 aeCreateTimeEvent ( config
. el
, 1 , showThroughput
, NULL
, NULL
);
535 config
. keepalive
= 1 ;
536 config
. donerequests
= 0 ;
538 config
. randomkeys
= 0 ;
539 config
. randomkeys_keyspacelen
= 0 ;
543 config
. latency
= NULL
;
544 config
. clients
= listCreate ();
545 config
. latency
= zmalloc ( sizeof ( int )*( MAX_LATENCY
+ 1 ));
547 config
. hostip
= "127.0.0.1" ;
548 config
. hostport
= 6379 ;
549 config
. hostsocket
= NULL
;
551 parseOptions ( argc
, argv
);
553 if ( config
. keepalive
== 0 ) {
554 printf ( "WARNING: keepalive disabled, you probably need 'echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse' for Linux and 'sudo sysctl -w net.inet.tcp.msl=1000' for Mac OS X in order to use a lot of clients/requests \n " );
557 if ( config
. idlemode
) {
558 printf ( "Creating %d idle connections and waiting forever (Ctrl+C when done) \n " , config
. numclients
);
559 prepareForBenchmark ( "IDLE" );
562 c
-> obuf
= sdsempty ();
563 prepareClientForReply ( c
, REPLY_RETCODE
); /* will never receive it */
564 createMissingClients ( c
);
566 /* and will wait for every */
570 prepareForBenchmark ( "PING" );
573 c
-> obuf
= sdscat ( c
-> obuf
, "PING \r\n " );
574 prepareClientForReply ( c
, REPLY_RETCODE
);
575 createMissingClients ( c
);
579 prepareForBenchmark ( "PING (multi bulk)" );
582 c
-> obuf
= sdscat ( c
-> obuf
, "*1 \r\n $4 \r\n PING \r\n " );
583 prepareClientForReply ( c
, REPLY_RETCODE
);
584 createMissingClients ( c
);
588 prepareForBenchmark ( "MSET (10 keys, multi bulk)" );
591 c
-> obuf
= sdscatprintf ( c
-> obuf
, "* %d \r\n $4 \r\n MSET \r\n " , 11 );
594 char * data
= zmalloc ( config
. datasize
+ 2 );
595 memset ( data
, 'x' , config
. datasize
);
596 for ( i
= 0 ; i
< 10 ; i
++) {
597 c
-> obuf
= sdscatprintf ( c
-> obuf
, "$ %d \r\n %s \r\n " , config
. datasize
, data
);
601 prepareClientForReply ( c
, REPLY_RETCODE
);
602 createMissingClients ( c
);
606 prepareForBenchmark ( "SET" );
609 c
-> obuf
= sdscat ( c
-> obuf
, "SET foo_rand000000000000 " );
611 char * data
= zmalloc ( config
. datasize
+ 2 );
612 memset ( data
, 'x' , config
. datasize
);
613 data
[ config
. datasize
] = ' \r ' ;
614 data
[ config
. datasize
+ 1 ] = ' \n ' ;
615 c
-> obuf
= sdscatlen ( c
-> obuf
, data
, config
. datasize
+ 2 );
617 prepareClientForReply ( c
, REPLY_RETCODE
);
618 createMissingClients ( c
);
622 prepareForBenchmark ( "GET" );
625 c
-> obuf
= sdscat ( c
-> obuf
, "GET foo_rand000000000000 \r\n " );
626 prepareClientForReply ( c
, REPLY_BULK
);
627 createMissingClients ( c
);
631 prepareForBenchmark ( "INCR" );
634 c
-> obuf
= sdscat ( c
-> obuf
, "INCR counter_rand000000000000 \r\n " );
635 prepareClientForReply ( c
, REPLY_INT
);
636 createMissingClients ( c
);
640 prepareForBenchmark ( "LPUSH" );
643 c
-> obuf
= sdscat ( c
-> obuf
, "LPUSH mylist bar \r\n " );
644 prepareClientForReply ( c
, REPLY_INT
);
645 createMissingClients ( c
);
649 prepareForBenchmark ( "LPOP" );
652 c
-> obuf
= sdscat ( c
-> obuf
, "LPOP mylist \r\n " );
653 prepareClientForReply ( c
, REPLY_BULK
);
654 createMissingClients ( c
);
658 prepareForBenchmark ( "SADD" );
661 c
-> obuf
= sdscat ( c
-> obuf
, "SADD myset counter_rand000000000000 \r\n " );
662 prepareClientForReply ( c
, REPLY_RETCODE
);
663 createMissingClients ( c
);
667 prepareForBenchmark ( "SPOP" );
670 c
-> obuf
= sdscat ( c
-> obuf
, "SPOP myset \r\n " );
671 prepareClientForReply ( c
, REPLY_BULK
);
672 createMissingClients ( c
);
676 prepareForBenchmark ( "LPUSH (again, in order to bench LRANGE)" );
679 c
-> obuf
= sdscat ( c
-> obuf
, "LPUSH mylist bar \r\n " );
680 prepareClientForReply ( c
, REPLY_RETCODE
);
681 createMissingClients ( c
);
685 prepareForBenchmark ( "LRANGE (first 100 elements)" );
688 c
-> obuf
= sdscat ( c
-> obuf
, "LRANGE mylist 0 99 \r\n " );
689 prepareClientForReply ( c
, REPLY_MBULK
);
690 createMissingClients ( c
);
694 prepareForBenchmark ( "LRANGE (first 300 elements)" );
697 c
-> obuf
= sdscat ( c
-> obuf
, "LRANGE mylist 0 299 \r\n " );
698 prepareClientForReply ( c
, REPLY_MBULK
);
699 createMissingClients ( c
);
703 prepareForBenchmark ( "LRANGE (first 450 elements)" );
706 c
-> obuf
= sdscat ( c
-> obuf
, "LRANGE mylist 0 449 \r\n " );
707 prepareClientForReply ( c
, REPLY_MBULK
);
708 createMissingClients ( c
);
712 prepareForBenchmark ( "LRANGE (first 600 elements)" );
715 c
-> obuf
= sdscat ( c
-> obuf
, "LRANGE mylist 0 599 \r\n " );
716 prepareClientForReply ( c
, REPLY_MBULK
);
717 createMissingClients ( c
);
722 } while ( config
. loop
);