]>
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 REDIS_NOTUSED(V) ((void) V)
50 static struct config
{
54 const char * hostsocket
;
59 int requests_finished
;
63 int randomkeys_keyspacelen
;
76 typedef struct _client
{
77 redisContext
* context
;
79 char * randptr
[ 10 ]; /* needed for MSET against 10 keys */
81 unsigned int written
; /* bytes of 'obuf' already written */
82 long long start
; /* start time of a request */
83 long long latency
; /* request latency */
87 static void writeHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
);
88 static void createMissingClients ( client c
);
91 static long long ustime ( void ) {
95 gettimeofday (& tv
, NULL
);
96 ust
= (( long ) tv
. tv_sec
)* 1000000 ;
101 static long long mstime ( void ) {
105 gettimeofday (& tv
, NULL
);
106 mst
= (( long ) tv
. tv_sec
)* 1000 ;
107 mst
+= tv
. tv_usec
/ 1000 ;
111 static void freeClient ( client c
) {
113 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
);
114 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_READABLE
);
115 redisFree ( c
-> context
);
118 config
. liveclients
--;
119 ln
= listSearchKey ( config
. clients
, c
);
121 listDelNode ( config
. clients
, ln
);
124 static void freeAllClients ( void ) {
125 listNode
* ln
= config
. clients
-> head
, * next
;
129 freeClient ( ln
-> value
);
134 static void resetClient ( client c
) {
135 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
);
136 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_READABLE
);
137 aeCreateFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
, writeHandler
, c
);
141 static void randomizeClientKey ( client c
) {
145 for ( i
= 0 ; i
< c
-> randlen
; i
++) {
146 r
= random () % config
. randomkeys_keyspacelen
;
147 snprintf ( buf
, sizeof ( buf
), " %0 12zu" , r
);
148 memcpy ( c
-> randptr
[ i
], buf
, 12 );
152 static void clientDone ( client c
) {
153 if ( config
. requests_finished
== config
. requests
) {
158 if ( config
. keepalive
) {
161 config
. liveclients
--;
162 createMissingClients ( c
);
163 config
. liveclients
++;
168 static void readHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
) {
175 /* Calculate latency only for the first read event. This means that the
176 * server already sent the reply and we need to parse it. Parsing overhead
177 * is not part of the latency, so calculate it only once, here. */
178 if ( c
-> latency
< 0 ) c
-> latency
= ustime ()-( c
-> start
);
180 if ( redisBufferRead ( c
-> context
) != REDIS_OK
) {
181 fprintf ( stderr
, "Error: %s \n " , c
-> context
-> errstr
);
184 if ( redisGetReply ( c
-> context
,& reply
) != REDIS_OK
) {
185 fprintf ( stderr
, "Error: %s \n " , c
-> context
-> errstr
);
189 if ( reply
== ( void *) REDIS_REPLY_ERROR
) {
190 fprintf ( stderr
, "Unexpected error reply, exiting... \n " );
194 if ( config
. requests_finished
< config
. requests
)
195 config
. latency
[ config
. requests_finished
++] = c
-> latency
;
201 static void writeHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
) {
207 /* Initialize request when nothing was written. */
208 if ( c
-> written
== 0 ) {
209 /* Enforce upper bound to number of requests. */
210 if ( config
. requests_issued
++ >= config
. requests
) {
215 /* Really initialize: randomize keys and set start time. */
216 if ( config
. randomkeys
) randomizeClientKey ( c
);
221 if ( sdslen ( c
-> obuf
) > c
-> written
) {
222 void * ptr
= c
-> obuf
+ c
-> written
;
223 int nwritten
= write ( c
-> context
-> fd
, ptr
, sdslen ( c
-> obuf
)- c
-> written
);
224 if ( nwritten
== - 1 ) {
226 fprintf ( stderr
, "Writing to socket: %s \n " , strerror ( errno
));
230 c
-> written
+= nwritten
;
231 if ( sdslen ( c
-> obuf
) == c
-> written
) {
232 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
);
233 aeCreateFileEvent ( config
. el
, c
-> context
-> fd
, AE_READABLE
, readHandler
, c
);
238 static client
createClient ( const char * cmd
, size_t len
) {
239 client c
= zmalloc ( sizeof ( struct _client
));
240 if ( config
. hostsocket
== NULL
) {
241 c
-> context
= redisConnectNonBlock ( config
. hostip
, config
. hostport
);
243 c
-> context
= redisConnectUnixNonBlock ( config
. hostsocket
);
245 if ( c
-> context
-> err
) {
246 fprintf ( stderr
, "Could not connect to Redis at " );
247 if ( config
. hostsocket
== NULL
)
248 fprintf ( stderr
, " %s : %d : %s \n " , config
. hostip
, config
. hostport
, c
-> context
-> errstr
);
250 fprintf ( stderr
, " %s : %s \n " , config
. hostsocket
, c
-> context
-> errstr
);
253 c
-> obuf
= sdsnewlen ( cmd
, len
);
257 /* Find substrings in the output buffer that need to be randomized. */
258 if ( config
. randomkeys
) {
259 char * p
= c
-> obuf
, * newline
;
260 while (( p
= strstr ( p
, ":rand:" )) != NULL
) {
261 newline
= strstr ( p
, " \r\n " );
262 assert ( newline
-( p
+ 6 ) == 12 ); /* 12 chars for randomness */
263 assert ( c
-> randlen
< ( signed )( sizeof ( c
-> randptr
)/ sizeof ( char *)));
264 c
-> randptr
[ c
-> randlen
++] = p
+ 6 ;
269 redisSetReplyObjectFunctions ( c
-> context
, NULL
);
270 aeCreateFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
, writeHandler
, c
);
271 listAddNodeTail ( config
. clients
, c
);
272 config
. liveclients
++;
276 static void createMissingClients ( client c
) {
279 while ( config
. liveclients
< config
. numclients
) {
280 createClient ( c
-> obuf
, sdslen ( c
-> obuf
));
282 /* Listen backlog is quite limited on most systems */
290 static int compareLatency ( const void * a
, const void * b
) {
291 return (*( long long *) a
)-(*( long long *) b
);
294 static void showLatencyReport ( void ) {
296 float perc
, reqpersec
;
298 reqpersec
= ( float ) config
. requests_finished
/(( float ) config
. totlatency
/ 1000 );
299 if (! config
. quiet
&& ! config
. csv
) {
300 printf ( "====== %s ====== \n " , config
. title
);
301 printf ( " %d requests completed in %.2f seconds \n " , config
. requests_finished
,
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
);
308 qsort ( config
. latency
, config
. requests
, sizeof ( long long ), compareLatency
);
309 for ( i
= 0 ; i
< config
. requests
; i
++) {
310 if ( config
. latency
[ i
]/ 1000 != curlat
|| i
== ( config
. requests
- 1 )) {
311 curlat
= config
. latency
[ i
]/ 1000 ;
312 perc
= (( float )( i
+ 1 )* 100 )/ config
. requests
;
313 printf ( "%.2f%% <= %d milliseconds \n " , perc
, curlat
);
316 printf ( "%.2f requests per second \n\n " , reqpersec
);
317 } else if ( config
. csv
) {
318 printf ( " \" %s \" , \" %.2f \"\n " , config
. title
, reqpersec
);
320 printf ( " %s : %.2f requests per second \n " , config
. title
, reqpersec
);
324 static void benchmark ( const char * title
, const char * cmd
, int len
) {
327 config
. title
= title
;
328 config
. requests_issued
= 0 ;
329 config
. requests_finished
= 0 ;
331 c
= createClient ( cmd
, len
);
332 createMissingClients ( c
);
334 config
. start
= mstime ();
336 config
. totlatency
= mstime ()- config
. start
;
342 /* Returns number of consumed options. */
343 int parseOptions ( int argc
, const char ** argv
) {
348 for ( i
= 1 ; i
< argc
; i
++) {
349 lastarg
= ( i
== ( argc
- 1 ));
351 if (! strcmp ( argv
[ i
], "-c" )) {
352 if ( lastarg
) goto invalid
;
353 config
. numclients
= atoi ( argv
[++ i
]);
354 } else if (! strcmp ( argv
[ i
], "-n" )) {
355 if ( lastarg
) goto invalid
;
356 config
. requests
= atoi ( argv
[++ i
]);
357 } else if (! strcmp ( argv
[ i
], "-k" )) {
358 if ( lastarg
) goto invalid
;
359 config
. keepalive
= atoi ( argv
[++ i
]);
360 } else if (! strcmp ( argv
[ i
], "-h" )) {
361 if ( lastarg
) goto invalid
;
362 config
. hostip
= strdup ( argv
[++ i
]);
363 } else if (! strcmp ( argv
[ i
], "-p" )) {
364 if ( lastarg
) goto invalid
;
365 config
. hostport
= atoi ( argv
[++ i
]);
366 } else if (! strcmp ( argv
[ i
], "-s" )) {
367 if ( lastarg
) goto invalid
;
368 config
. hostsocket
= strdup ( argv
[++ i
]);
369 } else if (! strcmp ( argv
[ i
], "-d" )) {
370 if ( lastarg
) goto invalid
;
371 config
. datasize
= atoi ( argv
[++ i
]);
372 if ( config
. datasize
< 1 ) config
. datasize
= 1 ;
373 if ( config
. datasize
> 1024 * 1024 * 1024 ) config
. datasize
= 1024 * 1024 * 1024 ;
374 } else if (! strcmp ( argv
[ i
], "-r" )) {
375 if ( lastarg
) goto invalid
;
376 config
. randomkeys
= 1 ;
377 config
. randomkeys_keyspacelen
= atoi ( argv
[++ i
]);
378 if ( config
. randomkeys_keyspacelen
< 0 )
379 config
. randomkeys_keyspacelen
= 0 ;
380 } else if (! strcmp ( argv
[ i
], "-q" )) {
382 } else if (! strcmp ( argv
[ i
], "--csv" )) {
384 } else if (! strcmp ( argv
[ i
], "-l" )) {
386 } else if (! strcmp ( argv
[ i
], "-I" )) {
388 } else if (! strcmp ( argv
[ i
], "--help" )) {
392 /* Assume the user meant to provide an option when the arg starts
393 * with a dash. We're done otherwise and should use the remainder
394 * as the command and arguments for running the benchmark. */
395 if ( argv
[ i
][ 0 ] == '-' ) goto invalid
;
403 printf ( "Invalid option \" %s \" or option argument missing \n\n " , argv
[ i
]);
406 printf ( "Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>] \n\n " );
407 printf ( " -h <hostname> Server hostname (default 127.0.0.1) \n " );
408 printf ( " -p <port> Server port (default 6379) \n " );
409 printf ( " -s <socket> Server socket (overrides host and port) \n " );
410 printf ( " -c <clients> Number of parallel connections (default 50) \n " );
411 printf ( " -n <requests> Total number of requests (default 10000) \n " );
412 printf ( " -d <size> Data size of SET/GET value in bytes (default 2) \n " );
413 printf ( " -k <boolean> 1=keep alive 0=reconnect (default 1) \n " );
414 printf ( " -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD \n " );
415 printf ( " Using this option the benchmark will get/set keys \n " );
416 printf ( " in the form mykey_rand000000012456 instead of constant \n " );
417 printf ( " keys, the <keyspacelen> argument determines the max \n " );
418 printf ( " number of values for the random number. For instance \n " );
419 printf ( " if set to 10 only rand000000000000 - rand000000000009 \n " );
420 printf ( " range will be allowed. \n " );
421 printf ( " -q Quiet. Just show query/sec values \n " );
422 printf ( " --csv Output in CSV format \n " );
423 printf ( " -l Loop. Run the tests forever \n " );
424 printf ( " -I Idle mode. Just open N idle connections and wait. \n " );
428 int showThroughput ( struct aeEventLoop
* eventLoop
, long long id
, void * clientData
) {
429 REDIS_NOTUSED ( eventLoop
);
431 REDIS_NOTUSED ( clientData
);
433 if ( config
. csv
) return 250 ;
434 float dt
= ( float )( mstime ()- config
. start
)/ 1000.0 ;
435 float rps
= ( float ) config
. requests_finished
/ dt
;
436 printf ( " %s : %.2f \r " , config
. title
, rps
);
438 return 250 ; /* every 250ms */
441 int main ( int argc
, const char ** argv
) {
448 signal ( SIGHUP
, SIG_IGN
);
449 signal ( SIGPIPE
, SIG_IGN
);
451 config
. numclients
= 50 ;
452 config
. requests
= 10000 ;
453 config
. liveclients
= 0 ;
454 config
. el
= aeCreateEventLoop ();
455 aeCreateTimeEvent ( config
. el
, 1 , showThroughput
, NULL
, NULL
);
456 config
. keepalive
= 1 ;
458 config
. randomkeys
= 0 ;
459 config
. randomkeys_keyspacelen
= 0 ;
464 config
. latency
= NULL
;
465 config
. clients
= listCreate ();
466 config
. hostip
= "127.0.0.1" ;
467 config
. hostport
= 6379 ;
468 config
. hostsocket
= NULL
;
470 i
= parseOptions ( argc
, argv
);
474 config
. latency
= zmalloc ( sizeof ( long long )* config
. requests
);
476 if ( config
. keepalive
== 0 ) {
477 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 " );
480 if ( config
. idlemode
) {
481 printf ( "Creating %d idle connections and waiting forever (Ctrl+C when done) \n " , config
. numclients
);
482 c
= createClient ( "" , 0 ); /* will never receive a reply */
483 createMissingClients ( c
);
485 /* and will wait for every */
488 /* Run benchmark with command in the remainder of the arguments. */
490 sds title
= sdsnew ( argv
[ 0 ]);
491 for ( i
= 1 ; i
< argc
; i
++) {
492 title
= sdscatlen ( title
, " " , 1 );
493 title
= sdscatlen ( title
, ( char *) argv
[ i
], strlen ( argv
[ i
]));
497 len
= redisFormatCommandArgv (& cmd
, argc
, argv
, NULL
);
498 benchmark ( title
, cmd
, len
);
500 } while ( config
. loop
);
505 /* Run default benchmark suite. */
507 data
= zmalloc ( config
. datasize
+ 1 );
508 memset ( data
, 'x' , config
. datasize
);
509 data
[ config
. datasize
] = '\0' ;
511 benchmark ( "PING (inline)" , "PING \r\n " , 6 );
513 len
= redisFormatCommand (& cmd
, "PING" );
514 benchmark ( "PING" , cmd
, len
);
517 const char * argv
[ 21 ];
519 for ( i
= 1 ; i
< 21 ; i
+= 2 ) {
520 argv
[ i
] = "foo:rand:000000000000" ;
523 len
= redisFormatCommandArgv (& cmd
, 21 , argv
, NULL
);
524 benchmark ( "MSET (10 keys)" , cmd
, len
);
527 len
= redisFormatCommand (& cmd
, "SET foo:rand:000000000000 %s " , data
);
528 benchmark ( "SET" , cmd
, len
);
531 len
= redisFormatCommand (& cmd
, "GET foo:rand:000000000000" );
532 benchmark ( "GET" , cmd
, len
);
535 len
= redisFormatCommand (& cmd
, "INCR counter:rand:000000000000" );
536 benchmark ( "INCR" , cmd
, len
);
539 len
= redisFormatCommand (& cmd
, "LPUSH mylist %s " , data
);
540 benchmark ( "LPUSH" , cmd
, len
);
543 len
= redisFormatCommand (& cmd
, "LPOP mylist" );
544 benchmark ( "LPOP" , cmd
, len
);
547 len
= redisFormatCommand (& cmd
, "SADD myset counter:rand:000000000000" );
548 benchmark ( "SADD" , cmd
, len
);
551 len
= redisFormatCommand (& cmd
, "SPOP myset" );
552 benchmark ( "SPOP" , cmd
, len
);
555 len
= redisFormatCommand (& cmd
, "LPUSH mylist %s " , data
);
556 benchmark ( "LPUSH (again, in order to bench LRANGE)" , cmd
, len
);
559 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 99" );
560 benchmark ( "LRANGE (first 100 elements)" , cmd
, len
);
563 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 299" );
564 benchmark ( "LRANGE (first 300 elements)" , cmd
, len
);
567 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 449" );
568 benchmark ( "LRANGE (first 450 elements)" , cmd
, len
);
571 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 599" );
572 benchmark ( "LRANGE (first 600 elements)" , cmd
, len
);
575 if (! config
. csv
) printf ( " \n " );
576 } while ( config
. loop
);