| 1 | /* Redis benchmark utility. |
| 2 | * |
| 3 | * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com> |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are met: |
| 8 | * |
| 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. |
| 17 | * |
| 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. |
| 29 | */ |
| 30 | |
| 31 | #include "fmacros.h" |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <unistd.h> |
| 37 | #include <errno.h> |
| 38 | #include <sys/time.h> |
| 39 | #include <signal.h> |
| 40 | #include <assert.h> |
| 41 | |
| 42 | #include "ae.h" |
| 43 | #include "hiredis.h" |
| 44 | #include "sds.h" |
| 45 | #include "adlist.h" |
| 46 | #include "zmalloc.h" |
| 47 | |
| 48 | #define REDIS_NOTUSED(V) ((void) V) |
| 49 | |
| 50 | static struct config { |
| 51 | aeEventLoop *el; |
| 52 | const char *hostip; |
| 53 | int hostport; |
| 54 | const char *hostsocket; |
| 55 | int numclients; |
| 56 | int liveclients; |
| 57 | int requests; |
| 58 | int requests_issued; |
| 59 | int requests_finished; |
| 60 | int keysize; |
| 61 | int datasize; |
| 62 | int randomkeys; |
| 63 | int randomkeys_keyspacelen; |
| 64 | int keepalive; |
| 65 | int pipeline; |
| 66 | long long start; |
| 67 | long long totlatency; |
| 68 | long long *latency; |
| 69 | const char *title; |
| 70 | list *clients; |
| 71 | int quiet; |
| 72 | int csv; |
| 73 | int loop; |
| 74 | int idlemode; |
| 75 | char *tests; |
| 76 | } config; |
| 77 | |
| 78 | typedef struct _client { |
| 79 | redisContext *context; |
| 80 | sds obuf; |
| 81 | char *randptr[32]; /* needed for MSET against 10 keys */ |
| 82 | size_t randlen; |
| 83 | unsigned int written; /* bytes of 'obuf' already written */ |
| 84 | long long start; /* start time of a request */ |
| 85 | long long latency; /* request latency */ |
| 86 | int pending; /* Number of pending requests (sent but no reply received) */ |
| 87 | } *client; |
| 88 | |
| 89 | /* Prototypes */ |
| 90 | static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask); |
| 91 | static void createMissingClients(client c); |
| 92 | |
| 93 | /* Implementation */ |
| 94 | static long long ustime(void) { |
| 95 | struct timeval tv; |
| 96 | long long ust; |
| 97 | |
| 98 | gettimeofday(&tv, NULL); |
| 99 | ust = ((long)tv.tv_sec)*1000000; |
| 100 | ust += tv.tv_usec; |
| 101 | return ust; |
| 102 | } |
| 103 | |
| 104 | static long long mstime(void) { |
| 105 | struct timeval tv; |
| 106 | long long mst; |
| 107 | |
| 108 | gettimeofday(&tv, NULL); |
| 109 | mst = ((long)tv.tv_sec)*1000; |
| 110 | mst += tv.tv_usec/1000; |
| 111 | return mst; |
| 112 | } |
| 113 | |
| 114 | static void freeClient(client c) { |
| 115 | listNode *ln; |
| 116 | aeDeleteFileEvent(config.el,c->context->fd,AE_WRITABLE); |
| 117 | aeDeleteFileEvent(config.el,c->context->fd,AE_READABLE); |
| 118 | redisFree(c->context); |
| 119 | sdsfree(c->obuf); |
| 120 | zfree(c); |
| 121 | config.liveclients--; |
| 122 | ln = listSearchKey(config.clients,c); |
| 123 | assert(ln != NULL); |
| 124 | listDelNode(config.clients,ln); |
| 125 | } |
| 126 | |
| 127 | static void freeAllClients(void) { |
| 128 | listNode *ln = config.clients->head, *next; |
| 129 | |
| 130 | while(ln) { |
| 131 | next = ln->next; |
| 132 | freeClient(ln->value); |
| 133 | ln = next; |
| 134 | } |
| 135 | } |
| 136 | |
| 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); |
| 141 | c->written = 0; |
| 142 | c->pending = config.pipeline; |
| 143 | } |
| 144 | |
| 145 | static void randomizeClientKey(client c) { |
| 146 | char buf[32]; |
| 147 | size_t i, r; |
| 148 | |
| 149 | for (i = 0; i < c->randlen; i++) { |
| 150 | r = random() % config.randomkeys_keyspacelen; |
| 151 | snprintf(buf,sizeof(buf),"%012zu",r); |
| 152 | memcpy(c->randptr[i],buf,12); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static void clientDone(client c) { |
| 157 | if (config.requests_finished == config.requests) { |
| 158 | freeClient(c); |
| 159 | aeStop(config.el); |
| 160 | return; |
| 161 | } |
| 162 | if (config.keepalive) { |
| 163 | resetClient(c); |
| 164 | } else { |
| 165 | config.liveclients--; |
| 166 | createMissingClients(c); |
| 167 | config.liveclients++; |
| 168 | freeClient(c); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static void readHandler(aeEventLoop *el, int fd, void *privdata, int mask) { |
| 173 | client c = privdata; |
| 174 | void *reply = NULL; |
| 175 | REDIS_NOTUSED(el); |
| 176 | REDIS_NOTUSED(fd); |
| 177 | REDIS_NOTUSED(mask); |
| 178 | |
| 179 | /* Calculate latency only for the first read event. This means that the |
| 180 | * server already sent the reply and we need to parse it. Parsing overhead |
| 181 | * is not part of the latency, so calculate it only once, here. */ |
| 182 | if (c->latency < 0) c->latency = ustime()-(c->start); |
| 183 | |
| 184 | if (redisBufferRead(c->context) != REDIS_OK) { |
| 185 | fprintf(stderr,"Error: %s\n",c->context->errstr); |
| 186 | exit(1); |
| 187 | } else { |
| 188 | while(c->pending) { |
| 189 | if (redisGetReply(c->context,&reply) != REDIS_OK) { |
| 190 | fprintf(stderr,"Error: %s\n",c->context->errstr); |
| 191 | exit(1); |
| 192 | } |
| 193 | if (reply != NULL) { |
| 194 | if (reply == (void*)REDIS_REPLY_ERROR) { |
| 195 | fprintf(stderr,"Unexpected error reply, exiting...\n"); |
| 196 | exit(1); |
| 197 | } |
| 198 | |
| 199 | freeReplyObject(reply); |
| 200 | |
| 201 | if (config.requests_finished < config.requests) |
| 202 | config.latency[config.requests_finished++] = c->latency; |
| 203 | c->pending--; |
| 204 | if (c->pending == 0) clientDone(c); |
| 205 | } else { |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask) { |
| 213 | client c = privdata; |
| 214 | REDIS_NOTUSED(el); |
| 215 | REDIS_NOTUSED(fd); |
| 216 | REDIS_NOTUSED(mask); |
| 217 | |
| 218 | /* Initialize request when nothing was written. */ |
| 219 | if (c->written == 0) { |
| 220 | /* Enforce upper bound to number of requests. */ |
| 221 | if (config.requests_issued++ >= config.requests) { |
| 222 | freeClient(c); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | /* Really initialize: randomize keys and set start time. */ |
| 227 | if (config.randomkeys) randomizeClientKey(c); |
| 228 | c->start = ustime(); |
| 229 | c->latency = -1; |
| 230 | } |
| 231 | |
| 232 | if (sdslen(c->obuf) > c->written) { |
| 233 | void *ptr = c->obuf+c->written; |
| 234 | int nwritten = write(c->context->fd,ptr,sdslen(c->obuf)-c->written); |
| 235 | if (nwritten == -1) { |
| 236 | if (errno != EPIPE) |
| 237 | fprintf(stderr, "Writing to socket: %s\n", strerror(errno)); |
| 238 | freeClient(c); |
| 239 | return; |
| 240 | } |
| 241 | c->written += nwritten; |
| 242 | if (sdslen(c->obuf) == c->written) { |
| 243 | aeDeleteFileEvent(config.el,c->context->fd,AE_WRITABLE); |
| 244 | aeCreateFileEvent(config.el,c->context->fd,AE_READABLE,readHandler,c); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | static client createClient(char *cmd, size_t len) { |
| 250 | int j; |
| 251 | client c = zmalloc(sizeof(struct _client)); |
| 252 | |
| 253 | if (config.hostsocket == NULL) { |
| 254 | c->context = redisConnectNonBlock(config.hostip,config.hostport); |
| 255 | } else { |
| 256 | c->context = redisConnectUnixNonBlock(config.hostsocket); |
| 257 | } |
| 258 | if (c->context->err) { |
| 259 | fprintf(stderr,"Could not connect to Redis at "); |
| 260 | if (config.hostsocket == NULL) |
| 261 | fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,c->context->errstr); |
| 262 | else |
| 263 | fprintf(stderr,"%s: %s\n",config.hostsocket,c->context->errstr); |
| 264 | exit(1); |
| 265 | } |
| 266 | /* Queue N requests accordingly to the pipeline size. */ |
| 267 | c->obuf = sdsempty(); |
| 268 | for (j = 0; j < config.pipeline; j++) |
| 269 | c->obuf = sdscatlen(c->obuf,cmd,len); |
| 270 | c->randlen = 0; |
| 271 | c->written = 0; |
| 272 | c->pending = config.pipeline; |
| 273 | |
| 274 | /* Find substrings in the output buffer that need to be randomized. */ |
| 275 | if (config.randomkeys) { |
| 276 | char *p = c->obuf; |
| 277 | while ((p = strstr(p,":rand:")) != NULL) { |
| 278 | assert(c->randlen < (signed)(sizeof(c->randptr)/sizeof(char*))); |
| 279 | c->randptr[c->randlen++] = p+6; |
| 280 | p += 6; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /* redisSetReplyObjectFunctions(c->context,NULL); */ |
| 285 | aeCreateFileEvent(config.el,c->context->fd,AE_WRITABLE,writeHandler,c); |
| 286 | listAddNodeTail(config.clients,c); |
| 287 | config.liveclients++; |
| 288 | return c; |
| 289 | } |
| 290 | |
| 291 | static void createMissingClients(client c) { |
| 292 | int n = 0; |
| 293 | |
| 294 | while(config.liveclients < config.numclients) { |
| 295 | createClient(c->obuf,sdslen(c->obuf)/config.pipeline); |
| 296 | |
| 297 | /* Listen backlog is quite limited on most systems */ |
| 298 | if (++n > 64) { |
| 299 | usleep(50000); |
| 300 | n = 0; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | static int compareLatency(const void *a, const void *b) { |
| 306 | return (*(long long*)a)-(*(long long*)b); |
| 307 | } |
| 308 | |
| 309 | static void showLatencyReport(void) { |
| 310 | int i, curlat = 0; |
| 311 | float perc, reqpersec; |
| 312 | |
| 313 | reqpersec = (float)config.requests_finished/((float)config.totlatency/1000); |
| 314 | if (!config.quiet && !config.csv) { |
| 315 | printf("====== %s ======\n", config.title); |
| 316 | printf(" %d requests completed in %.2f seconds\n", config.requests_finished, |
| 317 | (float)config.totlatency/1000); |
| 318 | printf(" %d parallel clients\n", config.numclients); |
| 319 | printf(" %d bytes payload\n", config.datasize); |
| 320 | printf(" keep alive: %d\n", config.keepalive); |
| 321 | printf("\n"); |
| 322 | |
| 323 | qsort(config.latency,config.requests,sizeof(long long),compareLatency); |
| 324 | for (i = 0; i < config.requests; i++) { |
| 325 | if (config.latency[i]/1000 != curlat || i == (config.requests-1)) { |
| 326 | curlat = config.latency[i]/1000; |
| 327 | perc = ((float)(i+1)*100)/config.requests; |
| 328 | printf("%.2f%% <= %d milliseconds\n", perc, curlat); |
| 329 | } |
| 330 | } |
| 331 | printf("%.2f requests per second\n\n", reqpersec); |
| 332 | } else if (config.csv) { |
| 333 | printf("\"%s\",\"%.2f\"\n", config.title, reqpersec); |
| 334 | } else { |
| 335 | printf("%s: %.2f requests per second\n", config.title, reqpersec); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | static void benchmark(char *title, char *cmd, int len) { |
| 340 | client c; |
| 341 | |
| 342 | config.title = title; |
| 343 | config.requests_issued = 0; |
| 344 | config.requests_finished = 0; |
| 345 | |
| 346 | c = createClient(cmd,len); |
| 347 | createMissingClients(c); |
| 348 | |
| 349 | config.start = mstime(); |
| 350 | aeMain(config.el); |
| 351 | config.totlatency = mstime()-config.start; |
| 352 | |
| 353 | showLatencyReport(); |
| 354 | freeAllClients(); |
| 355 | } |
| 356 | |
| 357 | /* Returns number of consumed options. */ |
| 358 | int parseOptions(int argc, const char **argv) { |
| 359 | int i; |
| 360 | int lastarg; |
| 361 | int exit_status = 1; |
| 362 | |
| 363 | for (i = 1; i < argc; i++) { |
| 364 | lastarg = (i == (argc-1)); |
| 365 | |
| 366 | if (!strcmp(argv[i],"-c")) { |
| 367 | if (lastarg) goto invalid; |
| 368 | config.numclients = atoi(argv[++i]); |
| 369 | } else if (!strcmp(argv[i],"-n")) { |
| 370 | if (lastarg) goto invalid; |
| 371 | config.requests = atoi(argv[++i]); |
| 372 | } else if (!strcmp(argv[i],"-k")) { |
| 373 | if (lastarg) goto invalid; |
| 374 | config.keepalive = atoi(argv[++i]); |
| 375 | } else if (!strcmp(argv[i],"-h")) { |
| 376 | if (lastarg) goto invalid; |
| 377 | config.hostip = strdup(argv[++i]); |
| 378 | } else if (!strcmp(argv[i],"-p")) { |
| 379 | if (lastarg) goto invalid; |
| 380 | config.hostport = atoi(argv[++i]); |
| 381 | } else if (!strcmp(argv[i],"-s")) { |
| 382 | if (lastarg) goto invalid; |
| 383 | config.hostsocket = strdup(argv[++i]); |
| 384 | } else if (!strcmp(argv[i],"-d")) { |
| 385 | if (lastarg) goto invalid; |
| 386 | config.datasize = atoi(argv[++i]); |
| 387 | if (config.datasize < 1) config.datasize=1; |
| 388 | if (config.datasize > 1024*1024*1024) config.datasize = 1024*1024*1024; |
| 389 | } else if (!strcmp(argv[i],"-P")) { |
| 390 | if (lastarg) goto invalid; |
| 391 | config.pipeline = atoi(argv[++i]); |
| 392 | if (config.pipeline <= 0) config.pipeline=1; |
| 393 | } else if (!strcmp(argv[i],"-r")) { |
| 394 | if (lastarg) goto invalid; |
| 395 | config.randomkeys = 1; |
| 396 | config.randomkeys_keyspacelen = atoi(argv[++i]); |
| 397 | if (config.randomkeys_keyspacelen < 0) |
| 398 | config.randomkeys_keyspacelen = 0; |
| 399 | } else if (!strcmp(argv[i],"-q")) { |
| 400 | config.quiet = 1; |
| 401 | } else if (!strcmp(argv[i],"--csv")) { |
| 402 | config.csv = 1; |
| 403 | } else if (!strcmp(argv[i],"-l")) { |
| 404 | config.loop = 1; |
| 405 | } else if (!strcmp(argv[i],"-I")) { |
| 406 | config.idlemode = 1; |
| 407 | } else if (!strcmp(argv[i],"-t")) { |
| 408 | if (lastarg) goto invalid; |
| 409 | /* We get the list of tests to run as a string in the form |
| 410 | * get,set,lrange,...,test_N. Then we add a comma before and |
| 411 | * after the string in order to make sure that searching |
| 412 | * for ",testname," will always get a match if the test is |
| 413 | * enabled. */ |
| 414 | config.tests = sdsnew(","); |
| 415 | config.tests = sdscat(config.tests,(char*)argv[++i]); |
| 416 | config.tests = sdscat(config.tests,","); |
| 417 | sdstolower(config.tests); |
| 418 | } else if (!strcmp(argv[i],"--help")) { |
| 419 | exit_status = 0; |
| 420 | goto usage; |
| 421 | } else { |
| 422 | /* Assume the user meant to provide an option when the arg starts |
| 423 | * with a dash. We're done otherwise and should use the remainder |
| 424 | * as the command and arguments for running the benchmark. */ |
| 425 | if (argv[i][0] == '-') goto invalid; |
| 426 | return i; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | return i; |
| 431 | |
| 432 | invalid: |
| 433 | printf("Invalid option \"%s\" or option argument missing\n\n",argv[i]); |
| 434 | |
| 435 | usage: |
| 436 | printf( |
| 437 | "Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]\n\n" |
| 438 | " -h <hostname> Server hostname (default 127.0.0.1)\n" |
| 439 | " -p <port> Server port (default 6379)\n" |
| 440 | " -s <socket> Server socket (overrides host and port)\n" |
| 441 | " -c <clients> Number of parallel connections (default 50)\n" |
| 442 | " -n <requests> Total number of requests (default 10000)\n" |
| 443 | " -d <size> Data size of SET/GET value in bytes (default 2)\n" |
| 444 | " -k <boolean> 1=keep alive 0=reconnect (default 1)\n" |
| 445 | " -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD\n" |
| 446 | " Using this option the benchmark will get/set keys\n" |
| 447 | " in the form mykey_rand:000000012456 instead of constant\n" |
| 448 | " keys, the <keyspacelen> argument determines the max\n" |
| 449 | " number of values for the random number. For instance\n" |
| 450 | " if set to 10 only rand:000000000000 - rand:000000000009\n" |
| 451 | " range will be allowed.\n" |
| 452 | " -P <numreq> Pipeline <numreq> requests. Default 1 (no pipeline).\n" |
| 453 | " -q Quiet. Just show query/sec values\n" |
| 454 | " --csv Output in CSV format\n" |
| 455 | " -l Loop. Run the tests forever\n" |
| 456 | " -t <tests> Only run the comma separated list of tests. The test\n" |
| 457 | " names are the same as the ones produced as output.\n" |
| 458 | " -I Idle mode. Just open N idle connections and wait.\n\n" |
| 459 | "Examples:\n\n" |
| 460 | " Run the benchmark with the default configuration against 127.0.0.1:6379:\n" |
| 461 | " $ redis-benchmark\n\n" |
| 462 | " Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1:\n" |
| 463 | " $ redis-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20\n\n" |
| 464 | " Fill 127.0.0.1:6379 with about 1 million keys only using the SET test:\n" |
| 465 | " $ redis-benchmark -t set -n 1000000 -r 100000000\n\n" |
| 466 | " Benchmark 127.0.0.1:6379 for a few commands producing CSV output:\n" |
| 467 | " $ redis-benchmark -t ping,set,get -n 100000 --csv\n\n" |
| 468 | " Fill a list with 10000 random elements:\n" |
| 469 | " $ redis-benchmark -r 10000 -n 10000 lpush mylist ele:rand:000000000000\n\n" |
| 470 | ); |
| 471 | exit(exit_status); |
| 472 | } |
| 473 | |
| 474 | int showThroughput(struct aeEventLoop *eventLoop, long long id, void *clientData) { |
| 475 | REDIS_NOTUSED(eventLoop); |
| 476 | REDIS_NOTUSED(id); |
| 477 | REDIS_NOTUSED(clientData); |
| 478 | |
| 479 | if (config.csv) return 250; |
| 480 | float dt = (float)(mstime()-config.start)/1000.0; |
| 481 | float rps = (float)config.requests_finished/dt; |
| 482 | printf("%s: %.2f\r", config.title, rps); |
| 483 | fflush(stdout); |
| 484 | return 250; /* every 250ms */ |
| 485 | } |
| 486 | |
| 487 | /* Return true if the named test was selected using the -t command line |
| 488 | * switch, or if all the tests are selected (no -t passed by user). */ |
| 489 | int test_is_selected(char *name) { |
| 490 | char buf[256]; |
| 491 | int l = strlen(name); |
| 492 | |
| 493 | if (config.tests == NULL) return 1; |
| 494 | buf[0] = ','; |
| 495 | memcpy(buf+1,name,l); |
| 496 | buf[l+1] = ','; |
| 497 | buf[l+2] = '\0'; |
| 498 | return strstr(config.tests,buf) != NULL; |
| 499 | } |
| 500 | |
| 501 | int main(int argc, const char **argv) { |
| 502 | int i; |
| 503 | char *data, *cmd; |
| 504 | int len; |
| 505 | |
| 506 | client c; |
| 507 | |
| 508 | signal(SIGHUP, SIG_IGN); |
| 509 | signal(SIGPIPE, SIG_IGN); |
| 510 | |
| 511 | config.numclients = 50; |
| 512 | config.requests = 10000; |
| 513 | config.liveclients = 0; |
| 514 | config.el = aeCreateEventLoop(1024*10); |
| 515 | aeCreateTimeEvent(config.el,1,showThroughput,NULL,NULL); |
| 516 | config.keepalive = 1; |
| 517 | config.datasize = 3; |
| 518 | config.pipeline = 1; |
| 519 | config.randomkeys = 0; |
| 520 | config.randomkeys_keyspacelen = 0; |
| 521 | config.quiet = 0; |
| 522 | config.csv = 0; |
| 523 | config.loop = 0; |
| 524 | config.idlemode = 0; |
| 525 | config.latency = NULL; |
| 526 | config.clients = listCreate(); |
| 527 | config.hostip = "127.0.0.1"; |
| 528 | config.hostport = 6379; |
| 529 | config.hostsocket = NULL; |
| 530 | config.tests = NULL; |
| 531 | |
| 532 | i = parseOptions(argc,argv); |
| 533 | argc -= i; |
| 534 | argv += i; |
| 535 | |
| 536 | config.latency = zmalloc(sizeof(long long)*config.requests); |
| 537 | |
| 538 | if (config.keepalive == 0) { |
| 539 | 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"); |
| 540 | } |
| 541 | |
| 542 | if (config.idlemode) { |
| 543 | printf("Creating %d idle connections and waiting forever (Ctrl+C when done)\n", config.numclients); |
| 544 | c = createClient("",0); /* will never receive a reply */ |
| 545 | createMissingClients(c); |
| 546 | aeMain(config.el); |
| 547 | /* and will wait for every */ |
| 548 | } |
| 549 | |
| 550 | /* Run benchmark with command in the remainder of the arguments. */ |
| 551 | if (argc) { |
| 552 | sds title = sdsnew(argv[0]); |
| 553 | for (i = 1; i < argc; i++) { |
| 554 | title = sdscatlen(title, " ", 1); |
| 555 | title = sdscatlen(title, (char*)argv[i], strlen(argv[i])); |
| 556 | } |
| 557 | |
| 558 | do { |
| 559 | len = redisFormatCommandArgv(&cmd,argc,argv,NULL); |
| 560 | benchmark(title,cmd,len); |
| 561 | free(cmd); |
| 562 | } while(config.loop); |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /* Run default benchmark suite. */ |
| 568 | do { |
| 569 | data = zmalloc(config.datasize+1); |
| 570 | memset(data,'x',config.datasize); |
| 571 | data[config.datasize] = '\0'; |
| 572 | |
| 573 | if (test_is_selected("ping_inline") || test_is_selected("ping")) |
| 574 | benchmark("PING_INLINE","PING\r\n",6); |
| 575 | |
| 576 | if (test_is_selected("ping_mbulk") || test_is_selected("ping")) { |
| 577 | len = redisFormatCommand(&cmd,"PING"); |
| 578 | benchmark("PING_BULK",cmd,len); |
| 579 | free(cmd); |
| 580 | } |
| 581 | |
| 582 | if (test_is_selected("set")) { |
| 583 | len = redisFormatCommand(&cmd,"SET foo:rand:000000000000 %s",data); |
| 584 | benchmark("SET",cmd,len); |
| 585 | free(cmd); |
| 586 | } |
| 587 | |
| 588 | if (test_is_selected("get")) { |
| 589 | len = redisFormatCommand(&cmd,"GET foo:rand:000000000000"); |
| 590 | benchmark("GET",cmd,len); |
| 591 | free(cmd); |
| 592 | } |
| 593 | |
| 594 | if (test_is_selected("incr")) { |
| 595 | len = redisFormatCommand(&cmd,"INCR counter:rand:000000000000"); |
| 596 | benchmark("INCR",cmd,len); |
| 597 | free(cmd); |
| 598 | } |
| 599 | |
| 600 | if (test_is_selected("lpush")) { |
| 601 | len = redisFormatCommand(&cmd,"LPUSH mylist %s",data); |
| 602 | benchmark("LPUSH",cmd,len); |
| 603 | free(cmd); |
| 604 | } |
| 605 | |
| 606 | if (test_is_selected("lpop")) { |
| 607 | len = redisFormatCommand(&cmd,"LPOP mylist"); |
| 608 | benchmark("LPOP",cmd,len); |
| 609 | free(cmd); |
| 610 | } |
| 611 | |
| 612 | if (test_is_selected("sadd")) { |
| 613 | len = redisFormatCommand(&cmd, |
| 614 | "SADD myset counter:rand:000000000000"); |
| 615 | benchmark("SADD",cmd,len); |
| 616 | free(cmd); |
| 617 | } |
| 618 | |
| 619 | if (test_is_selected("spop")) { |
| 620 | len = redisFormatCommand(&cmd,"SPOP myset"); |
| 621 | benchmark("SPOP",cmd,len); |
| 622 | free(cmd); |
| 623 | } |
| 624 | |
| 625 | if (test_is_selected("lrange") || |
| 626 | test_is_selected("lrange_100") || |
| 627 | test_is_selected("lrange_300") || |
| 628 | test_is_selected("lrange_500") || |
| 629 | test_is_selected("lrange_600")) |
| 630 | { |
| 631 | len = redisFormatCommand(&cmd,"LPUSH mylist %s",data); |
| 632 | benchmark("LPUSH (needed to benchmark LRANGE)",cmd,len); |
| 633 | free(cmd); |
| 634 | } |
| 635 | |
| 636 | if (test_is_selected("lrange") || test_is_selected("lrange_100")) { |
| 637 | len = redisFormatCommand(&cmd,"LRANGE mylist 0 99"); |
| 638 | benchmark("LRANGE_100 (first 100 elements)",cmd,len); |
| 639 | free(cmd); |
| 640 | } |
| 641 | |
| 642 | if (test_is_selected("lrange") || test_is_selected("lrange_300")) { |
| 643 | len = redisFormatCommand(&cmd,"LRANGE mylist 0 299"); |
| 644 | benchmark("LRANGE_300 (first 300 elements)",cmd,len); |
| 645 | free(cmd); |
| 646 | } |
| 647 | |
| 648 | if (test_is_selected("lrange") || test_is_selected("lrange_500")) { |
| 649 | len = redisFormatCommand(&cmd,"LRANGE mylist 0 449"); |
| 650 | benchmark("LRANGE_500 (first 450 elements)",cmd,len); |
| 651 | free(cmd); |
| 652 | } |
| 653 | |
| 654 | if (test_is_selected("lrange") || test_is_selected("lrange_600")) { |
| 655 | len = redisFormatCommand(&cmd,"LRANGE mylist 0 599"); |
| 656 | benchmark("LRANGE_600 (first 600 elements)",cmd,len); |
| 657 | free(cmd); |
| 658 | } |
| 659 | |
| 660 | if (test_is_selected("mset")) { |
| 661 | const char *argv[21]; |
| 662 | argv[0] = "MSET"; |
| 663 | for (i = 1; i < 21; i += 2) { |
| 664 | argv[i] = "foo:rand:000000000000"; |
| 665 | argv[i+1] = data; |
| 666 | } |
| 667 | len = redisFormatCommandArgv(&cmd,21,argv,NULL); |
| 668 | benchmark("MSET (10 keys)",cmd,len); |
| 669 | free(cmd); |
| 670 | } |
| 671 | |
| 672 | if (!config.csv) printf("\n"); |
| 673 | } while(config.loop); |
| 674 | |
| 675 | return 0; |
| 676 | } |