]>
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
{
59 int randomkeys_keyspacelen
;
75 typedef struct _client
{
76 redisContext
* context
;
78 char * randptr
[ 10 ]; /* needed for MSET against 10 keys */
80 unsigned int written
; /* bytes of 'obuf' already written */
81 long long start
; /* start time of a request */
82 long long latency
; /* request latency */
86 static void writeHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
);
87 static void createMissingClients ( client c
);
90 static long long ustime ( void ) {
94 gettimeofday (& tv
, NULL
);
95 ust
= (( long ) tv
. tv_sec
)* 1000000 ;
100 static long long mstime ( void ) {
104 gettimeofday (& tv
, NULL
);
105 mst
= (( long ) tv
. tv_sec
)* 1000 ;
106 mst
+= tv
. tv_usec
/ 1000 ;
110 static void freeClient ( client c
) {
112 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
);
113 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_READABLE
);
114 redisFree ( c
-> context
);
117 config
. liveclients
--;
118 ln
= listSearchKey ( config
. clients
, c
);
120 listDelNode ( config
. clients
, ln
);
123 static void freeAllClients ( void ) {
124 listNode
* ln
= config
. clients
-> head
, * next
;
128 freeClient ( ln
-> value
);
133 static void resetClient ( client c
) {
134 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
);
135 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_READABLE
);
136 aeCreateFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
, writeHandler
, c
);
140 static void randomizeClientKey ( client c
) {
144 for ( i
= 0 ; i
< c
-> randlen
; i
++) {
145 r
= random () % config
. randomkeys_keyspacelen
;
146 snprintf ( buf
, sizeof ( buf
), " %0 12lu" , r
);
147 memcpy ( c
-> randptr
[ i
], buf
, 12 );
151 static void clientDone ( client c
) {
152 if ( config
. donerequests
== config
. requests
) {
157 if ( config
. keepalive
) {
160 config
. liveclients
--;
161 createMissingClients ( c
);
162 config
. liveclients
++;
167 static void readHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
) {
174 /* Calculate latency only for the first read event. This means that the
175 * server already sent the reply and we need to parse it. Parsing overhead
176 * is not part of the latency, so calculate it only once, here. */
177 if ( c
-> latency
< 0 ) c
-> latency
= ustime ()-( c
-> start
);
179 if ( redisBufferRead ( c
-> context
) != REDIS_OK
) {
180 fprintf ( stderr
, "Error: %s \n " , c
-> context
-> errstr
);
183 if ( redisGetReply ( c
-> context
,& reply
) != REDIS_OK
) {
184 fprintf ( stderr
, "Error: %s \n " , c
-> context
-> errstr
);
188 if ( reply
== ( void *) REDIS_REPLY_ERROR
) {
189 fprintf ( stderr
, "Unexpected error reply, exiting... \n " );
193 if ( config
. donerequests
< config
. requests
)
194 config
. latency
[ config
. donerequests
++] = c
-> latency
;
200 static void writeHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
) {
206 /* When nothing was written yet, randomize keys and set start time. */
207 if ( c
-> written
== 0 ) {
208 if ( config
. randomkeys
) randomizeClientKey ( c
);
213 if ( sdslen ( c
-> obuf
) > c
-> written
) {
214 void * ptr
= c
-> obuf
+ c
-> written
;
215 int nwritten
= write ( c
-> context
-> fd
, ptr
, sdslen ( c
-> obuf
)- c
-> written
);
216 if ( nwritten
== - 1 ) {
218 fprintf ( stderr
, "Writing to socket: %s \n " , strerror ( errno
));
222 c
-> written
+= nwritten
;
223 if ( sdslen ( c
-> obuf
) == c
-> written
) {
224 aeDeleteFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
);
225 aeCreateFileEvent ( config
. el
, c
-> context
-> fd
, AE_READABLE
, readHandler
, c
);
230 static client
createClient ( char * cmd
, int len
) {
231 client c
= zmalloc ( sizeof ( struct _client
));
232 if ( config
. hostsocket
== NULL
) {
233 c
-> context
= redisConnectNonBlock ( config
. hostip
, config
. hostport
);
235 c
-> context
= redisConnectUnixNonBlock ( config
. hostsocket
);
237 if ( c
-> context
-> err
) {
238 fprintf ( stderr
, "Could not connect to Redis at " );
239 if ( config
. hostsocket
== NULL
)
240 fprintf ( stderr
, " %s : %d : %s \n " , config
. hostip
, config
. hostport
, c
-> context
-> errstr
);
242 fprintf ( stderr
, " %s : %s \n " , config
. hostsocket
, c
-> context
-> errstr
);
245 c
-> obuf
= sdsnewlen ( cmd
, len
);
249 /* Find substrings in the output buffer that need to be randomized. */
250 if ( config
. randomkeys
) {
251 char * p
= c
-> obuf
, * newline
;
252 while (( p
= strstr ( p
, ":rand:" )) != NULL
) {
253 newline
= strstr ( p
, " \r\n " );
254 assert ( newline
-( p
+ 6 ) == 12 ); /* 12 chars for randomness */
255 assert ( c
-> randlen
< ( signed )( sizeof ( c
-> randptr
)/ sizeof ( char *)));
256 c
-> randptr
[ c
-> randlen
++] = p
+ 6 ;
261 redisSetReplyObjectFunctions ( c
-> context
, NULL
);
262 aeCreateFileEvent ( config
. el
, c
-> context
-> fd
, AE_WRITABLE
, writeHandler
, c
);
263 listAddNodeTail ( config
. clients
, c
);
264 config
. liveclients
++;
268 static void createMissingClients ( client c
) {
271 while ( config
. liveclients
< config
. numclients
) {
272 createClient ( c
-> obuf
, sdslen ( c
-> obuf
));
274 /* Listen backlog is quite limited on most systems */
282 static int compareLatency ( const void * a
, const void * b
) {
283 return (*( long long *) a
)-(*( long long *) b
);
286 static void showLatencyReport ( void ) {
288 float perc
, reqpersec
;
290 reqpersec
= ( float ) config
. donerequests
/(( float ) config
. totlatency
/ 1000 );
292 printf ( "====== %s ====== \n " , config
. title
);
293 printf ( " %d requests completed in %.2f seconds \n " , config
. donerequests
,
294 ( float ) config
. totlatency
/ 1000 );
295 printf ( " %d parallel clients \n " , config
. numclients
);
296 printf ( " %d bytes payload \n " , config
. datasize
);
297 printf ( " keep alive: %d \n " , config
. keepalive
);
300 qsort ( config
. latency
, config
. requests
, sizeof ( long long ), compareLatency
);
301 for ( i
= 0 ; i
< config
. requests
; i
++) {
302 if ( config
. latency
[ i
]/ 1000 != curlat
|| i
== ( config
. requests
- 1 )) {
303 curlat
= config
. latency
[ i
]/ 1000 ;
304 perc
= (( float )( i
+ 1 )* 100 )/ config
. requests
;
305 printf ( "%.2f%% <= %d milliseconds \n " , perc
, curlat
);
308 printf ( "%.2f requests per second \n\n " , reqpersec
);
310 printf ( " %s : %.2f requests per second \n " , config
. title
, reqpersec
);
314 static void benchmark ( char * title
, char * cmd
, int len
) {
317 config
. title
= title
;
318 config
. donerequests
= 0 ;
320 c
= createClient ( cmd
, len
);
321 createMissingClients ( c
);
323 config
. start
= mstime ();
325 config
. totlatency
= mstime ()- config
. start
;
331 void parseOptions ( int argc
, char ** argv
) {
334 for ( i
= 1 ; i
< argc
; i
++) {
335 int lastarg
= i
== argc
- 1 ;
337 if (! strcmp ( argv
[ i
], "-c" ) && ! lastarg
) {
338 config
. numclients
= atoi ( argv
[ i
+ 1 ]);
340 } else if (! strcmp ( argv
[ i
], "-n" ) && ! lastarg
) {
341 config
. requests
= atoi ( argv
[ i
+ 1 ]);
343 } else if (! strcmp ( argv
[ i
], "-k" ) && ! lastarg
) {
344 config
. keepalive
= atoi ( argv
[ i
+ 1 ]);
346 } else if (! strcmp ( argv
[ i
], "-h" ) && ! lastarg
) {
347 config
. hostip
= argv
[ i
+ 1 ];
349 } else if (! strcmp ( argv
[ i
], "-p" ) && ! lastarg
) {
350 config
. hostport
= atoi ( argv
[ i
+ 1 ]);
352 } else if (! strcmp ( argv
[ i
], "-s" ) && ! lastarg
) {
353 config
. hostsocket
= argv
[ i
+ 1 ];
355 } else if (! strcmp ( argv
[ i
], "-d" ) && ! lastarg
) {
356 config
. datasize
= atoi ( argv
[ i
+ 1 ]);
358 if ( config
. datasize
< 1 ) config
. datasize
= 1 ;
359 if ( config
. datasize
> 1024 * 1024 ) config
. datasize
= 1024 * 1024 ;
360 } else if (! strcmp ( argv
[ i
], "-r" ) && ! lastarg
) {
361 config
. randomkeys
= 1 ;
362 config
. randomkeys_keyspacelen
= atoi ( argv
[ i
+ 1 ]);
363 if ( config
. randomkeys_keyspacelen
< 0 )
364 config
. randomkeys_keyspacelen
= 0 ;
366 } else if (! strcmp ( argv
[ i
], "-q" )) {
368 } else if (! strcmp ( argv
[ i
], "-l" )) {
370 } else if (! strcmp ( argv
[ i
], "-D" )) {
372 } else if (! strcmp ( argv
[ i
], "-I" )) {
375 printf ( "Wrong option ' %s ' or option argument missing \n\n " , argv
[ i
]);
376 printf ( "Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>] \n\n " );
377 printf ( " -h <hostname> Server hostname (default 127.0.0.1) \n " );
378 printf ( " -p <port> Server port (default 6379) \n " );
379 printf ( " -s <socket> Server socket (overrides host and port) \n " );
380 printf ( " -c <clients> Number of parallel connections (default 50) \n " );
381 printf ( " -n <requests> Total number of requests (default 10000) \n " );
382 printf ( " -d <size> Data size of SET/GET value in bytes (default 2) \n " );
383 printf ( " -k <boolean> 1=keep alive 0=reconnect (default 1) \n " );
384 printf ( " -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD \n " );
385 printf ( " Using this option the benchmark will get/set keys \n " );
386 printf ( " in the form mykey_rand000000012456 instead of constant \n " );
387 printf ( " keys, the <keyspacelen> argument determines the max \n " );
388 printf ( " number of values for the random number. For instance \n " );
389 printf ( " if set to 10 only rand000000000000 - rand000000000009 \n " );
390 printf ( " range will be allowed. \n " );
391 printf ( " -q Quiet. Just show query/sec values \n " );
392 printf ( " -l Loop. Run the tests forever \n " );
393 printf ( " -I Idle mode. Just open N idle connections and wait. \n " );
394 printf ( " -D Debug mode. more verbose. \n " );
400 int showThroughput ( struct aeEventLoop
* eventLoop
, long long id
, void * clientData
) {
401 REDIS_NOTUSED ( eventLoop
);
403 REDIS_NOTUSED ( clientData
);
405 float dt
= ( float )( mstime ()- config
. start
)/ 1000.0 ;
406 float rps
= ( float ) config
. donerequests
/ dt
;
407 printf ( " %s : %.2f \r " , config
. title
, rps
);
409 return 250 ; /* every 250ms */
412 int main ( int argc
, char ** argv
) {
416 signal ( SIGHUP
, SIG_IGN
);
417 signal ( SIGPIPE
, SIG_IGN
);
420 config
. numclients
= 50 ;
421 config
. requests
= 10000 ;
422 config
. liveclients
= 0 ;
423 config
. el
= aeCreateEventLoop ();
424 aeCreateTimeEvent ( config
. el
, 1 , showThroughput
, NULL
, NULL
);
425 config
. keepalive
= 1 ;
426 config
. donerequests
= 0 ;
428 config
. randomkeys
= 0 ;
429 config
. randomkeys_keyspacelen
= 0 ;
433 config
. latency
= NULL
;
434 config
. clients
= listCreate ();
435 config
. hostip
= "127.0.0.1" ;
436 config
. hostport
= 6379 ;
437 config
. hostsocket
= NULL
;
439 parseOptions ( argc
, argv
);
440 config
. latency
= zmalloc ( sizeof ( long long )* config
. requests
);
442 if ( config
. keepalive
== 0 ) {
443 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 " );
446 if ( config
. idlemode
) {
447 printf ( "Creating %d idle connections and waiting forever (Ctrl+C when done) \n " , config
. numclients
);
448 c
= createClient ( "" , 0 ); /* will never receive a reply */
449 createMissingClients ( c
);
451 /* and will wait for every */
458 data
= zmalloc ( config
. datasize
+ 1 );
459 memset ( data
, 'x' , config
. datasize
);
460 data
[ config
. datasize
] = '\0' ;
462 benchmark ( "PING (inline)" , "PING \r\n " , 6 );
464 len
= redisFormatCommand (& cmd
, "PING" );
465 benchmark ( "PING" , cmd
, len
);
468 const char * argv
[ 21 ];
470 for ( i
= 1 ; i
< 21 ; i
+= 2 ) {
471 argv
[ i
] = "foo:rand:000000000000" ;
474 len
= redisFormatCommandArgv (& cmd
, 21 , argv
, NULL
);
475 benchmark ( "MSET (10 keys)" , cmd
, len
);
478 len
= redisFormatCommand (& cmd
, "SET foo:rand:000000000000 %s " , data
);
479 benchmark ( "SET" , cmd
, len
);
482 len
= redisFormatCommand (& cmd
, "GET foo:rand:000000000000" );
483 benchmark ( "GET" , cmd
, len
);
486 len
= redisFormatCommand (& cmd
, "INCR counter:rand:000000000000" );
487 benchmark ( "INCR" , cmd
, len
);
490 len
= redisFormatCommand (& cmd
, "LPUSH mylist %s " , data
);
491 benchmark ( "LPUSH" , cmd
, len
);
494 len
= redisFormatCommand (& cmd
, "LPOP mylist" );
495 benchmark ( "LPOP" , cmd
, len
);
498 len
= redisFormatCommand (& cmd
, "SADD myset counter:rand:000000000000" );
499 benchmark ( "SADD" , cmd
, len
);
502 len
= redisFormatCommand (& cmd
, "SPOP myset" );
503 benchmark ( "SPOP" , cmd
, len
);
506 len
= redisFormatCommand (& cmd
, "LPUSH mylist %s " , data
);
507 benchmark ( "LPUSH (again, in order to bench LRANGE)" , cmd
, len
);
510 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 99" );
511 benchmark ( "LRANGE (first 100 elements)" , cmd
, len
);
514 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 299" );
515 benchmark ( "LRANGE (first 300 elements)" , cmd
, len
);
518 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 449" );
519 benchmark ( "LRANGE (first 450 elements)" , cmd
, len
);
522 len
= redisFormatCommand (& cmd
, "LRANGE mylist 0 599" );
523 benchmark ( "LRANGE (first 600 elements)" , cmd
, len
);
527 } while ( config
. loop
);