]>
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.
48 #define CLIENT_CONNECTING 0
49 #define CLIENT_SENDQUERY 1
50 #define CLIENT_READREPLY 2
52 #define REDIS_NOTUSED(V) ((void) V)
54 static struct config
{
63 int randomkeys_keyspacelen
;
79 typedef struct _client
{
80 redisContext
* context
;
84 unsigned int written
; /* bytes of 'obuf' already written */
86 long long start
; /* start time of a request */
87 long long latency
; /* request latency */
91 static void writeHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
);
92 static void createMissingClients ( client c
);
95 static long long ustime ( void ) {
99 gettimeofday (& tv
, NULL
);
100 ust
= (( long ) tv
. tv_sec
)* 1000000 ;
105 static long long mstime ( void ) {
109 gettimeofday (& tv
, NULL
);
110 mst
= (( long ) tv
. tv_sec
)* 1000 ;
111 mst
+= tv
. tv_usec
/ 1000 ;
115 static void freeClient ( client c
) {
117 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
);
118 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_READABLE
);
119 redisFree ( c
-> context
);
122 config
. liveclients
--;
123 ln
= listSearchKey ( config
. clients
, c
);
125 listDelNode ( config
. clients
, ln
);
128 static void freeAllClients ( void ) {
129 listNode
* ln
= config
. clients
-> head
, * next
;
133 freeClient ( ln
-> value
);
138 static void resetClient ( client c
) {
139 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
);
140 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_READABLE
);
141 aeCreateFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
, writeHandler
, c
);
143 c
-> state
= CLIENT_SENDQUERY
;
148 static void randomizeClientKey ( client c
) {
153 if ( c
-> randptr
== NULL
) return ;
155 /* Check if we have to randomize (only once per connection) */
156 if ( c
-> randptr
== ( void *)- 1 ) {
157 p
= strstr ( c
-> obuf
, ":rand:" );
162 newline
= strstr ( p
, " \r\n " );
163 assert ( newline
-( p
+ 6 ) == 12 ); /* 12 chars for randomness */
168 /* Set random number in output buffer */
169 r
= random () % config
. randomkeys_keyspacelen
;
170 snprintf ( buf
, sizeof ( buf
), " %0 12ld" , r
);
171 memcpy ( c
-> randptr
, buf
, 12 );
174 static void clientDone ( client c
) {
175 if ( config
. donerequests
== config
. requests
) {
180 if ( config
. keepalive
) {
182 if ( config
. randomkeys
) randomizeClientKey ( c
);
184 config
. liveclients
--;
185 createMissingClients ( c
);
186 config
. liveclients
++;
191 static void readHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
) {
198 /* Calculate latency only for the first read event. This means that the
199 * server already sent the reply and we need to parse it. Parsing overhead
200 * is not part of the latency, so calculate it only once, here. */
201 if ( c
-> latency
< 0 ) c
-> latency
= ustime ()-( c
-> start
);
203 if ( redisBufferRead ( c
-> context
) != REDIS_OK
) {
204 fprintf ( stderr
, "Error: %s \n " , c
-> context
-> errstr
);
207 if ( redisGetReply ( c
-> context
,& reply
) != REDIS_OK
) {
208 fprintf ( stderr
, "Error: %s \n " , c
-> context
-> errstr
);
212 if ( reply
== ( void *) REDIS_REPLY_ERROR
) {
213 fprintf ( stderr
, "Unexpected error reply, exiting... \n " );
217 if ( config
. donerequests
< config
. requests
)
218 config
. latency
[ config
. donerequests
++] = c
-> latency
;
224 static void writeHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
) {
230 if ( c
-> state
== CLIENT_CONNECTING
) {
231 c
-> state
= CLIENT_SENDQUERY
;
235 if ( sdslen ( c
-> obuf
) > c
-> written
) {
236 void * ptr
= c
-> obuf
+ c
-> written
;
237 int nwritten
= write ( c
-> context
-> fd
, ptr
, sdslen ( c
-> obuf
)- c
-> written
);
238 if ( nwritten
== - 1 ) {
240 fprintf ( stderr
, "Writing to socket: %s \n " , strerror ( errno
));
244 c
-> written
+= nwritten
;
245 if ( sdslen ( c
-> obuf
) == c
-> written
) {
246 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
);
247 aeCreateFileEvent ( config
. el
, c
-> context
-> fd
, AE_READABLE
, readHandler
, c
);
248 c
-> state
= CLIENT_READREPLY
;
253 static client
createClient ( int replytype
) {
254 client c
= zmalloc ( sizeof ( struct _client
));
255 if ( config
. hostsocket
== NULL
) {
256 c
-> context
= redisConnectNonBlock ( config
. hostip
, config
. hostport
);
258 c
-> context
= redisConnectUnixNonBlock ( config
. hostsocket
);
260 if ( c
-> context
-> err
) {
261 fprintf ( stderr
, "Could not connect to Redis at " );
262 if ( config
. hostsocket
== NULL
)
263 fprintf ( stderr
, " %s : %d : %s \n " , config
. hostip
, config
. hostport
, c
-> context
-> errstr
);
265 fprintf ( stderr
, " %s : %s \n " , config
. hostsocket
, c
-> context
-> errstr
);
268 c
-> replytype
= replytype
;
269 c
-> state
= CLIENT_CONNECTING
;
271 c
-> randptr
= ( void *)- 1 ;
273 redisSetReplyObjectFunctions ( c
-> context
, NULL
);
274 aeCreateFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
, writeHandler
, c
);
275 listAddNodeTail ( config
. clients
, c
);
276 config
. liveclients
++;
280 static void createMissingClients ( client c
) {
283 while ( config
. liveclients
< config
. numclients
) {
284 client
new = createClient ( c
-> replytype
);
285 new -> obuf
= sdsdup ( c
-> obuf
);
286 if ( config
. randomkeys
) randomizeClientKey ( c
);
288 /* Listen backlog is quite limited on most systems */
295 /* Start the timer once the connection are established */
296 config
. start
= mstime ();
299 static int compareLatency ( const void * a
, const void * b
) {
300 return (*( long long *) a
)-(*( long long *) b
);
303 static void showLatencyReport ( void ) {
305 float perc
, reqpersec
;
307 reqpersec
= ( float ) config
. donerequests
/(( float ) config
. totlatency
/ 1000 );
309 printf ( "====== %s ====== \n " , config
. title
);
310 printf ( " %d requests completed in %.2f seconds \n " , config
. donerequests
,
311 ( float ) config
. totlatency
/ 1000 );
312 printf ( " %d parallel clients \n " , config
. numclients
);
313 printf ( " %d bytes payload \n " , config
. datasize
);
314 printf ( " keep alive: %d \n " , config
. keepalive
);
317 qsort ( config
. latency
, config
. requests
, sizeof ( long long ), compareLatency
);
318 for ( i
= 0 ; i
< config
. requests
; i
++) {
319 if ( config
. latency
[ i
]/ 1000 != curlat
|| i
== ( config
. requests
- 1 )) {
320 curlat
= config
. latency
[ i
]/ 1000 ;
321 perc
= (( float )( i
+ 1 )* 100 )/ config
. requests
;
322 printf ( "%.2f%% <= %d milliseconds \n " , perc
, curlat
);
325 printf ( "%.2f requests per second \n\n " , reqpersec
);
327 printf ( " %s : %.2f requests per second \n " , config
. title
, reqpersec
);
331 static void prepareForBenchmark ( char * title
) {
332 config
. title
= title
;
333 config
. start
= mstime ();
334 config
. donerequests
= 0 ;
337 static void endBenchmark ( void ) {
338 config
. totlatency
= mstime ()- config
. start
;
343 void parseOptions ( int argc
, char ** argv
) {
346 for ( i
= 1 ; i
< argc
; i
++) {
347 int lastarg
= i
== argc
- 1 ;
349 if (! strcmp ( argv
[ i
], "-c" ) && ! lastarg
) {
350 config
. numclients
= atoi ( argv
[ i
+ 1 ]);
352 } else if (! strcmp ( argv
[ i
], "-n" ) && ! lastarg
) {
353 config
. requests
= atoi ( argv
[ i
+ 1 ]);
355 } else if (! strcmp ( argv
[ i
], "-k" ) && ! lastarg
) {
356 config
. keepalive
= atoi ( argv
[ i
+ 1 ]);
358 } else if (! strcmp ( argv
[ i
], "-h" ) && ! lastarg
) {
359 config
. hostip
= argv
[ i
+ 1 ];
361 } else if (! strcmp ( argv
[ i
], "-p" ) && ! lastarg
) {
362 config
. hostport
= atoi ( argv
[ i
+ 1 ]);
364 } else if (! strcmp ( argv
[ i
], "-s" ) && ! lastarg
) {
365 config
. hostsocket
= argv
[ i
+ 1 ];
367 } else if (! strcmp ( argv
[ i
], "-d" ) && ! lastarg
) {
368 config
. datasize
= atoi ( argv
[ i
+ 1 ]);
370 if ( config
. datasize
< 1 ) config
. datasize
= 1 ;
371 if ( config
. datasize
> 1024 * 1024 ) config
. datasize
= 1024 * 1024 ;
372 } else if (! strcmp ( argv
[ i
], "-r" ) && ! lastarg
) {
373 config
. randomkeys
= 1 ;
374 config
. randomkeys_keyspacelen
= atoi ( argv
[ i
+ 1 ]);
375 if ( config
. randomkeys_keyspacelen
< 0 )
376 config
. randomkeys_keyspacelen
= 0 ;
378 } else if (! strcmp ( argv
[ i
], "-q" )) {
380 } else if (! strcmp ( argv
[ i
], "-l" )) {
382 } else if (! strcmp ( argv
[ i
], "-D" )) {
384 } else if (! strcmp ( argv
[ i
], "-I" )) {
387 printf ( "Wrong option ' %s ' or option argument missing \n\n " , argv
[ i
]);
388 printf ( "Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>] \n\n " );
389 printf ( " -h <hostname> Server hostname (default 127.0.0.1) \n " );
390 printf ( " -p <port> Server port (default 6379) \n " );
391 printf ( " -s <socket> Server socket (overrides host and port) \n " );
392 printf ( " -c <clients> Number of parallel connections (default 50) \n " );
393 printf ( " -n <requests> Total number of requests (default 10000) \n " );
394 printf ( " -d <size> Data size of SET/GET value in bytes (default 2) \n " );
395 printf ( " -k <boolean> 1=keep alive 0=reconnect (default 1) \n " );
396 printf ( " -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD \n " );
397 printf ( " Using this option the benchmark will get/set keys \n " );
398 printf ( " in the form mykey_rand000000012456 instead of constant \n " );
399 printf ( " keys, the <keyspacelen> argument determines the max \n " );
400 printf ( " number of values for the random number. For instance \n " );
401 printf ( " if set to 10 only rand000000000000 - rand000000000009 \n " );
402 printf ( " range will be allowed. \n " );
403 printf ( " -q Quiet. Just show query/sec values \n " );
404 printf ( " -l Loop. Run the tests forever \n " );
405 printf ( " -I Idle mode. Just open N idle connections and wait. \n " );
406 printf ( " -D Debug mode. more verbose. \n " );
412 int showThroughput ( struct aeEventLoop
* eventLoop
, long long id
, void * clientData
) {
413 REDIS_NOTUSED ( eventLoop
);
415 REDIS_NOTUSED ( clientData
);
417 float dt
= ( float )( mstime ()- config
. start
)/ 1000.0 ;
418 float rps
= ( float ) config
. donerequests
/ dt
;
419 printf ( " %s : %.2f \r " , config
. title
, rps
);
421 return 250 ; /* every 250ms */
424 int main ( int argc
, char ** argv
) {
428 signal ( SIGHUP
, SIG_IGN
);
429 signal ( SIGPIPE
, SIG_IGN
);
432 config
. numclients
= 50 ;
433 config
. requests
= 10000 ;
434 config
. liveclients
= 0 ;
435 config
. el
= aeCreateEventLoop ();
436 aeCreateTimeEvent ( config
. el
, 1 , showThroughput
, NULL
, NULL
);
437 config
. keepalive
= 1 ;
438 config
. donerequests
= 0 ;
440 config
. randomkeys
= 0 ;
441 config
. randomkeys_keyspacelen
= 0 ;
445 config
. latency
= NULL
;
446 config
. clients
= listCreate ();
447 config
. hostip
= "127.0.0.1" ;
448 config
. hostport
= 6379 ;
449 config
. hostsocket
= NULL
;
451 parseOptions ( argc
, argv
);
452 config
. latency
= zmalloc ( sizeof ( long long )* config
. requests
);
454 if ( config
. keepalive
== 0 ) {
455 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 " );
458 if ( config
. idlemode
) {
459 printf ( "Creating %d idle connections and waiting forever (Ctrl+C when done) \n " , config
. numclients
);
460 prepareForBenchmark ( "IDLE" );
461 c
= createClient ( 0 ); /* will never receive a reply */
462 c
-> obuf
= sdsempty ();
463 createMissingClients ( c
);
465 /* and will wait for every */
472 data
= zmalloc ( config
. datasize
+ 1 );
473 memset ( data
, 'x' , config
. datasize
);
474 data
[ config
. datasize
] = '\0' ;
476 prepareForBenchmark ( "PING (inline)" );
477 c
= createClient ( REDIS_REPLY_STATUS
);
478 c
-> obuf
= sdscat ( sdsempty (), "PING \r\n " );
479 createMissingClients ( c
);
483 prepareForBenchmark ( "PING" );
484 c
= createClient ( REDIS_REPLY_STATUS
);
485 len
= redisFormatCommand (& cmd
, "PING" );
486 c
-> obuf
= sdsnewlen ( cmd
, len
);
488 createMissingClients ( c
);
492 prepareForBenchmark ( "MSET (10 keys)" );
493 c
= createClient ( REDIS_REPLY_ARRAY
);
495 const char * argv
[ 11 ];
497 for ( i
= 1 ; i
< 11 ; i
++)
499 len
= redisFormatCommandArgv (& cmd
, 11 , argv
, NULL
);
500 c
-> obuf
= sdsnewlen ( cmd
, len
);
503 createMissingClients ( c
);
507 prepareForBenchmark ( "SET" );
508 c
= createClient ( REDIS_REPLY_STATUS
);
509 len
= redisFormatCommand (& cmd
, "SET foo:rand:000000000000 %s " , data
);
510 c
-> obuf
= sdsnewlen ( cmd
, len
);
512 createMissingClients ( c
);
516 prepareForBenchmark ( "GET" );
517 c
= createClient ( REDIS_REPLY_STRING
);
518 len
= redisFormatCommand (& cmd
, "GET foo:rand:000000000000" );
519 c
-> obuf
= sdsnewlen ( cmd
, len
);
521 createMissingClients ( c
);
525 prepareForBenchmark ( "INCR" );
526 c
= createClient ( REDIS_REPLY_INTEGER
);
527 len
= redisFormatCommand (& cmd
, "INCR counter:rand:000000000000" );
528 c
-> obuf
= sdsnewlen ( cmd
, len
);
530 createMissingClients ( c
);
534 prepareForBenchmark ( "LPUSH" );
535 c
= createClient ( REDIS_REPLY_INTEGER
);
536 len
= redisFormatCommand (& cmd
, "LPUSH mylist %s " , data
);
537 c
-> obuf
= sdsnewlen ( cmd
, len
);
539 createMissingClients ( c
);
543 prepareForBenchmark ( "LPOP" );
544 c
= createClient ( REDIS_REPLY_STRING
);
545 len
= redisFormatCommand (& cmd
, "LPOP mylist" );
546 c
-> obuf
= sdsnewlen ( cmd
, len
);
548 createMissingClients ( c
);
552 prepareForBenchmark ( "SADD" );
553 c
= createClient ( REDIS_REPLY_STATUS
);
554 len
= redisFormatCommand (& cmd
, "SADD myset counter:rand:000000000000" );
555 c
-> obuf
= sdsnewlen ( cmd
, len
);
557 createMissingClients ( c
);
561 prepareForBenchmark ( "SPOP" );
562 c
= createClient ( REDIS_REPLY_STRING
);
563 len
= redisFormatCommand (& cmd
, "SPOP myset" );
564 c
-> obuf
= sdsnewlen ( cmd
, len
);
566 createMissingClients ( c
);
570 prepareForBenchmark ( "LPUSH (again, in order to bench LRANGE)" );
571 c
= createClient ( REDIS_REPLY_STATUS
);
572 len
= redisFormatCommand (& cmd
, "LPUSH mylist %s " , data
);
573 c
-> obuf
= sdsnewlen ( cmd
, len
);
575 createMissingClients ( c
);
579 prepareForBenchmark ( "LRANGE (first 100 elements)" );
580 c
= createClient ( REDIS_REPLY_ARRAY
);
581 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 99" );
582 c
-> obuf
= sdsnewlen ( cmd
, len
);
584 createMissingClients ( c
);
588 prepareForBenchmark ( "LRANGE (first 300 elements)" );
589 c
= createClient ( REDIS_REPLY_ARRAY
);
590 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 299" );
591 c
-> obuf
= sdsnewlen ( cmd
, len
);
593 createMissingClients ( c
);
597 prepareForBenchmark ( "LRANGE (first 450 elements)" );
598 c
= createClient ( REDIS_REPLY_ARRAY
);
599 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 449" );
600 c
-> obuf
= sdsnewlen ( cmd
, len
);
602 createMissingClients ( c
);
606 prepareForBenchmark ( "LRANGE (first 600 elements)" );
607 c
= createClient ( REDIS_REPLY_ARRAY
);
608 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 599" );
609 c
-> obuf
= sdsnewlen ( cmd
, len
);
611 createMissingClients ( c
);
616 } while ( config
. loop
);