]>
git.saurik.com Git - redis.git/blob - benchmark.c
1 /* Redis benchmark utility.
3 * Copyright (c) 2006-2009, 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
{
69 int randomkeys_keyspacelen
;
82 typedef struct _client
{
87 int mbulk
; /* Number of elements in an mbulk reply */
88 int readlen
; /* readlen == -1 means read a single line */
89 unsigned int written
; /* bytes of 'obuf' already written */
91 long long start
; /* start time in milliseconds */
95 static void writeHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
);
96 static void createMissingClients ( client c
);
99 static long long mstime ( void ) {
103 gettimeofday (& tv
, NULL
);
104 mst
= (( long ) tv
. tv_sec
)* 1000 ;
105 mst
+= tv
. tv_usec
/ 1000 ;
109 static void freeClient ( client c
) {
112 aeDeleteFileEvent ( config
. el
, c
-> fd
, AE_WRITABLE
);
113 aeDeleteFileEvent ( config
. el
, c
-> fd
, AE_READABLE
);
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
-> fd
, AE_WRITABLE
);
136 aeDeleteFileEvent ( config
. el
, c
-> fd
, AE_READABLE
);
137 aeCreateFileEvent ( config
. el
, c
-> fd
, AE_WRITABLE
, writeHandler
, c
, NULL
);
139 c
-> ibuf
= sdsempty ();
140 c
-> readlen
= ( c
-> replytype
== REPLY_BULK
) ? - 1 : 0 ;
143 c
-> state
= CLIENT_SENDQUERY
;
145 createMissingClients ( c
);
148 static void randomizeClientKey ( client c
) {
153 p
= strstr ( c
-> obuf
, "_rand" );
156 r
= random () % config
. randomkeys_keyspacelen
;
157 sprintf ( buf
, " %l d" , r
);
158 memcpy ( p
, buf
, strlen ( buf
));
161 static void prepareClientForReply ( client c
, int type
) {
162 if ( type
== REPLY_BULK
) {
163 c
-> replytype
= REPLY_BULK
;
165 } else if ( type
== REPLY_MBULK
) {
166 c
-> replytype
= REPLY_MBULK
;
175 static void clientDone ( client c
) {
177 config
. donerequests
++;
178 latency
= mstime () - c
-> start
;
179 if ( latency
> MAX_LATENCY
) latency
= MAX_LATENCY
;
180 config
. latency
[ latency
]++;
182 if ( config
. donerequests
== config
. requests
) {
187 if ( config
. keepalive
) {
189 if ( config
. randomkeys
) randomizeClientKey ( c
);
191 config
. liveclients
--;
192 createMissingClients ( c
);
193 config
. liveclients
++;
198 static void readHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
)
207 nread
= read ( c
-> fd
, buf
, 1024 );
209 fprintf ( stderr
, "Reading from socket: %s \n " , strerror ( errno
));
214 fprintf ( stderr
, "EOF from client \n " );
218 c
-> ibuf
= sdscatlen ( c
-> ibuf
, buf
, nread
);
221 /* Are we waiting for the first line of the command of for sdf
222 * count in bulk or multi bulk operations? */
223 if ( c
-> replytype
== REPLY_INT
||
224 c
-> replytype
== REPLY_RETCODE
||
225 ( c
-> replytype
== REPLY_BULK
&& c
-> readlen
== - 1 ) ||
226 ( c
-> replytype
== REPLY_MBULK
&& c
-> readlen
== - 1 ) ||
227 ( c
-> replytype
== REPLY_MBULK
&& c
-> mbulk
== - 1 )) {
230 /* Check if the first line is complete. This is only true if
231 * there is a newline inside the buffer. */
232 if (( p
= strchr ( c
-> ibuf
, ' \n ' )) != NULL
) {
233 if ( c
-> replytype
== REPLY_BULK
||
234 ( c
-> replytype
== REPLY_MBULK
&& c
-> mbulk
!= - 1 ))
236 /* Read the count of a bulk reply (being it a single bulk or
237 * a multi bulk reply). "$<count>" for the protocol spec. */
240 c
-> readlen
= atoi ( c
-> ibuf
+ 1 )+ 2 ;
241 /* Handle null bulk reply "$-1" */
242 if ( c
-> readlen
- 2 == - 1 ) {
246 /* Leave all the rest in the input buffer */
247 c
-> ibuf
= sdsrange ( c
-> ibuf
,( p
- c
-> ibuf
)+ 1 ,- 1 );
248 /* fall through to reach the point where the code will try
249 * to check if the bulk reply is complete. */
250 } else if ( c
-> replytype
== REPLY_MBULK
&& c
-> mbulk
== - 1 ) {
251 /* Read the count of a multi bulk reply. That is, how many
252 * bulk replies we have to read next. "*<count>" protocol. */
255 c
-> mbulk
= atoi ( c
-> ibuf
+ 1 );
256 /* Handle null bulk reply "*-1" */
257 if ( c
-> mbulk
== - 1 ) {
261 /* Leave all the rest in the input buffer */
262 c
-> ibuf
= sdsrange ( c
-> ibuf
,( p
- c
-> ibuf
)+ 1 ,- 1 );
265 c
-> ibuf
= sdstrim ( c
-> ibuf
, " \r\n " );
271 /* bulk read, did we read everything? */
272 if ((( c
-> replytype
== REPLY_MBULK
&& c
-> mbulk
!= - 1 ) ||
273 ( c
-> replytype
== REPLY_BULK
)) && c
-> readlen
!= - 1 &&
274 ( unsigned ) c
-> readlen
<= sdslen ( c
-> ibuf
))
276 if ( c
-> replytype
== REPLY_BULK
) {
278 } else if ( c
-> replytype
== REPLY_MBULK
) {
279 if (-- c
-> mbulk
== 0 ) {
282 c
-> ibuf
= sdsrange ( c
-> ibuf
, c
-> readlen
,- 1 );
290 static void writeHandler ( aeEventLoop
* el
, int fd
, void * privdata
, int mask
)
297 if ( c
-> state
== CLIENT_CONNECTING
) {
298 c
-> state
= CLIENT_SENDQUERY
;
301 if ( sdslen ( c
-> obuf
) > c
-> written
) {
302 void * ptr
= c
-> obuf
+ c
-> written
;
303 int len
= sdslen ( c
-> obuf
) - c
-> written
;
304 int nwritten
= write ( c
-> fd
, ptr
, len
);
305 if ( nwritten
== - 1 ) {
306 fprintf ( stderr
, "Writing to socket: %s \n " , strerror ( errno
));
310 c
-> written
+= nwritten
;
311 if ( sdslen ( c
-> obuf
) == c
-> written
) {
312 aeDeleteFileEvent ( config
. el
, c
-> fd
, AE_WRITABLE
);
313 aeCreateFileEvent ( config
. el
, c
-> fd
, AE_READABLE
, readHandler
, c
, NULL
);
314 c
-> state
= CLIENT_READREPLY
;
319 static client
createClient ( void ) {
320 client c
= zmalloc ( sizeof ( struct _client
));
321 char err
[ ANET_ERR_LEN
];
323 c
-> fd
= anetTcpNonBlockConnect ( err
, config
. hostip
, config
. hostport
);
324 if ( c
-> fd
== ANET_ERR
) {
326 fprintf ( stderr
, "Connect: %s \n " , err
);
329 anetTcpNoDelay ( NULL
, c
-> fd
);
330 c
-> obuf
= sdsempty ();
331 c
-> ibuf
= sdsempty ();
335 c
-> state
= CLIENT_CONNECTING
;
336 aeCreateFileEvent ( config
. el
, c
-> fd
, AE_WRITABLE
, writeHandler
, c
, NULL
);
337 config
. liveclients
++;
338 listAddNodeTail ( config
. clients
, c
);
342 static void createMissingClients ( client c
) {
343 while ( config
. liveclients
< config
. numclients
) {
344 client
new = createClient ();
347 new -> obuf
= sdsdup ( c
-> obuf
);
348 if ( config
. randomkeys
) randomizeClientKey ( c
);
349 new -> replytype
= c
-> replytype
;
350 if ( c
-> replytype
== REPLY_BULK
)
355 static void showLatencyReport ( char * title
) {
357 float perc
, reqpersec
;
359 reqpersec
= ( float ) config
. donerequests
/(( float ) config
. totlatency
/ 1000 );
361 printf ( "====== %s ====== \n " , title
);
362 printf ( " %d requests completed in %.2f seconds \n " , config
. donerequests
,
363 ( float ) config
. totlatency
/ 1000 );
364 printf ( " %d parallel clients \n " , config
. numclients
);
365 printf ( " %d bytes payload \n " , config
. datasize
);
366 printf ( " keep alive: %d \n " , config
. keepalive
);
368 for ( j
= 0 ; j
<= MAX_LATENCY
; j
++) {
369 if ( config
. latency
[ j
]) {
370 seen
+= config
. latency
[ j
];
371 perc
= (( float ) seen
* 100 )/ config
. donerequests
;
372 printf ( "%.2f%% <= %d milliseconds \n " , perc
, j
);
375 printf ( "%.2f requests per second \n\n " , reqpersec
);
377 printf ( " %s : %.2f requests per second \n " , title
, reqpersec
);
381 static void prepareForBenchmark ( void )
383 memset ( config
. latency
, 0 , sizeof ( int )*( MAX_LATENCY
+ 1 ));
384 config
. start
= mstime ();
385 config
. donerequests
= 0 ;
388 static void endBenchmark ( char * title
) {
389 config
. totlatency
= mstime ()- config
. start
;
390 showLatencyReport ( title
);
394 void parseOptions ( int argc
, char ** argv
) {
397 for ( i
= 1 ; i
< argc
; i
++) {
398 int lastarg
= i
== argc
- 1 ;
400 if (! strcmp ( argv
[ i
], "-c" ) && ! lastarg
) {
401 config
. numclients
= atoi ( argv
[ i
+ 1 ]);
403 } else if (! strcmp ( argv
[ i
], "-n" ) && ! lastarg
) {
404 config
. requests
= atoi ( argv
[ i
+ 1 ]);
406 } else if (! strcmp ( argv
[ i
], "-k" ) && ! lastarg
) {
407 config
. keepalive
= atoi ( argv
[ i
+ 1 ]);
409 } else if (! strcmp ( argv
[ i
], "-h" ) && ! lastarg
) {
410 char * ip
= zmalloc ( 32 );
411 if ( anetResolve ( NULL
, argv
[ i
+ 1 ], ip
) == ANET_ERR
) {
412 printf ( "Can't resolve %s \n " , argv
[ i
]);
417 } else if (! strcmp ( argv
[ i
], "-p" ) && ! lastarg
) {
418 config
. hostport
= atoi ( argv
[ i
+ 1 ]);
420 } else if (! strcmp ( argv
[ i
], "-d" ) && ! lastarg
) {
421 config
. datasize
= atoi ( argv
[ i
+ 1 ]);
423 if ( config
. datasize
< 1 ) config
. datasize
= 1 ;
424 if ( config
. datasize
> 1024 * 1024 ) config
. datasize
= 1024 * 1024 ;
425 } else if (! strcmp ( argv
[ i
], "-r" ) && ! lastarg
) {
426 config
. randomkeys
= 1 ;
427 config
. randomkeys_keyspacelen
= atoi ( argv
[ i
+ 1 ]);
428 if ( config
. randomkeys_keyspacelen
< 0 )
429 config
. randomkeys_keyspacelen
= 0 ;
431 } else if (! strcmp ( argv
[ i
], "-q" )) {
433 } else if (! strcmp ( argv
[ i
], "-l" )) {
436 printf ( "Wrong option ' %s ' or option argument missing \n\n " , argv
[ i
]);
437 printf ( "Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>] \n\n " );
438 printf ( " -h <hostname> Server hostname (default 127.0.0.1) \n " );
439 printf ( " -p <hostname> Server port (default 6379) \n " );
440 printf ( " -c <clients> Number of parallel connections (default 50) \n " );
441 printf ( " -n <requests> Total number of requests (default 10000) \n " );
442 printf ( " -d <size> Data size of SET/GET value in bytes (default 2) \n " );
443 printf ( " -k <boolean> 1=keep alive 0=reconnect (default 1) \n " );
444 printf ( " -r <keyspacelen> Use random keys for SET/GET/INCR \n " );
445 printf ( " Using this option the benchmark will get/set keys \n " );
446 printf ( " in the form mykey_rand000000012456 instead of constant \n " );
447 printf ( " keys, the <keyspacelen> argument determines the max \n " );
448 printf ( " number of values for the random number. For instance \n " );
449 printf ( " if set to 10 only rand000000000000 - rand000000000009 \n " );
450 printf ( " range will be allowed. \n " );
451 printf ( " -q Quiet. Just show query/sec values \n " );
452 printf ( " -l Loop. Run the tests forever \n " );
458 int main ( int argc
, char ** argv
) {
461 signal ( SIGHUP
, SIG_IGN
);
462 signal ( SIGPIPE
, SIG_IGN
);
464 config
. numclients
= 50 ;
465 config
. requests
= 10000 ;
466 config
. liveclients
= 0 ;
467 config
. el
= aeCreateEventLoop ();
468 config
. keepalive
= 1 ;
469 config
. donerequests
= 0 ;
471 config
. randomkeys
= 0 ;
472 config
. randomkeys_keyspacelen
= 0 ;
475 config
. latency
= NULL
;
476 config
. clients
= listCreate ();
477 config
. latency
= zmalloc ( sizeof ( int )*( MAX_LATENCY
+ 1 ));
479 config
. hostip
= "127.0.0.1" ;
480 config
. hostport
= 6379 ;
482 parseOptions ( argc
, argv
);
484 if ( config
. keepalive
== 0 ) {
485 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 " );
489 prepareForBenchmark ();
492 c
-> obuf
= sdscatprintf ( c
-> obuf
, "SET foo_rand000000000000 %d \r\n " , config
. datasize
);
494 char * data
= zmalloc ( config
. datasize
+ 2 );
495 memset ( data
, 'x' , config
. datasize
);
496 data
[ config
. datasize
] = ' \r ' ;
497 data
[ config
. datasize
+ 1 ] = ' \n ' ;
498 c
-> obuf
= sdscatlen ( c
-> obuf
, data
, config
. datasize
+ 2 );
500 prepareClientForReply ( c
, REPLY_RETCODE
);
501 createMissingClients ( c
);
505 prepareForBenchmark ();
508 c
-> obuf
= sdscat ( c
-> obuf
, "GET foo_rand000000000000 \r\n " );
509 prepareClientForReply ( c
, REPLY_BULK
);
510 createMissingClients ( c
);
514 prepareForBenchmark ();
517 c
-> obuf
= sdscat ( c
-> obuf
, "INCR counter_rand000000000000 \r\n " );
518 prepareClientForReply ( c
, REPLY_INT
);
519 createMissingClients ( c
);
521 endBenchmark ( "INCR" );
523 prepareForBenchmark ();
526 c
-> obuf
= sdscat ( c
-> obuf
, "LPUSH mylist 3 \r\n bar \r\n " );
527 prepareClientForReply ( c
, REPLY_INT
);
528 createMissingClients ( c
);
530 endBenchmark ( "LPUSH" );
532 prepareForBenchmark ();
535 c
-> obuf
= sdscat ( c
-> obuf
, "LPOP mylist \r\n " );
536 prepareClientForReply ( c
, REPLY_BULK
);
537 createMissingClients ( c
);
539 endBenchmark ( "LPOP" );
541 prepareForBenchmark ();
544 c
-> obuf
= sdscat ( c
-> obuf
, "PING \r\n " );
545 prepareClientForReply ( c
, REPLY_RETCODE
);
546 createMissingClients ( c
);
548 endBenchmark ( "PING" );
550 prepareForBenchmark ();
553 c
-> obuf
= sdscat ( c
-> obuf
, "LPUSH mylist 3 \r\n bar \r\n " );
554 prepareClientForReply ( c
, REPLY_RETCODE
);
555 createMissingClients ( c
);
557 endBenchmark ( "LPUSH (again, in order to bench LRANGE)" );
559 prepareForBenchmark ();
562 c
-> obuf
= sdscat ( c
-> obuf
, "LRANGE mylist 0 99 \r\n " );
563 prepareClientForReply ( c
, REPLY_MBULK
);
564 createMissingClients ( c
);
566 endBenchmark ( "LRANGE (first 100 elements)" );
569 } while ( config
. loop
);