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