]>
Commit | Line | Data |
---|---|---|
ed9b544e | 1 | /* Redis benchmark utility. |
2 | * | |
3 | * Copyright (c) 2006-2009, 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 | ||
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 | |
51 | ||
52 | #define CLIENT_CONNECTING 0 | |
53 | #define CLIENT_SENDQUERY 1 | |
54 | #define CLIENT_READREPLY 2 | |
55 | ||
56 | #define MAX_LATENCY 5000 | |
57 | ||
58 | #define REDIS_NOTUSED(V) ((void) V) | |
59 | ||
60 | static struct config { | |
61 | int numclients; | |
62 | int requests; | |
63 | int liveclients; | |
64 | int donerequests; | |
65 | int keysize; | |
66 | int datasize; | |
57172ffb | 67 | int randomkeys; |
ecfaf6da | 68 | int randomkeys_keyspacelen; |
ed9b544e | 69 | aeEventLoop *el; |
70 | char *hostip; | |
71 | int hostport; | |
72 | int keepalive; | |
73 | long long start; | |
74 | long long totlatency; | |
75 | int *latency; | |
76 | list *clients; | |
77 | int quiet; | |
78 | int loop; | |
79 | } config; | |
80 | ||
81 | typedef struct _client { | |
82 | int state; | |
83 | int fd; | |
84 | sds obuf; | |
85 | sds ibuf; | |
86 | int readlen; /* readlen == -1 means read a single line */ | |
87 | unsigned int written; /* bytes of 'obuf' already written */ | |
88 | int replytype; | |
89 | long long start; /* start time in milliseconds */ | |
90 | } *client; | |
91 | ||
92 | /* Prototypes */ | |
93 | static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask); | |
94 | static void createMissingClients(client c); | |
95 | ||
96 | /* Implementation */ | |
97 | static long long mstime(void) { | |
98 | struct timeval tv; | |
99 | long long mst; | |
100 | ||
101 | gettimeofday(&tv, NULL); | |
102 | mst = ((long)tv.tv_sec)*1000; | |
103 | mst += tv.tv_usec/1000; | |
104 | return mst; | |
105 | } | |
106 | ||
107 | static void freeClient(client c) { | |
108 | listNode *ln; | |
109 | ||
110 | aeDeleteFileEvent(config.el,c->fd,AE_WRITABLE); | |
111 | aeDeleteFileEvent(config.el,c->fd,AE_READABLE); | |
112 | sdsfree(c->ibuf); | |
113 | sdsfree(c->obuf); | |
114 | close(c->fd); | |
115 | zfree(c); | |
116 | config.liveclients--; | |
117 | ln = listSearchKey(config.clients,c); | |
118 | assert(ln != NULL); | |
119 | listDelNode(config.clients,ln); | |
120 | } | |
121 | ||
122 | static void freeAllClients(void) { | |
123 | listNode *ln = config.clients->head, *next; | |
124 | ||
125 | while(ln) { | |
126 | next = ln->next; | |
127 | freeClient(ln->value); | |
128 | ln = next; | |
129 | } | |
130 | } | |
131 | ||
132 | static void resetClient(client c) { | |
133 | aeDeleteFileEvent(config.el,c->fd,AE_WRITABLE); | |
134 | aeDeleteFileEvent(config.el,c->fd,AE_READABLE); | |
135 | aeCreateFileEvent(config.el,c->fd, AE_WRITABLE,writeHandler,c,NULL); | |
136 | sdsfree(c->ibuf); | |
137 | c->ibuf = sdsempty(); | |
138 | c->readlen = (c->replytype == REPLY_BULK) ? -1 : 0; | |
139 | c->written = 0; | |
140 | c->state = CLIENT_SENDQUERY; | |
141 | c->start = mstime(); | |
74077975 | 142 | createMissingClients(c); |
ed9b544e | 143 | } |
144 | ||
ecfaf6da | 145 | static void randomizeClientKey(client c) { |
146 | char *p; | |
147 | char buf[32]; | |
148 | long r; | |
149 | ||
150 | p = strstr(c->obuf, "_rand"); | |
151 | if (!p) return; | |
152 | p += 5; | |
153 | r = random() % config.randomkeys_keyspacelen; | |
154 | sprintf(buf,"%ld",r); | |
155 | memcpy(p,buf,strlen(buf)); | |
156 | } | |
157 | ||
ed9b544e | 158 | static void clientDone(client c) { |
159 | long long latency; | |
160 | config.donerequests ++; | |
161 | latency = mstime() - c->start; | |
162 | if (latency > MAX_LATENCY) latency = MAX_LATENCY; | |
163 | config.latency[latency]++; | |
164 | ||
165 | if (config.donerequests == config.requests) { | |
166 | freeClient(c); | |
167 | aeStop(config.el); | |
168 | return; | |
169 | } | |
170 | if (config.keepalive) { | |
171 | resetClient(c); | |
ecfaf6da | 172 | if (config.randomkeys) randomizeClientKey(c); |
ed9b544e | 173 | } else { |
174 | config.liveclients--; | |
175 | createMissingClients(c); | |
176 | config.liveclients++; | |
177 | freeClient(c); | |
178 | } | |
179 | } | |
180 | ||
181 | static void readHandler(aeEventLoop *el, int fd, void *privdata, int mask) | |
182 | { | |
183 | char buf[1024]; | |
184 | int nread; | |
185 | client c = privdata; | |
186 | REDIS_NOTUSED(el); | |
187 | REDIS_NOTUSED(fd); | |
188 | REDIS_NOTUSED(mask); | |
189 | ||
190 | nread = read(c->fd, buf, 1024); | |
191 | if (nread == -1) { | |
192 | fprintf(stderr, "Reading from socket: %s\n", strerror(errno)); | |
193 | freeClient(c); | |
194 | return; | |
195 | } | |
196 | if (nread == 0) { | |
197 | fprintf(stderr, "EOF from client\n"); | |
198 | freeClient(c); | |
199 | return; | |
200 | } | |
201 | c->ibuf = sdscatlen(c->ibuf,buf,nread); | |
202 | ||
203 | if (c->replytype == REPLY_INT || | |
204 | c->replytype == REPLY_RETCODE || | |
205 | (c->replytype == REPLY_BULK && c->readlen == -1)) { | |
206 | char *p; | |
207 | ||
208 | if ((p = strchr(c->ibuf,'\n')) != NULL) { | |
209 | if (c->replytype == REPLY_BULK) { | |
210 | *p = '\0'; | |
211 | *(p-1) = '\0'; | |
4c26a79a | 212 | c->readlen = atoi(c->ibuf+1)+2; |
ecfaf6da | 213 | if (c->readlen-2 == -1) { |
ed9b544e | 214 | clientDone(c); |
215 | return; | |
216 | } | |
ed9b544e | 217 | c->ibuf = sdsrange(c->ibuf,(p-c->ibuf)+1,-1); |
218 | } else { | |
219 | c->ibuf = sdstrim(c->ibuf,"\r\n"); | |
220 | clientDone(c); | |
221 | return; | |
222 | } | |
223 | } | |
224 | } | |
225 | /* bulk read */ | |
226 | if ((unsigned)c->readlen == sdslen(c->ibuf)) | |
227 | clientDone(c); | |
228 | } | |
229 | ||
230 | static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask) | |
231 | { | |
232 | client c = privdata; | |
233 | REDIS_NOTUSED(el); | |
234 | REDIS_NOTUSED(fd); | |
235 | REDIS_NOTUSED(mask); | |
236 | ||
237 | if (c->state == CLIENT_CONNECTING) { | |
238 | c->state = CLIENT_SENDQUERY; | |
239 | c->start = mstime(); | |
240 | } | |
241 | if (sdslen(c->obuf) > c->written) { | |
242 | void *ptr = c->obuf+c->written; | |
243 | int len = sdslen(c->obuf) - c->written; | |
244 | int nwritten = write(c->fd, ptr, len); | |
245 | if (nwritten == -1) { | |
246 | fprintf(stderr, "Writing to socket: %s\n", strerror(errno)); | |
247 | freeClient(c); | |
248 | return; | |
249 | } | |
250 | c->written += nwritten; | |
251 | if (sdslen(c->obuf) == c->written) { | |
252 | aeDeleteFileEvent(config.el,c->fd,AE_WRITABLE); | |
253 | aeCreateFileEvent(config.el,c->fd,AE_READABLE,readHandler,c,NULL); | |
254 | c->state = CLIENT_READREPLY; | |
255 | } | |
256 | } | |
257 | } | |
258 | ||
259 | static client createClient(void) { | |
260 | client c = zmalloc(sizeof(struct _client)); | |
261 | char err[ANET_ERR_LEN]; | |
262 | ||
263 | c->fd = anetTcpNonBlockConnect(err,config.hostip,config.hostport); | |
264 | if (c->fd == ANET_ERR) { | |
265 | zfree(c); | |
266 | fprintf(stderr,"Connect: %s\n",err); | |
267 | return NULL; | |
268 | } | |
269 | anetTcpNoDelay(NULL,c->fd); | |
270 | c->obuf = sdsempty(); | |
271 | c->ibuf = sdsempty(); | |
272 | c->readlen = 0; | |
273 | c->written = 0; | |
274 | c->state = CLIENT_CONNECTING; | |
275 | aeCreateFileEvent(config.el, c->fd, AE_WRITABLE, writeHandler, c, NULL); | |
276 | config.liveclients++; | |
277 | listAddNodeTail(config.clients,c); | |
278 | return c; | |
279 | } | |
280 | ||
281 | static void createMissingClients(client c) { | |
282 | while(config.liveclients < config.numclients) { | |
283 | client new = createClient(); | |
284 | if (!new) continue; | |
285 | sdsfree(new->obuf); | |
286 | new->obuf = sdsdup(c->obuf); | |
ecfaf6da | 287 | if (config.randomkeys) randomizeClientKey(c); |
ed9b544e | 288 | new->replytype = c->replytype; |
289 | if (c->replytype == REPLY_BULK) | |
290 | new->readlen = -1; | |
291 | } | |
292 | } | |
293 | ||
294 | static void showLatencyReport(char *title) { | |
295 | int j, seen = 0; | |
296 | float perc, reqpersec; | |
297 | ||
298 | reqpersec = (float)config.donerequests/((float)config.totlatency/1000); | |
299 | if (!config.quiet) { | |
300 | printf("====== %s ======\n", title); | |
301 | printf(" %d requests completed in %.2f seconds\n", config.donerequests, | |
302 | (float)config.totlatency/1000); | |
303 | printf(" %d parallel clients\n", config.numclients); | |
304 | printf(" %d bytes payload\n", config.datasize); | |
305 | printf(" keep alive: %d\n", config.keepalive); | |
306 | printf("\n"); | |
307 | for (j = 0; j <= MAX_LATENCY; j++) { | |
308 | if (config.latency[j]) { | |
309 | seen += config.latency[j]; | |
310 | perc = ((float)seen*100)/config.donerequests; | |
311 | printf("%.2f%% <= %d milliseconds\n", perc, j); | |
312 | } | |
313 | } | |
314 | printf("%.2f requests per second\n\n", reqpersec); | |
315 | } else { | |
316 | printf("%s: %.2f requests per second\n", title, reqpersec); | |
317 | } | |
318 | } | |
319 | ||
320 | static void prepareForBenchmark(void) | |
321 | { | |
322 | memset(config.latency,0,sizeof(int)*(MAX_LATENCY+1)); | |
323 | config.start = mstime(); | |
324 | config.donerequests = 0; | |
325 | } | |
326 | ||
327 | static void endBenchmark(char *title) { | |
328 | config.totlatency = mstime()-config.start; | |
329 | showLatencyReport(title); | |
330 | freeAllClients(); | |
331 | } | |
332 | ||
333 | void parseOptions(int argc, char **argv) { | |
334 | int i; | |
335 | ||
336 | for (i = 1; i < argc; i++) { | |
337 | int lastarg = i==argc-1; | |
338 | ||
339 | if (!strcmp(argv[i],"-c") && !lastarg) { | |
340 | config.numclients = atoi(argv[i+1]); | |
341 | i++; | |
342 | } else if (!strcmp(argv[i],"-n") && !lastarg) { | |
343 | config.requests = atoi(argv[i+1]); | |
344 | i++; | |
345 | } else if (!strcmp(argv[i],"-k") && !lastarg) { | |
346 | config.keepalive = atoi(argv[i+1]); | |
347 | i++; | |
348 | } else if (!strcmp(argv[i],"-h") && !lastarg) { | |
349 | char *ip = zmalloc(32); | |
350 | if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) { | |
351 | printf("Can't resolve %s\n", argv[i]); | |
352 | exit(1); | |
353 | } | |
354 | config.hostip = ip; | |
355 | i++; | |
356 | } else if (!strcmp(argv[i],"-p") && !lastarg) { | |
357 | config.hostport = atoi(argv[i+1]); | |
358 | i++; | |
359 | } else if (!strcmp(argv[i],"-d") && !lastarg) { | |
360 | config.datasize = atoi(argv[i+1]); | |
361 | i++; | |
362 | if (config.datasize < 1) config.datasize=1; | |
363 | if (config.datasize > 1024*1024) config.datasize = 1024*1024; | |
ecfaf6da | 364 | } else if (!strcmp(argv[i],"-r") && !lastarg) { |
57172ffb | 365 | config.randomkeys = 1; |
ecfaf6da | 366 | config.randomkeys_keyspacelen = atoi(argv[i+1]); |
367 | if (config.randomkeys_keyspacelen < 0) | |
368 | config.randomkeys_keyspacelen = 0; | |
369 | i++; | |
ed9b544e | 370 | } else if (!strcmp(argv[i],"-q")) { |
371 | config.quiet = 1; | |
372 | } else if (!strcmp(argv[i],"-l")) { | |
373 | config.loop = 1; | |
374 | } else { | |
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 <hostname> Server port (default 6379)\n"); | |
379 | printf(" -c <clients> Number of parallel connections (default 50)\n"); | |
380 | printf(" -n <requests> Total number of requests (default 10000)\n"); | |
381 | printf(" -d <size> Data size of SET/GET value in bytes (default 2)\n"); | |
382 | printf(" -k <boolean> 1=keep alive 0=reconnect (default 1)\n"); | |
ecfaf6da | 383 | printf(" -r <keyspacelen> Use random keys for SET/GET/INCR\n"); |
384 | printf(" Using this option the benchmark will get/set keys\n"); | |
385 | printf(" in the form mykey_rand000000012456 instead of constant\n"); | |
386 | printf(" keys, the <keyspacelen> argument determines the max\n"); | |
387 | printf(" number of values for the random number. For instance\n"); | |
388 | printf(" if set to 10 only rand000000000000 - rand000000000009\n"); | |
389 | printf(" range will be allowed.\n"); | |
ed9b544e | 390 | printf(" -q Quiet. Just show query/sec values\n"); |
391 | printf(" -l Loop. Run the tests forever\n"); | |
392 | exit(1); | |
393 | } | |
394 | } | |
395 | } | |
396 | ||
397 | int main(int argc, char **argv) { | |
398 | client c; | |
399 | ||
400 | signal(SIGHUP, SIG_IGN); | |
401 | signal(SIGPIPE, SIG_IGN); | |
402 | ||
403 | config.numclients = 50; | |
404 | config.requests = 10000; | |
405 | config.liveclients = 0; | |
406 | config.el = aeCreateEventLoop(); | |
407 | config.keepalive = 1; | |
408 | config.donerequests = 0; | |
409 | config.datasize = 3; | |
57172ffb | 410 | config.randomkeys = 0; |
ecfaf6da | 411 | config.randomkeys_keyspacelen = 0; |
ed9b544e | 412 | config.quiet = 0; |
413 | config.loop = 0; | |
414 | config.latency = NULL; | |
415 | config.clients = listCreate(); | |
416 | config.latency = zmalloc(sizeof(int)*(MAX_LATENCY+1)); | |
417 | ||
418 | config.hostip = "127.0.0.1"; | |
419 | config.hostport = 6379; | |
420 | ||
421 | parseOptions(argc,argv); | |
422 | ||
423 | if (config.keepalive == 0) { | |
c3251497 | 424 | 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 | 425 | } |
426 | ||
427 | do { | |
ed9b544e | 428 | prepareForBenchmark(); |
429 | c = createClient(); | |
430 | if (!c) exit(1); | |
57172ffb | 431 | c->obuf = sdscatprintf(c->obuf,"SET foo_rand000000000000 %d\r\n",config.datasize); |
ed9b544e | 432 | { |
433 | char *data = zmalloc(config.datasize+2); | |
434 | memset(data,'x',config.datasize); | |
435 | data[config.datasize] = '\r'; | |
436 | data[config.datasize+1] = '\n'; | |
437 | c->obuf = sdscatlen(c->obuf,data,config.datasize+2); | |
438 | } | |
439 | c->replytype = REPLY_RETCODE; | |
440 | createMissingClients(c); | |
441 | aeMain(config.el); | |
442 | endBenchmark("SET"); | |
443 | ||
444 | prepareForBenchmark(); | |
445 | c = createClient(); | |
446 | if (!c) exit(1); | |
57172ffb | 447 | c->obuf = sdscat(c->obuf,"GET foo_rand000000000000\r\n"); |
ed9b544e | 448 | c->replytype = REPLY_BULK; |
449 | c->readlen = -1; | |
450 | createMissingClients(c); | |
451 | aeMain(config.el); | |
452 | endBenchmark("GET"); | |
453 | ||
454 | prepareForBenchmark(); | |
455 | c = createClient(); | |
456 | if (!c) exit(1); | |
57172ffb | 457 | c->obuf = sdscat(c->obuf,"INCR counter_rand000000000000\r\n"); |
ed9b544e | 458 | c->replytype = REPLY_INT; |
459 | createMissingClients(c); | |
460 | aeMain(config.el); | |
461 | endBenchmark("INCR"); | |
462 | ||
463 | prepareForBenchmark(); | |
464 | c = createClient(); | |
465 | if (!c) exit(1); | |
466 | c->obuf = sdscat(c->obuf,"LPUSH mylist 3\r\nbar\r\n"); | |
467 | c->replytype = REPLY_INT; | |
468 | createMissingClients(c); | |
469 | aeMain(config.el); | |
470 | endBenchmark("LPUSH"); | |
471 | ||
472 | prepareForBenchmark(); | |
473 | c = createClient(); | |
474 | if (!c) exit(1); | |
475 | c->obuf = sdscat(c->obuf,"LPOP mylist\r\n"); | |
476 | c->replytype = REPLY_BULK; | |
477 | c->readlen = -1; | |
478 | createMissingClients(c); | |
479 | aeMain(config.el); | |
480 | endBenchmark("LPOP"); | |
481 | ||
9f3c422c | 482 | prepareForBenchmark(); |
483 | c = createClient(); | |
484 | if (!c) exit(1); | |
485 | c->obuf = sdscat(c->obuf,"PING\r\n"); | |
486 | c->replytype = REPLY_RETCODE; | |
487 | createMissingClients(c); | |
488 | aeMain(config.el); | |
489 | endBenchmark("PING"); | |
490 | ||
ed9b544e | 491 | printf("\n"); |
492 | } while(config.loop); | |
493 | ||
494 | return 0; | |
495 | } |