]>
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 */
85 long long start
; /* start time of a request */
86 long long latency
; /* request latency */
90 static void writeHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
);
91 static void createMissingClients ( client c
);
94 static long long ustime ( void ) {
98 gettimeofday (& tv
, NULL
);
99 ust
= (( long ) tv
. tv_sec
)* 1000000 ;
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
) {
116 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
);
117 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_READABLE
);
118 redisFree ( c
-> context
);
121 config
. liveclients
--;
122 ln
= listSearchKey ( config
. clients
, c
);
124 listDelNode ( config
. clients
, ln
);
127 static void freeAllClients ( void ) {
128 listNode
* ln
= config
. clients
-> head
, * next
;
132 freeClient ( ln
-> value
);
137 static void resetClient ( client c
) {
138 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
);
139 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_READABLE
);
140 aeCreateFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
, writeHandler
, c
);
142 c
-> state
= CLIENT_SENDQUERY
;
147 static void randomizeClientKey ( client c
) {
152 if ( c
-> randptr
== NULL
) return ;
154 /* Check if we have to randomize (only once per connection) */
155 if ( c
-> randptr
== ( void *)- 1 ) {
156 p
= strstr ( c
-> obuf
, ":rand:" );
161 newline
= strstr ( p
, " \r\n " );
162 assert ( newline
-( p
+ 6 ) == 12 ); /* 12 chars for randomness */
167 /* Set random number in output buffer */
168 r
= random () % config
. randomkeys_keyspacelen
;
169 snprintf ( buf
, sizeof ( buf
), " %0 12ld" , r
);
170 memcpy ( c
-> randptr
, buf
, 12 );
173 static void clientDone ( client c
) {
174 if ( config
. donerequests
== config
. requests
) {
179 if ( config
. keepalive
) {
181 if ( config
. randomkeys
) randomizeClientKey ( c
);
183 config
. liveclients
--;
184 createMissingClients ( c
);
185 config
. liveclients
++;
190 static void readHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
) {
197 /* Calculate latency only for the first read event. This means that the
198 * server already sent the reply and we need to parse it. Parsing overhead
199 * is not part of the latency, so calculate it only once, here. */
200 if ( c
-> latency
< 0 ) c
-> latency
= ustime ()-( c
-> start
);
202 if ( redisBufferRead ( c
-> context
) != REDIS_OK
) {
203 fprintf ( stderr
, "Error: %s \n " , c
-> context
-> errstr
);
206 if ( redisGetReply ( c
-> context
,& reply
) != REDIS_OK
) {
207 fprintf ( stderr
, "Error: %s \n " , c
-> context
-> errstr
);
211 if ( reply
== ( void *) REDIS_REPLY_ERROR
) {
212 fprintf ( stderr
, "Unexpected error reply, exiting... \n " );
216 if ( config
. donerequests
< config
. requests
)
217 config
. latency
[ config
. donerequests
++] = c
-> latency
;
223 static void writeHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
) {
229 if ( c
-> state
== CLIENT_CONNECTING
) {
230 c
-> state
= CLIENT_SENDQUERY
;
234 if ( sdslen ( c
-> obuf
) > c
-> written
) {
235 void * ptr
= c
-> obuf
+ c
-> written
;
236 int nwritten
= write ( c
-> context
-> fd
, ptr
, sdslen ( c
-> obuf
)- c
-> written
);
237 if ( nwritten
== - 1 ) {
239 fprintf ( stderr
, "Writing to socket: %s \n " , strerror ( errno
));
243 c
-> written
+= nwritten
;
244 if ( sdslen ( c
-> obuf
) == c
-> written
) {
245 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
);
246 aeCreateFileEvent ( config
. el
, c
-> context
-> fd
, AE_READABLE
, readHandler
, c
);
247 c
-> state
= CLIENT_READREPLY
;
252 static client
createClient () {
253 client c
= zmalloc ( sizeof ( struct _client
));
254 if ( config
. hostsocket
== NULL
) {
255 c
-> context
= redisConnectNonBlock ( config
. hostip
, config
. hostport
);
257 c
-> context
= redisConnectUnixNonBlock ( config
. hostsocket
);
259 if ( c
-> context
-> err
) {
260 fprintf ( stderr
, "Could not connect to Redis at " );
261 if ( config
. hostsocket
== NULL
)
262 fprintf ( stderr
, " %s : %d : %s \n " , config
. hostip
, config
. hostport
, c
-> context
-> errstr
);
264 fprintf ( stderr
, " %s : %s \n " , config
. hostsocket
, c
-> context
-> errstr
);
267 c
-> state
= CLIENT_CONNECTING
;
269 c
-> randptr
= ( void *)- 1 ;
271 redisSetReplyObjectFunctions ( c
-> context
, NULL
);
272 aeCreateFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
, writeHandler
, c
);
273 listAddNodeTail ( config
. clients
, c
);
274 config
. liveclients
++;
278 static void createMissingClients ( client c
) {
281 while ( config
. liveclients
< config
. numclients
) {
282 client
new = createClient ();
283 new -> obuf
= sdsdup ( c
-> obuf
);
284 if ( config
. randomkeys
) randomizeClientKey ( c
);
286 /* Listen backlog is quite limited on most systems */
294 static int compareLatency ( const void * a
, const void * b
) {
295 return (*( long long *) a
)-(*( long long *) b
);
298 static void showLatencyReport ( void ) {
300 float perc
, reqpersec
;
302 reqpersec
= ( float ) config
. donerequests
/(( float ) config
. totlatency
/ 1000 );
304 printf ( "====== %s ====== \n " , config
. title
);
305 printf ( " %d requests completed in %.2f seconds \n " , config
. donerequests
,
306 ( float ) config
. totlatency
/ 1000 );
307 printf ( " %d parallel clients \n " , config
. numclients
);
308 printf ( " %d bytes payload \n " , config
. datasize
);
309 printf ( " keep alive: %d \n " , config
. keepalive
);
312 qsort ( config
. latency
, config
. requests
, sizeof ( long long ), compareLatency
);
313 for ( i
= 0 ; i
< config
. requests
; i
++) {
314 if ( config
. latency
[ i
]/ 1000 != curlat
|| i
== ( config
. requests
- 1 )) {
315 curlat
= config
. latency
[ i
]/ 1000 ;
316 perc
= (( float )( i
+ 1 )* 100 )/ config
. requests
;
317 printf ( "%.2f%% <= %d milliseconds \n " , perc
, curlat
);
320 printf ( "%.2f requests per second \n\n " , reqpersec
);
322 printf ( " %s : %.2f requests per second \n " , config
. title
, reqpersec
);
326 static void benchmark ( char * title
, char * cmd
, int len
) {
329 config
. title
= title
;
330 config
. donerequests
= 0 ;
333 c
-> obuf
= sdsnewlen ( cmd
, len
);
334 createMissingClients ( c
);
336 config
. start
= mstime ();
338 config
. totlatency
= mstime ()- config
. start
;
344 void parseOptions ( int argc
, char ** argv
) {
347 for ( i
= 1 ; i
< argc
; i
++) {
348 int lastarg
= i
== argc
- 1 ;
350 if (! strcmp ( argv
[ i
], "-c" ) && ! lastarg
) {
351 config
. numclients
= atoi ( argv
[ i
+ 1 ]);
353 } else if (! strcmp ( argv
[ i
], "-n" ) && ! lastarg
) {
354 config
. requests
= atoi ( argv
[ i
+ 1 ]);
356 } else if (! strcmp ( argv
[ i
], "-k" ) && ! lastarg
) {
357 config
. keepalive
= atoi ( argv
[ i
+ 1 ]);
359 } else if (! strcmp ( argv
[ i
], "-h" ) && ! lastarg
) {
360 config
. hostip
= argv
[ i
+ 1 ];
362 } else if (! strcmp ( argv
[ i
], "-p" ) && ! lastarg
) {
363 config
. hostport
= atoi ( argv
[ i
+ 1 ]);
365 } else if (! strcmp ( argv
[ i
], "-s" ) && ! lastarg
) {
366 config
. hostsocket
= argv
[ i
+ 1 ];
368 } else if (! strcmp ( argv
[ i
], "-d" ) && ! lastarg
) {
369 config
. datasize
= atoi ( argv
[ i
+ 1 ]);
371 if ( config
. datasize
< 1 ) config
. datasize
= 1 ;
372 if ( config
. datasize
> 1024 * 1024 ) config
. datasize
= 1024 * 1024 ;
373 } else if (! strcmp ( argv
[ i
], "-r" ) && ! lastarg
) {
374 config
. randomkeys
= 1 ;
375 config
. randomkeys_keyspacelen
= atoi ( argv
[ i
+ 1 ]);
376 if ( config
. randomkeys_keyspacelen
< 0 )
377 config
. randomkeys_keyspacelen
= 0 ;
379 } else if (! strcmp ( argv
[ i
], "-q" )) {
381 } else if (! strcmp ( argv
[ i
], "-l" )) {
383 } else if (! strcmp ( argv
[ i
], "-D" )) {
385 } else if (! strcmp ( argv
[ i
], "-I" )) {
388 printf ( "Wrong option ' %s ' or option argument missing \n\n " , argv
[ i
]);
389 printf ( "Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>] \n\n " );
390 printf ( " -h <hostname> Server hostname (default 127.0.0.1) \n " );
391 printf ( " -p <port> Server port (default 6379) \n " );
392 printf ( " -s <socket> Server socket (overrides host and port) \n " );
393 printf ( " -c <clients> Number of parallel connections (default 50) \n " );
394 printf ( " -n <requests> Total number of requests (default 10000) \n " );
395 printf ( " -d <size> Data size of SET/GET value in bytes (default 2) \n " );
396 printf ( " -k <boolean> 1=keep alive 0=reconnect (default 1) \n " );
397 printf ( " -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD \n " );
398 printf ( " Using this option the benchmark will get/set keys \n " );
399 printf ( " in the form mykey_rand000000012456 instead of constant \n " );
400 printf ( " keys, the <keyspacelen> argument determines the max \n " );
401 printf ( " number of values for the random number. For instance \n " );
402 printf ( " if set to 10 only rand000000000000 - rand000000000009 \n " );
403 printf ( " range will be allowed. \n " );
404 printf ( " -q Quiet. Just show query/sec values \n " );
405 printf ( " -l Loop. Run the tests forever \n " );
406 printf ( " -I Idle mode. Just open N idle connections and wait. \n " );
407 printf ( " -D Debug mode. more verbose. \n " );
413 int showThroughput ( struct aeEventLoop
* eventLoop
, long long id
, void * clientData
) {
414 REDIS_NOTUSED ( eventLoop
);
416 REDIS_NOTUSED ( clientData
);
418 float dt
= ( float )( mstime ()- config
. start
)/ 1000.0 ;
419 float rps
= ( float ) config
. donerequests
/ dt
;
420 printf ( " %s : %.2f \r " , config
. title
, rps
);
422 return 250 ; /* every 250ms */
425 int main ( int argc
, char ** argv
) {
429 signal ( SIGHUP
, SIG_IGN
);
430 signal ( SIGPIPE
, SIG_IGN
);
433 config
. numclients
= 50 ;
434 config
. requests
= 10000 ;
435 config
. liveclients
= 0 ;
436 config
. el
= aeCreateEventLoop ();
437 aeCreateTimeEvent ( config
. el
, 1 , showThroughput
, NULL
, NULL
);
438 config
. keepalive
= 1 ;
439 config
. donerequests
= 0 ;
441 config
. randomkeys
= 0 ;
442 config
. randomkeys_keyspacelen
= 0 ;
446 config
. latency
= NULL
;
447 config
. clients
= listCreate ();
448 config
. hostip
= "127.0.0.1" ;
449 config
. hostport
= 6379 ;
450 config
. hostsocket
= NULL
;
452 parseOptions ( argc
, argv
);
453 config
. latency
= zmalloc ( sizeof ( long long )* config
. requests
);
455 if ( config
. keepalive
== 0 ) {
456 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 " );
459 if ( config
. idlemode
) {
460 printf ( "Creating %d idle connections and waiting forever (Ctrl+C when done) \n " , config
. numclients
);
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 benchmark ( "PING (inline)" , "PING \r\n " , 6 );
478 len
= redisFormatCommand (& cmd
, "PING" );
479 benchmark ( "PING" , cmd
, len
);
482 const char * argv
[ 21 ];
484 for ( i
= 1 ; i
< 21 ; i
+= 2 ) {
485 argv
[ i
] = "foo:rand:000000000000" ;
488 len
= redisFormatCommandArgv (& cmd
, 21 , argv
, NULL
);
489 benchmark ( "MSET (10 keys)" , cmd
, len
);
492 len
= redisFormatCommand (& cmd
, "SET foo:rand:000000000000 %s " , data
);
493 benchmark ( "SET" , cmd
, len
);
496 len
= redisFormatCommand (& cmd
, "GET foo:rand:000000000000" );
497 benchmark ( "GET" , cmd
, len
);
500 len
= redisFormatCommand (& cmd
, "INCR counter:rand:000000000000" );
501 benchmark ( "INCR" , cmd
, len
);
504 len
= redisFormatCommand (& cmd
, "LPUSH mylist %s " , data
);
505 benchmark ( "LPUSH" , cmd
, len
);
508 len
= redisFormatCommand (& cmd
, "LPOP mylist" );
509 benchmark ( "LPOP" , cmd
, len
);
512 len
= redisFormatCommand (& cmd
, "SADD myset counter:rand:000000000000" );
513 benchmark ( "SADD" , cmd
, len
);
516 len
= redisFormatCommand (& cmd
, "SPOP myset" );
517 benchmark ( "SPOP" , cmd
, len
);
520 len
= redisFormatCommand (& cmd
, "LPUSH mylist %s " , data
);
521 benchmark ( "LPUSH (again, in order to bench LRANGE)" , cmd
, len
);
524 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 99" );
525 benchmark ( "LRANGE (first 100 elements)" , cmd
, len
);
528 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 299" );
529 benchmark ( "LRANGE (first 300 elements)" , cmd
, len
);
532 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 449" );
533 benchmark ( "LRANGE (first 450 elements)" , cmd
, len
);
536 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 599" );
537 benchmark ( "LRANGE (first 600 elements)" , cmd
, len
);
541 } while ( config
. loop
);