]>
Commit | Line | Data |
---|---|---|
ed9b544e | 1 | /* Redis benchmark utility. |
2 | * | |
12d090d2 | 3 | * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com> |
ed9b544e | 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 | ||
5f5b9840 | 31 | #include "fmacros.h" |
32 | ||
ed9b544e | 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 "anet.h" | |
44 | #include "sds.h" | |
45 | #include "adlist.h" | |
46 | #include "zmalloc.h" | |
47 | ||
48 | #define REPLY_INT 0 | |
49 | #define REPLY_RETCODE 1 | |
50 | #define REPLY_BULK 2 | |
6c4e61b3 | 51 | #define REPLY_MBULK 3 |
ed9b544e | 52 | |
53 | #define CLIENT_CONNECTING 0 | |
54 | #define CLIENT_SENDQUERY 1 | |
55 | #define CLIENT_READREPLY 2 | |
56 | ||
57 | #define MAX_LATENCY 5000 | |
58 | ||
59 | #define REDIS_NOTUSED(V) ((void) V) | |
60 | ||
61 | static struct config { | |
58cd7103 | 62 | int debug; |
ed9b544e | 63 | int numclients; |
64 | int requests; | |
65 | int liveclients; | |
66 | int donerequests; | |
67 | int keysize; | |
68 | int datasize; | |
57172ffb | 69 | int randomkeys; |
ecfaf6da | 70 | int randomkeys_keyspacelen; |
ed9b544e | 71 | aeEventLoop *el; |
72 | char *hostip; | |
73 | int hostport; | |
c61e6925 | 74 | char *hostsocket; |
ed9b544e | 75 | int keepalive; |
76 | long long start; | |
77 | long long totlatency; | |
78 | int *latency; | |
ed0dd554 | 79 | char *title; |
ed9b544e | 80 | list *clients; |
81 | int quiet; | |
82 | int loop; | |
266373b2 | 83 | int idlemode; |
ed9b544e | 84 | } config; |
85 | ||
86 | typedef struct _client { | |
87 | int state; | |
88 | int fd; | |
89 | sds obuf; | |
90 | sds ibuf; | |
2fd30952 | 91 | int mbulk; /* Number of elements in an mbulk reply */ |
ed9b544e | 92 | int readlen; /* readlen == -1 means read a single line */ |
58cd7103 | 93 | int totreceived; |
ed9b544e | 94 | unsigned int written; /* bytes of 'obuf' already written */ |
95 | int replytype; | |
96 | long long start; /* start time in milliseconds */ | |
97 | } *client; | |
98 | ||
99 | /* Prototypes */ | |
100 | static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask); | |
101 | static void createMissingClients(client c); | |
102 | ||
103 | /* Implementation */ | |
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 | ||
117 | aeDeleteFileEvent(config.el,c->fd,AE_WRITABLE); | |
118 | aeDeleteFileEvent(config.el,c->fd,AE_READABLE); | |
119 | sdsfree(c->ibuf); | |
120 | sdsfree(c->obuf); | |
121 | close(c->fd); | |
122 | zfree(c); | |
123 | config.liveclients--; | |
124 | ln = listSearchKey(config.clients,c); | |
125 | assert(ln != NULL); | |
126 | listDelNode(config.clients,ln); | |
127 | } | |
128 | ||
129 | static void freeAllClients(void) { | |
130 | listNode *ln = config.clients->head, *next; | |
131 | ||
132 | while(ln) { | |
133 | next = ln->next; | |
134 | freeClient(ln->value); | |
135 | ln = next; | |
136 | } | |
137 | } | |
138 | ||
139 | static void resetClient(client c) { | |
140 | aeDeleteFileEvent(config.el,c->fd,AE_WRITABLE); | |
141 | aeDeleteFileEvent(config.el,c->fd,AE_READABLE); | |
266373b2 | 142 | aeCreateFileEvent(config.el,c->fd, AE_WRITABLE,writeHandler,c); |
ed9b544e | 143 | sdsfree(c->ibuf); |
144 | c->ibuf = sdsempty(); | |
89ee361e | 145 | c->readlen = (c->replytype == REPLY_BULK || |
146 | c->replytype == REPLY_MBULK) ? -1 : 0; | |
2fd30952 | 147 | c->mbulk = -1; |
ed9b544e | 148 | c->written = 0; |
58cd7103 | 149 | c->totreceived = 0; |
ed9b544e | 150 | c->state = CLIENT_SENDQUERY; |
151 | c->start = mstime(); | |
74077975 | 152 | createMissingClients(c); |
ed9b544e | 153 | } |
154 | ||
ecfaf6da | 155 | static void randomizeClientKey(client c) { |
156 | char *p; | |
157 | char buf[32]; | |
158 | long r; | |
159 | ||
160 | p = strstr(c->obuf, "_rand"); | |
161 | if (!p) return; | |
162 | p += 5; | |
163 | r = random() % config.randomkeys_keyspacelen; | |
164 | sprintf(buf,"%ld",r); | |
165 | memcpy(p,buf,strlen(buf)); | |
166 | } | |
167 | ||
2fd30952 | 168 | static void prepareClientForReply(client c, int type) { |
169 | if (type == REPLY_BULK) { | |
170 | c->replytype = REPLY_BULK; | |
171 | c->readlen = -1; | |
172 | } else if (type == REPLY_MBULK) { | |
173 | c->replytype = REPLY_MBULK; | |
174 | c->readlen = -1; | |
175 | c->mbulk = -1; | |
176 | } else { | |
177 | c->replytype = type; | |
178 | c->readlen = 0; | |
179 | } | |
180 | } | |
181 | ||
ed9b544e | 182 | static void clientDone(client c) { |
58cd7103 | 183 | static int last_tot_received = 1; |
184 | ||
ed9b544e | 185 | long long latency; |
186 | config.donerequests ++; | |
187 | latency = mstime() - c->start; | |
188 | if (latency > MAX_LATENCY) latency = MAX_LATENCY; | |
189 | config.latency[latency]++; | |
190 | ||
58cd7103 | 191 | if (config.debug && last_tot_received != c->totreceived) { |
192 | printf("Tot bytes received: %d\n", c->totreceived); | |
193 | last_tot_received = c->totreceived; | |
194 | } | |
ed9b544e | 195 | if (config.donerequests == config.requests) { |
196 | freeClient(c); | |
197 | aeStop(config.el); | |
198 | return; | |
199 | } | |
200 | if (config.keepalive) { | |
201 | resetClient(c); | |
ecfaf6da | 202 | if (config.randomkeys) randomizeClientKey(c); |
ed9b544e | 203 | } else { |
204 | config.liveclients--; | |
205 | createMissingClients(c); | |
206 | config.liveclients++; | |
207 | freeClient(c); | |
208 | } | |
209 | } | |
210 | ||
36babc1e PN |
211 | /* Read a length from the buffer pointed to by *p, store the length in *len, |
212 | * and return the number of bytes that the cursor advanced. */ | |
213 | static int readLen(char *p, int *len) { | |
214 | char *tail = strstr(p,"\r\n"); | |
215 | if (tail == NULL) | |
216 | return 0; | |
217 | *tail = '\0'; | |
218 | *len = atoi(p+1); | |
219 | return tail+2-p; | |
220 | } | |
221 | ||
ed9b544e | 222 | static void readHandler(aeEventLoop *el, int fd, void *privdata, int mask) |
223 | { | |
36babc1e PN |
224 | char buf[1024], *p; |
225 | int nread, pos=0, len=0; | |
ed9b544e | 226 | client c = privdata; |
227 | REDIS_NOTUSED(el); | |
228 | REDIS_NOTUSED(fd); | |
229 | REDIS_NOTUSED(mask); | |
230 | ||
36babc1e | 231 | nread = read(c->fd,buf,sizeof(buf)); |
ed9b544e | 232 | if (nread == -1) { |
233 | fprintf(stderr, "Reading from socket: %s\n", strerror(errno)); | |
234 | freeClient(c); | |
235 | return; | |
236 | } | |
237 | if (nread == 0) { | |
238 | fprintf(stderr, "EOF from client\n"); | |
239 | freeClient(c); | |
240 | return; | |
241 | } | |
58cd7103 | 242 | c->totreceived += nread; |
ed9b544e | 243 | c->ibuf = sdscatlen(c->ibuf,buf,nread); |
36babc1e | 244 | len = sdslen(c->ibuf); |
ed9b544e | 245 | |
246 | if (c->replytype == REPLY_INT || | |
36babc1e PN |
247 | c->replytype == REPLY_RETCODE) |
248 | { | |
249 | /* Check if the first line is complete. This is everything we need | |
250 | * when waiting for an integer or status code reply.*/ | |
251 | if ((p = strstr(c->ibuf,"\r\n")) != NULL) | |
252 | goto done; | |
253 | } else if (c->replytype == REPLY_BULK) { | |
254 | int advance = 0; | |
255 | if (c->readlen < 0) { | |
256 | advance = readLen(c->ibuf+pos,&c->readlen); | |
257 | if (advance) { | |
258 | pos += advance; | |
259 | if (c->readlen == -1) { | |
260 | goto done; | |
261 | } else { | |
262 | /* include the trailing \r\n */ | |
263 | c->readlen += 2; | |
2fd30952 | 264 | } |
ed9b544e | 265 | } else { |
36babc1e | 266 | goto skip; |
ed9b544e | 267 | } |
268 | } | |
36babc1e PN |
269 | |
270 | int canconsume; | |
271 | if (c->readlen > 0) { | |
272 | canconsume = c->readlen > (len-pos) ? (len-pos) : c->readlen; | |
273 | c->readlen -= canconsume; | |
274 | pos += canconsume; | |
275 | } | |
276 | ||
277 | if (c->readlen == 0) | |
278 | goto done; | |
279 | } else if (c->replytype == REPLY_MBULK) { | |
280 | int advance = 0; | |
281 | if (c->mbulk == -1) { | |
282 | advance = readLen(c->ibuf+pos,&c->mbulk); | |
283 | if (advance) { | |
284 | pos += advance; | |
285 | if (c->mbulk == -1) | |
286 | goto done; | |
287 | } else { | |
288 | goto skip; | |
289 | } | |
290 | } | |
291 | ||
292 | int canconsume; | |
293 | while(c->mbulk > 0 && pos < len) { | |
294 | if (c->readlen > 0) { | |
295 | canconsume = c->readlen > (len-pos) ? (len-pos) : c->readlen; | |
296 | c->readlen -= canconsume; | |
297 | pos += canconsume; | |
298 | if (c->readlen == 0) | |
299 | c->mbulk--; | |
2fd30952 | 300 | } else { |
36babc1e PN |
301 | advance = readLen(c->ibuf+pos,&c->readlen); |
302 | if (advance) { | |
303 | pos += advance; | |
304 | if (c->readlen == -1) { | |
305 | c->mbulk--; | |
306 | continue; | |
307 | } else { | |
308 | /* include the trailing \r\n */ | |
309 | c->readlen += 2; | |
310 | } | |
311 | } else { | |
312 | goto skip; | |
313 | } | |
2fd30952 | 314 | } |
315 | } | |
36babc1e PN |
316 | |
317 | if (c->mbulk == 0) | |
318 | goto done; | |
2fd30952 | 319 | } |
36babc1e PN |
320 | |
321 | skip: | |
322 | c->ibuf = sdsrange(c->ibuf,pos,-1); | |
323 | return; | |
324 | done: | |
325 | clientDone(c); | |
326 | return; | |
ed9b544e | 327 | } |
328 | ||
329 | static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask) | |
330 | { | |
331 | client c = privdata; | |
332 | REDIS_NOTUSED(el); | |
333 | REDIS_NOTUSED(fd); | |
334 | REDIS_NOTUSED(mask); | |
335 | ||
336 | if (c->state == CLIENT_CONNECTING) { | |
337 | c->state = CLIENT_SENDQUERY; | |
338 | c->start = mstime(); | |
339 | } | |
340 | if (sdslen(c->obuf) > c->written) { | |
341 | void *ptr = c->obuf+c->written; | |
342 | int len = sdslen(c->obuf) - c->written; | |
343 | int nwritten = write(c->fd, ptr, len); | |
344 | if (nwritten == -1) { | |
61c47ecd | 345 | if (errno != EPIPE) |
346 | fprintf(stderr, "Writing to socket: %s\n", strerror(errno)); | |
ed9b544e | 347 | freeClient(c); |
348 | return; | |
349 | } | |
350 | c->written += nwritten; | |
351 | if (sdslen(c->obuf) == c->written) { | |
352 | aeDeleteFileEvent(config.el,c->fd,AE_WRITABLE); | |
266373b2 | 353 | aeCreateFileEvent(config.el,c->fd,AE_READABLE,readHandler,c); |
ed9b544e | 354 | c->state = CLIENT_READREPLY; |
355 | } | |
356 | } | |
357 | } | |
358 | ||
359 | static client createClient(void) { | |
360 | client c = zmalloc(sizeof(struct _client)); | |
361 | char err[ANET_ERR_LEN]; | |
362 | ||
c61e6925 PN |
363 | if (config.hostsocket == NULL) |
364 | c->fd = anetTcpNonBlockConnect(err,config.hostip,config.hostport); | |
365 | else | |
366 | c->fd = anetUnixNonBlockConnect(err,config.hostsocket); | |
367 | ||
ed9b544e | 368 | if (c->fd == ANET_ERR) { |
369 | zfree(c); | |
370 | fprintf(stderr,"Connect: %s\n",err); | |
371 | return NULL; | |
372 | } | |
373 | anetTcpNoDelay(NULL,c->fd); | |
374 | c->obuf = sdsempty(); | |
375 | c->ibuf = sdsempty(); | |
2fd30952 | 376 | c->mbulk = -1; |
ed9b544e | 377 | c->readlen = 0; |
378 | c->written = 0; | |
58cd7103 | 379 | c->totreceived = 0; |
ed9b544e | 380 | c->state = CLIENT_CONNECTING; |
266373b2 | 381 | aeCreateFileEvent(config.el, c->fd, AE_WRITABLE, writeHandler, c); |
ed9b544e | 382 | config.liveclients++; |
383 | listAddNodeTail(config.clients,c); | |
384 | return c; | |
385 | } | |
386 | ||
387 | static void createMissingClients(client c) { | |
388 | while(config.liveclients < config.numclients) { | |
389 | client new = createClient(); | |
390 | if (!new) continue; | |
391 | sdsfree(new->obuf); | |
392 | new->obuf = sdsdup(c->obuf); | |
ecfaf6da | 393 | if (config.randomkeys) randomizeClientKey(c); |
b892cabe | 394 | prepareClientForReply(new,c->replytype); |
ed9b544e | 395 | } |
396 | } | |
397 | ||
ed0dd554 | 398 | static void showLatencyReport(void) { |
ed9b544e | 399 | int j, seen = 0; |
400 | float perc, reqpersec; | |
401 | ||
402 | reqpersec = (float)config.donerequests/((float)config.totlatency/1000); | |
403 | if (!config.quiet) { | |
ed0dd554 | 404 | printf("====== %s ======\n", config.title); |
ed9b544e | 405 | printf(" %d requests completed in %.2f seconds\n", config.donerequests, |
406 | (float)config.totlatency/1000); | |
407 | printf(" %d parallel clients\n", config.numclients); | |
408 | printf(" %d bytes payload\n", config.datasize); | |
409 | printf(" keep alive: %d\n", config.keepalive); | |
410 | printf("\n"); | |
411 | for (j = 0; j <= MAX_LATENCY; j++) { | |
412 | if (config.latency[j]) { | |
413 | seen += config.latency[j]; | |
414 | perc = ((float)seen*100)/config.donerequests; | |
415 | printf("%.2f%% <= %d milliseconds\n", perc, j); | |
416 | } | |
417 | } | |
418 | printf("%.2f requests per second\n\n", reqpersec); | |
419 | } else { | |
ed0dd554 | 420 | printf("%s: %.2f requests per second\n", config.title, reqpersec); |
ed9b544e | 421 | } |
422 | } | |
423 | ||
ed0dd554 | 424 | static void prepareForBenchmark(char *title) { |
ed9b544e | 425 | memset(config.latency,0,sizeof(int)*(MAX_LATENCY+1)); |
ed0dd554 | 426 | config.title = title; |
ed9b544e | 427 | config.start = mstime(); |
428 | config.donerequests = 0; | |
429 | } | |
430 | ||
ed0dd554 | 431 | static void endBenchmark(void) { |
ed9b544e | 432 | config.totlatency = mstime()-config.start; |
ed0dd554 | 433 | showLatencyReport(); |
ed9b544e | 434 | freeAllClients(); |
435 | } | |
436 | ||
437 | void parseOptions(int argc, char **argv) { | |
438 | int i; | |
439 | ||
440 | for (i = 1; i < argc; i++) { | |
441 | int lastarg = i==argc-1; | |
442 | ||
443 | if (!strcmp(argv[i],"-c") && !lastarg) { | |
444 | config.numclients = atoi(argv[i+1]); | |
445 | i++; | |
446 | } else if (!strcmp(argv[i],"-n") && !lastarg) { | |
447 | config.requests = atoi(argv[i+1]); | |
448 | i++; | |
449 | } else if (!strcmp(argv[i],"-k") && !lastarg) { | |
450 | config.keepalive = atoi(argv[i+1]); | |
451 | i++; | |
452 | } else if (!strcmp(argv[i],"-h") && !lastarg) { | |
453 | char *ip = zmalloc(32); | |
454 | if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) { | |
455 | printf("Can't resolve %s\n", argv[i]); | |
456 | exit(1); | |
457 | } | |
458 | config.hostip = ip; | |
459 | i++; | |
460 | } else if (!strcmp(argv[i],"-p") && !lastarg) { | |
461 | config.hostport = atoi(argv[i+1]); | |
462 | i++; | |
c61e6925 PN |
463 | } else if (!strcmp(argv[i],"-s") && !lastarg) { |
464 | config.hostsocket = argv[i+1]; | |
465 | i++; | |
ed9b544e | 466 | } else if (!strcmp(argv[i],"-d") && !lastarg) { |
467 | config.datasize = atoi(argv[i+1]); | |
468 | i++; | |
469 | if (config.datasize < 1) config.datasize=1; | |
470 | if (config.datasize > 1024*1024) config.datasize = 1024*1024; | |
ecfaf6da | 471 | } else if (!strcmp(argv[i],"-r") && !lastarg) { |
57172ffb | 472 | config.randomkeys = 1; |
ecfaf6da | 473 | config.randomkeys_keyspacelen = atoi(argv[i+1]); |
474 | if (config.randomkeys_keyspacelen < 0) | |
475 | config.randomkeys_keyspacelen = 0; | |
476 | i++; | |
ed9b544e | 477 | } else if (!strcmp(argv[i],"-q")) { |
478 | config.quiet = 1; | |
479 | } else if (!strcmp(argv[i],"-l")) { | |
480 | config.loop = 1; | |
58cd7103 | 481 | } else if (!strcmp(argv[i],"-D")) { |
482 | config.debug = 1; | |
266373b2 | 483 | } else if (!strcmp(argv[i],"-I")) { |
484 | config.idlemode = 1; | |
ed9b544e | 485 | } else { |
486 | printf("Wrong option '%s' or option argument missing\n\n",argv[i]); | |
487 | printf("Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]\n\n"); | |
488 | printf(" -h <hostname> Server hostname (default 127.0.0.1)\n"); | |
c61e6925 PN |
489 | printf(" -p <port> Server port (default 6379)\n"); |
490 | printf(" -s <socket> Server socket (overrides host and port)\n"); | |
ed9b544e | 491 | printf(" -c <clients> Number of parallel connections (default 50)\n"); |
492 | printf(" -n <requests> Total number of requests (default 10000)\n"); | |
493 | printf(" -d <size> Data size of SET/GET value in bytes (default 2)\n"); | |
494 | printf(" -k <boolean> 1=keep alive 0=reconnect (default 1)\n"); | |
b1ad58ed | 495 | printf(" -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD\n"); |
ecfaf6da | 496 | printf(" Using this option the benchmark will get/set keys\n"); |
497 | printf(" in the form mykey_rand000000012456 instead of constant\n"); | |
498 | printf(" keys, the <keyspacelen> argument determines the max\n"); | |
499 | printf(" number of values for the random number. For instance\n"); | |
500 | printf(" if set to 10 only rand000000000000 - rand000000000009\n"); | |
501 | printf(" range will be allowed.\n"); | |
ed9b544e | 502 | printf(" -q Quiet. Just show query/sec values\n"); |
503 | printf(" -l Loop. Run the tests forever\n"); | |
266373b2 | 504 | printf(" -I Idle mode. Just open N idle connections and wait.\n"); |
58cd7103 | 505 | printf(" -D Debug mode. more verbose.\n"); |
ed9b544e | 506 | exit(1); |
507 | } | |
508 | } | |
509 | } | |
510 | ||
ed0dd554 PN |
511 | int showThroughput(struct aeEventLoop *eventLoop, long long id, void *clientData) { |
512 | REDIS_NOTUSED(eventLoop); | |
513 | REDIS_NOTUSED(id); | |
514 | REDIS_NOTUSED(clientData); | |
515 | ||
516 | float dt = (float)(mstime()-config.start)/1000.0; | |
517 | float rps = (float)config.donerequests/dt; | |
518 | printf("%s: %.2f\r", config.title, rps); | |
519 | fflush(stdout); | |
520 | return 250; /* every 250ms */ | |
521 | } | |
522 | ||
ed9b544e | 523 | int main(int argc, char **argv) { |
524 | client c; | |
525 | ||
526 | signal(SIGHUP, SIG_IGN); | |
527 | signal(SIGPIPE, SIG_IGN); | |
528 | ||
58cd7103 | 529 | config.debug = 0; |
ed9b544e | 530 | config.numclients = 50; |
531 | config.requests = 10000; | |
532 | config.liveclients = 0; | |
533 | config.el = aeCreateEventLoop(); | |
ed0dd554 | 534 | aeCreateTimeEvent(config.el,1,showThroughput,NULL,NULL); |
ed9b544e | 535 | config.keepalive = 1; |
536 | config.donerequests = 0; | |
537 | config.datasize = 3; | |
57172ffb | 538 | config.randomkeys = 0; |
ecfaf6da | 539 | config.randomkeys_keyspacelen = 0; |
ed9b544e | 540 | config.quiet = 0; |
541 | config.loop = 0; | |
266373b2 | 542 | config.idlemode = 0; |
ed9b544e | 543 | config.latency = NULL; |
544 | config.clients = listCreate(); | |
545 | config.latency = zmalloc(sizeof(int)*(MAX_LATENCY+1)); | |
546 | ||
547 | config.hostip = "127.0.0.1"; | |
548 | config.hostport = 6379; | |
c61e6925 | 549 | config.hostsocket = NULL; |
ed9b544e | 550 | |
551 | parseOptions(argc,argv); | |
552 | ||
553 | if (config.keepalive == 0) { | |
c3251497 | 554 | 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"); |
ed9b544e | 555 | } |
556 | ||
266373b2 | 557 | if (config.idlemode) { |
558 | printf("Creating %d idle connections and waiting forever (Ctrl+C when done)\n", config.numclients); | |
ed0dd554 | 559 | prepareForBenchmark("IDLE"); |
266373b2 | 560 | c = createClient(); |
561 | if (!c) exit(1); | |
562 | c->obuf = sdsempty(); | |
563 | prepareClientForReply(c,REPLY_RETCODE); /* will never receive it */ | |
564 | createMissingClients(c); | |
565 | aeMain(config.el); | |
566 | /* and will wait for every */ | |
567 | } | |
568 | ||
ed9b544e | 569 | do { |
ed0dd554 | 570 | prepareForBenchmark("PING"); |
6766f45e | 571 | c = createClient(); |
572 | if (!c) exit(1); | |
573 | c->obuf = sdscat(c->obuf,"PING\r\n"); | |
574 | prepareClientForReply(c,REPLY_RETCODE); | |
575 | createMissingClients(c); | |
576 | aeMain(config.el); | |
ed0dd554 | 577 | endBenchmark(); |
6766f45e | 578 | |
ed0dd554 | 579 | prepareForBenchmark("PING (multi bulk)"); |
6766f45e | 580 | c = createClient(); |
581 | if (!c) exit(1); | |
582 | c->obuf = sdscat(c->obuf,"*1\r\n$4\r\nPING\r\n"); | |
583 | prepareClientForReply(c,REPLY_RETCODE); | |
584 | createMissingClients(c); | |
585 | aeMain(config.el); | |
ed0dd554 | 586 | endBenchmark(); |
6766f45e | 587 | |
ea5b7092 PN |
588 | prepareForBenchmark("MSET (10 keys, multi bulk)"); |
589 | c = createClient(); | |
590 | if (!c) exit(1); | |
591 | c->obuf = sdscatprintf(c->obuf,"*%d\r\n$4\r\nMSET\r\n", 11); | |
592 | { | |
593 | int i; | |
594 | char *data = zmalloc(config.datasize+2); | |
595 | memset(data,'x',config.datasize); | |
596 | for (i = 0; i < 10; i++) { | |
597 | c->obuf = sdscatprintf(c->obuf,"$%d\r\n%s\r\n",config.datasize,data); | |
598 | } | |
599 | zfree(data); | |
600 | } | |
601 | prepareClientForReply(c,REPLY_RETCODE); | |
602 | createMissingClients(c); | |
603 | aeMain(config.el); | |
604 | endBenchmark(); | |
605 | ||
ed0dd554 | 606 | prepareForBenchmark("SET"); |
ed9b544e | 607 | c = createClient(); |
608 | if (!c) exit(1); | |
1aa608fc | 609 | c->obuf = sdscat(c->obuf,"SET foo_rand000000000000 "); |
ed9b544e | 610 | { |
611 | char *data = zmalloc(config.datasize+2); | |
612 | memset(data,'x',config.datasize); | |
613 | data[config.datasize] = '\r'; | |
614 | data[config.datasize+1] = '\n'; | |
615 | c->obuf = sdscatlen(c->obuf,data,config.datasize+2); | |
616 | } | |
2fd30952 | 617 | prepareClientForReply(c,REPLY_RETCODE); |
ed9b544e | 618 | createMissingClients(c); |
619 | aeMain(config.el); | |
ed0dd554 | 620 | endBenchmark(); |
ed9b544e | 621 | |
ed0dd554 | 622 | prepareForBenchmark("GET"); |
ed9b544e | 623 | c = createClient(); |
624 | if (!c) exit(1); | |
57172ffb | 625 | c->obuf = sdscat(c->obuf,"GET foo_rand000000000000\r\n"); |
2fd30952 | 626 | prepareClientForReply(c,REPLY_BULK); |
ed9b544e | 627 | createMissingClients(c); |
628 | aeMain(config.el); | |
ed0dd554 | 629 | endBenchmark(); |
ed9b544e | 630 | |
ed0dd554 | 631 | prepareForBenchmark("INCR"); |
ed9b544e | 632 | c = createClient(); |
633 | if (!c) exit(1); | |
57172ffb | 634 | c->obuf = sdscat(c->obuf,"INCR counter_rand000000000000\r\n"); |
2fd30952 | 635 | prepareClientForReply(c,REPLY_INT); |
ed9b544e | 636 | createMissingClients(c); |
637 | aeMain(config.el); | |
ed0dd554 | 638 | endBenchmark(); |
ed9b544e | 639 | |
ed0dd554 | 640 | prepareForBenchmark("LPUSH"); |
ed9b544e | 641 | c = createClient(); |
642 | if (!c) exit(1); | |
1aa608fc | 643 | c->obuf = sdscat(c->obuf,"LPUSH mylist bar\r\n"); |
2fd30952 | 644 | prepareClientForReply(c,REPLY_INT); |
ed9b544e | 645 | createMissingClients(c); |
646 | aeMain(config.el); | |
ed0dd554 | 647 | endBenchmark(); |
ed9b544e | 648 | |
ed0dd554 | 649 | prepareForBenchmark("LPOP"); |
ed9b544e | 650 | c = createClient(); |
651 | if (!c) exit(1); | |
652 | c->obuf = sdscat(c->obuf,"LPOP mylist\r\n"); | |
2fd30952 | 653 | prepareClientForReply(c,REPLY_BULK); |
ed9b544e | 654 | createMissingClients(c); |
655 | aeMain(config.el); | |
ed0dd554 | 656 | endBenchmark(); |
ed9b544e | 657 | |
ed0dd554 | 658 | prepareForBenchmark("SADD"); |
b1ad58ed | 659 | c = createClient(); |
660 | if (!c) exit(1); | |
1aa608fc | 661 | c->obuf = sdscat(c->obuf,"SADD myset counter_rand000000000000\r\n"); |
b1ad58ed | 662 | prepareClientForReply(c,REPLY_RETCODE); |
663 | createMissingClients(c); | |
664 | aeMain(config.el); | |
ed0dd554 | 665 | endBenchmark(); |
b1ad58ed | 666 | |
ed0dd554 | 667 | prepareForBenchmark("SPOP"); |
b1ad58ed | 668 | c = createClient(); |
669 | if (!c) exit(1); | |
670 | c->obuf = sdscat(c->obuf,"SPOP myset\r\n"); | |
671 | prepareClientForReply(c,REPLY_BULK); | |
672 | createMissingClients(c); | |
673 | aeMain(config.el); | |
ed0dd554 | 674 | endBenchmark(); |
b1ad58ed | 675 | |
ed0dd554 | 676 | prepareForBenchmark("LPUSH (again, in order to bench LRANGE)"); |
2fd30952 | 677 | c = createClient(); |
678 | if (!c) exit(1); | |
1aa608fc | 679 | c->obuf = sdscat(c->obuf,"LPUSH mylist bar\r\n"); |
2fd30952 | 680 | prepareClientForReply(c,REPLY_RETCODE); |
681 | createMissingClients(c); | |
682 | aeMain(config.el); | |
ed0dd554 | 683 | endBenchmark(); |
2fd30952 | 684 | |
ed0dd554 | 685 | prepareForBenchmark("LRANGE (first 100 elements)"); |
2fd30952 | 686 | c = createClient(); |
687 | if (!c) exit(1); | |
688 | c->obuf = sdscat(c->obuf,"LRANGE mylist 0 99\r\n"); | |
689 | prepareClientForReply(c,REPLY_MBULK); | |
690 | createMissingClients(c); | |
691 | aeMain(config.el); | |
ed0dd554 | 692 | endBenchmark(); |
2fd30952 | 693 | |
ed0dd554 | 694 | prepareForBenchmark("LRANGE (first 300 elements)"); |
ccb5332c | 695 | c = createClient(); |
696 | if (!c) exit(1); | |
697 | c->obuf = sdscat(c->obuf,"LRANGE mylist 0 299\r\n"); | |
698 | prepareClientForReply(c,REPLY_MBULK); | |
699 | createMissingClients(c); | |
700 | aeMain(config.el); | |
ed0dd554 | 701 | endBenchmark(); |
ccb5332c | 702 | |
ed0dd554 | 703 | prepareForBenchmark("LRANGE (first 450 elements)"); |
cc30e368 | 704 | c = createClient(); |
705 | if (!c) exit(1); | |
706 | c->obuf = sdscat(c->obuf,"LRANGE mylist 0 449\r\n"); | |
707 | prepareClientForReply(c,REPLY_MBULK); | |
708 | createMissingClients(c); | |
709 | aeMain(config.el); | |
ed0dd554 | 710 | endBenchmark(); |
cc30e368 | 711 | |
ed0dd554 | 712 | prepareForBenchmark("LRANGE (first 600 elements)"); |
cc30e368 | 713 | c = createClient(); |
714 | if (!c) exit(1); | |
715 | c->obuf = sdscat(c->obuf,"LRANGE mylist 0 599\r\n"); | |
716 | prepareClientForReply(c,REPLY_MBULK); | |
717 | createMissingClients(c); | |
718 | aeMain(config.el); | |
ed0dd554 | 719 | endBenchmark(); |
cc30e368 | 720 | |
ed9b544e | 721 | printf("\n"); |
722 | } while(config.loop); | |
723 | ||
724 | return 0; | |
725 | } |