Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 679 lines (596 sloc) 22.746 kb
ed9b544 @antirez first commit
authored
1 /* Redis benchmark utility.
2 *
12d090d @antirez A problem with replication with multiple slaves connectiong to a single ...
authored
3 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
ed9b544 @antirez first commit
authored
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
5f5b984 @antirez Partial qsort implemented in SORT command, only when both BY and LIMIT i...
authored
31 #include "fmacros.h"
32
ed9b544 @antirez first commit
authored
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"
ec8f066 @pietern Use hiredis from redis-benchmark
pietern authored
43 #include "hiredis.h"
ed9b544 @antirez first commit
authored
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 {
fc05e8c @pietern More const's in redis-benchmark.c
pietern authored
51 aeEventLoop *el;
52 const char *hostip;
53 int hostport;
54 const char *hostsocket;
ed9b544 @antirez first commit
authored
55 int numclients;
56 int liveclients;
bdbf3ac @pietern Enforce upper bound for number of requests in redis-benchmark
pietern authored
57 int requests;
58 int requests_issued;
59 int requests_finished;
ed9b544 @antirez first commit
authored
60 int keysize;
61 int datasize;
57172ff @antirez CPP client added thanks to Brian Hammond
authored
62 int randomkeys;
ecfaf6d @antirez benchmark utility now supports random keys
authored
63 int randomkeys_keyspacelen;
ed9b544 @antirez first commit
authored
64 int keepalive;
b947428 @antirez redis-benchmark: Initial support for pipelining.
authored
65 int pipeline;
ed9b544 @antirez first commit
authored
66 long long start;
67 long long totlatency;
8146e31 @pietern Microsecond latency resolution in redis-benchmark
pietern authored
68 long long *latency;
fc05e8c @pietern More const's in redis-benchmark.c
pietern authored
69 const char *title;
ed9b544 @antirez first commit
authored
70 list *clients;
71 int quiet;
7b86f5e @antirez csv output for redis-benchmark
authored
72 int csv;
ed9b544 @antirez first commit
authored
73 int loop;
266373b @antirez ae.c now supports multiple polling API modules, even if only ae_select.c...
authored
74 int idlemode;
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
75 char *tests;
ed9b544 @antirez first commit
authored
76 } config;
77
78 typedef struct _client {
ec8f066 @pietern Use hiredis from redis-benchmark
pietern authored
79 redisContext *context;
ed9b544 @antirez first commit
authored
80 sds obuf;
f6da155 @antirez redis-benchmark: a few fixes to pipelining implementation.
authored
81 char *randptr[32]; /* needed for MSET against 10 keys */
3c49070 @pietern Find substrings to randomize when the client is created
pietern authored
82 size_t randlen;
8146e31 @pietern Microsecond latency resolution in redis-benchmark
pietern authored
83 unsigned int written; /* bytes of 'obuf' already written */
84 long long start; /* start time of a request */
85 long long latency; /* request latency */
b947428 @antirez redis-benchmark: Initial support for pipelining.
authored
86 int pending; /* Number of pending requests (sent but no reply received) */
ed9b544 @antirez first commit
authored
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 */
8146e31 @pietern Microsecond latency resolution in redis-benchmark
pietern authored
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
ed9b544 @antirez first commit
authored
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;
ec8f066 @pietern Use hiredis from redis-benchmark
pietern authored
116 aeDeleteFileEvent(config.el,c->context->fd,AE_WRITABLE);
117 aeDeleteFileEvent(config.el,c->context->fd,AE_READABLE);
118 redisFree(c->context);
ed9b544 @antirez first commit
authored
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) {
ec8f066 @pietern Use hiredis from redis-benchmark
pietern authored
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);
ed9b544 @antirez first commit
authored
141 c->written = 0;
b947428 @antirez redis-benchmark: Initial support for pipelining.
authored
142 c->pending = config.pipeline;
ed9b544 @antirez first commit
authored
143 }
144
ecfaf6d @antirez benchmark utility now supports random keys
authored
145 static void randomizeClientKey(client c) {
146 char buf[32];
3c49070 @pietern Find substrings to randomize when the client is created
pietern authored
147 size_t i, r;
ecfaf6d @antirez benchmark utility now supports random keys
authored
148
3c49070 @pietern Find substrings to randomize when the client is created
pietern authored
149 for (i = 0; i < c->randlen; i++) {
150 r = random() % config.randomkeys_keyspacelen;
9b45592 @antirez portability fix in redis benchmark, using zu format specified with size_...
authored
151 snprintf(buf,sizeof(buf),"%012zu",r);
3c49070 @pietern Find substrings to randomize when the client is created
pietern authored
152 memcpy(c->randptr[i],buf,12);
1cd3c1e @pietern Use multi-bulk protocol by default in redis-benchmark
pietern authored
153 }
ecfaf6d @antirez benchmark utility now supports random keys
authored
154 }
155
ed9b544 @antirez first commit
authored
156 static void clientDone(client c) {
bdbf3ac @pietern Enforce upper bound for number of requests in redis-benchmark
pietern authored
157 if (config.requests_finished == config.requests) {
ed9b544 @antirez first commit
authored
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
ec8f066 @pietern Use hiredis from redis-benchmark
pietern authored
172 static void readHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
ed9b544 @antirez first commit
authored
173 client c = privdata;
ec8f066 @pietern Use hiredis from redis-benchmark
pietern authored
174 void *reply = NULL;
ed9b544 @antirez first commit
authored
175 REDIS_NOTUSED(el);
176 REDIS_NOTUSED(fd);
177 REDIS_NOTUSED(mask);
178
8146e31 @pietern Microsecond latency resolution in redis-benchmark
pietern authored
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
ec8f066 @pietern Use hiredis from redis-benchmark
pietern authored
184 if (redisBufferRead(c->context) != REDIS_OK) {
185 fprintf(stderr,"Error: %s\n",c->context->errstr);
186 exit(1);
187 } else {
b947428 @antirez redis-benchmark: Initial support for pipelining.
authored
188 while(c->pending) {
189 if (redisGetReply(c->context,&reply) != REDIS_OK) {
190 fprintf(stderr,"Error: %s\n",c->context->errstr);
53f1d81 @pietern Fix NULL-termination of variable data in redis-benchmark
pietern authored
191 exit(1);
192 }
b947428 @antirez redis-benchmark: Initial support for pipelining.
authored
193 if (reply != NULL) {
194 if (reply == (void*)REDIS_REPLY_ERROR) {
195 fprintf(stderr,"Unexpected error reply, exiting...\n");
196 exit(1);
197 }
198
59132e4 @anydot fix redis-benchmark memory leak
anydot authored
199 freeReplyObject(reply);
200
b947428 @antirez redis-benchmark: Initial support for pipelining.
authored
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 }
8146e31 @pietern Microsecond latency resolution in redis-benchmark
pietern authored
208 }
2fd3095 @antirez multi-bulk reply support for redis-bench, and as a result LRANGE is not ...
authored
209 }
ed9b544 @antirez first commit
authored
210 }
211
ec8f066 @pietern Use hiredis from redis-benchmark
pietern authored
212 static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
ed9b544 @antirez first commit
authored
213 client c = privdata;
214 REDIS_NOTUSED(el);
215 REDIS_NOTUSED(fd);
216 REDIS_NOTUSED(mask);
217
bdbf3ac @pietern Enforce upper bound for number of requests in redis-benchmark
pietern authored
218 /* Initialize request when nothing was written. */
2380388 @pietern Randomize keys and set start time when first write event fires
pietern authored
219 if (c->written == 0) {
bdbf3ac @pietern Enforce upper bound for number of requests in redis-benchmark
pietern authored
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. */
2380388 @pietern Randomize keys and set start time when first write event fires
pietern authored
227 if (config.randomkeys) randomizeClientKey(c);
8146e31 @pietern Microsecond latency resolution in redis-benchmark
pietern authored
228 c->start = ustime();
229 c->latency = -1;
ed9b544 @antirez first commit
authored
230 }
2380388 @pietern Randomize keys and set start time when first write event fires
pietern authored
231
ed9b544 @antirez first commit
authored
232 if (sdslen(c->obuf) > c->written) {
233 void *ptr = c->obuf+c->written;
ec8f066 @pietern Use hiredis from redis-benchmark
pietern authored
234 int nwritten = write(c->context->fd,ptr,sdslen(c->obuf)-c->written);
ed9b544 @antirez first commit
authored
235 if (nwritten == -1) {
61c47ec @antirez better handling of non blocking connect on redis-benchmark: EPIPE on rea...
authored
236 if (errno != EPIPE)
237 fprintf(stderr, "Writing to socket: %s\n", strerror(errno));
ed9b544 @antirez first commit
authored
238 freeClient(c);
239 return;
240 }
241 c->written += nwritten;
242 if (sdslen(c->obuf) == c->written) {
ec8f066 @pietern Use hiredis from redis-benchmark
pietern authored
243 aeDeleteFileEvent(config.el,c->context->fd,AE_WRITABLE);
244 aeCreateFileEvent(config.el,c->context->fd,AE_READABLE,readHandler,c);
ed9b544 @antirez first commit
authored
245 }
246 }
247 }
248
b947428 @antirez redis-benchmark: Initial support for pipelining.
authored
249 static client createClient(char *cmd, size_t len) {
250 int j;
ed9b544 @antirez first commit
authored
251 client c = zmalloc(sizeof(struct _client));
b947428 @antirez redis-benchmark: Initial support for pipelining.
authored
252
ec8f066 @pietern Use hiredis from redis-benchmark
pietern authored
253 if (config.hostsocket == NULL) {
254 c->context = redisConnectNonBlock(config.hostip,config.hostport);
255 } else {
256 c->context = redisConnectUnixNonBlock(config.hostsocket);
ed9b544 @antirez first commit
authored
257 }
ec8f066 @pietern Use hiredis from redis-benchmark
pietern authored
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 }
227b429 @antirez redis-benchmark: disable big buffer cleanup in hiredis context.
authored
266 /* Suppress hiredis cleanup of unused buffers for max speed. */
267 c->context->reader->maxbuf = 0;
b947428 @antirez redis-benchmark: Initial support for pipelining.
authored
268 /* Queue N requests accordingly to the pipeline size. */
269 c->obuf = sdsempty();
270 for (j = 0; j < config.pipeline; j++)
271 c->obuf = sdscatlen(c->obuf,cmd,len);
3c49070 @pietern Find substrings to randomize when the client is created
pietern authored
272 c->randlen = 0;
ed9b544 @antirez first commit
authored
273 c->written = 0;
b947428 @antirez redis-benchmark: Initial support for pipelining.
authored
274 c->pending = config.pipeline;
3c49070 @pietern Find substrings to randomize when the client is created
pietern authored
275
276 /* Find substrings in the output buffer that need to be randomized. */
277 if (config.randomkeys) {
f6da155 @antirez redis-benchmark: a few fixes to pipelining implementation.
authored
278 char *p = c->obuf;
3c49070 @pietern Find substrings to randomize when the client is created
pietern authored
279 while ((p = strstr(p,":rand:")) != NULL) {
280 assert(c->randlen < (signed)(sizeof(c->randptr)/sizeof(char*)));
281 c->randptr[c->randlen++] = p+6;
f6da155 @antirez redis-benchmark: a few fixes to pipelining implementation.
authored
282 p += 6;
3c49070 @pietern Find substrings to randomize when the client is created
pietern authored
283 }
284 }
285
b66e5ad @antirez The hiredis lib shipped with Redis was updated to latest version.
authored
286 /* redisSetReplyObjectFunctions(c->context,NULL); */
ec8f066 @pietern Use hiredis from redis-benchmark
pietern authored
287 aeCreateFileEvent(config.el,c->context->fd,AE_WRITABLE,writeHandler,c);
ed9b544 @antirez first commit
authored
288 listAddNodeTail(config.clients,c);
ec8f066 @pietern Use hiredis from redis-benchmark
pietern authored
289 config.liveclients++;
ed9b544 @antirez first commit
authored
290 return c;
291 }
292
293 static void createMissingClients(client c) {
f474a5b @dspezia Add wait states to deal with many connections.
dspezia authored
294 int n = 0;
295
ed9b544 @antirez first commit
authored
296 while(config.liveclients < config.numclients) {
f6da155 @antirez redis-benchmark: a few fixes to pipelining implementation.
authored
297 createClient(c->obuf,sdslen(c->obuf)/config.pipeline);
f474a5b @dspezia Add wait states to deal with many connections.
dspezia authored
298
299 /* Listen backlog is quite limited on most systems */
300 if (++n > 64) {
301 usleep(50000);
302 n = 0;
303 }
ed9b544 @antirez first commit
authored
304 }
305 }
306
8146e31 @pietern Microsecond latency resolution in redis-benchmark
pietern authored
307 static int compareLatency(const void *a, const void *b) {
308 return (*(long long*)a)-(*(long long*)b);
309 }
310
ed0dd55 @pietern Show the current throughput while benchmarking
pietern authored
311 static void showLatencyReport(void) {
8146e31 @pietern Microsecond latency resolution in redis-benchmark
pietern authored
312 int i, curlat = 0;
ed9b544 @antirez first commit
authored
313 float perc, reqpersec;
314
bdbf3ac @pietern Enforce upper bound for number of requests in redis-benchmark
pietern authored
315 reqpersec = (float)config.requests_finished/((float)config.totlatency/1000);
7b86f5e @antirez csv output for redis-benchmark
authored
316 if (!config.quiet && !config.csv) {
ed0dd55 @pietern Show the current throughput while benchmarking
pietern authored
317 printf("====== %s ======\n", config.title);
bdbf3ac @pietern Enforce upper bound for number of requests in redis-benchmark
pietern authored
318 printf(" %d requests completed in %.2f seconds\n", config.requests_finished,
ed9b544 @antirez first commit
authored
319 (float)config.totlatency/1000);
320 printf(" %d parallel clients\n", config.numclients);
321 printf(" %d bytes payload\n", config.datasize);
322 printf(" keep alive: %d\n", config.keepalive);
323 printf("\n");
8146e31 @pietern Microsecond latency resolution in redis-benchmark
pietern authored
324
325 qsort(config.latency,config.requests,sizeof(long long),compareLatency);
326 for (i = 0; i < config.requests; i++) {
327 if (config.latency[i]/1000 != curlat || i == (config.requests-1)) {
328 curlat = config.latency[i]/1000;
329 perc = ((float)(i+1)*100)/config.requests;
330 printf("%.2f%% <= %d milliseconds\n", perc, curlat);
ed9b544 @antirez first commit
authored
331 }
332 }
333 printf("%.2f requests per second\n\n", reqpersec);
7b86f5e @antirez csv output for redis-benchmark
authored
334 } else if (config.csv) {
335 printf("\"%s\",\"%.2f\"\n", config.title, reqpersec);
ed9b544 @antirez first commit
authored
336 } else {
ed0dd55 @pietern Show the current throughput while benchmarking
pietern authored
337 printf("%s: %.2f requests per second\n", config.title, reqpersec);
ed9b544 @antirez first commit
authored
338 }
339 }
340
b947428 @antirez redis-benchmark: Initial support for pipelining.
authored
341 static void benchmark(char *title, char *cmd, int len) {
f2f2424 @pietern Remove code duplication
pietern authored
342 client c;
343
ed0dd55 @pietern Show the current throughput while benchmarking
pietern authored
344 config.title = title;
bdbf3ac @pietern Enforce upper bound for number of requests in redis-benchmark
pietern authored
345 config.requests_issued = 0;
346 config.requests_finished = 0;
ed9b544 @antirez first commit
authored
347
3c49070 @pietern Find substrings to randomize when the client is created
pietern authored
348 c = createClient(cmd,len);
f2f2424 @pietern Remove code duplication
pietern authored
349 createMissingClients(c);
350
351 config.start = mstime();
352 aeMain(config.el);
ed9b544 @antirez first commit
authored
353 config.totlatency = mstime()-config.start;
f2f2424 @pietern Remove code duplication
pietern authored
354
ed0dd55 @pietern Show the current throughput while benchmarking
pietern authored
355 showLatencyReport();
ed9b544 @antirez first commit
authored
356 freeAllClients();
357 }
358
39bf440 @pietern Provide custom command to benchmark against via args
pietern authored
359 /* Returns number of consumed options. */
360 int parseOptions(int argc, const char **argv) {
ed9b544 @antirez first commit
authored
361 int i;
39bf440 @pietern Provide custom command to benchmark against via args
pietern authored
362 int lastarg;
363 int exit_status = 1;
ed9b544 @antirez first commit
authored
364
365 for (i = 1; i < argc; i++) {
39bf440 @pietern Provide custom command to benchmark against via args
pietern authored
366 lastarg = (i == (argc-1));
367
368 if (!strcmp(argv[i],"-c")) {
369 if (lastarg) goto invalid;
370 config.numclients = atoi(argv[++i]);
371 } else if (!strcmp(argv[i],"-n")) {
372 if (lastarg) goto invalid;
373 config.requests = atoi(argv[++i]);
374 } else if (!strcmp(argv[i],"-k")) {
375 if (lastarg) goto invalid;
376 config.keepalive = atoi(argv[++i]);
377 } else if (!strcmp(argv[i],"-h")) {
378 if (lastarg) goto invalid;
379 config.hostip = strdup(argv[++i]);
380 } else if (!strcmp(argv[i],"-p")) {
381 if (lastarg) goto invalid;
382 config.hostport = atoi(argv[++i]);
383 } else if (!strcmp(argv[i],"-s")) {
384 if (lastarg) goto invalid;
385 config.hostsocket = strdup(argv[++i]);
386 } else if (!strcmp(argv[i],"-d")) {
387 if (lastarg) goto invalid;
388 config.datasize = atoi(argv[++i]);
ed9b544 @antirez first commit
authored
389 if (config.datasize < 1) config.datasize=1;
826b5be @antirez further optimizations for the multi bulk protocol parsing code when big ...
authored
390 if (config.datasize > 1024*1024*1024) config.datasize = 1024*1024*1024;
b947428 @antirez redis-benchmark: Initial support for pipelining.
authored
391 } else if (!strcmp(argv[i],"-P")) {
392 if (lastarg) goto invalid;
393 config.pipeline = atoi(argv[++i]);
394 if (config.pipeline <= 0) config.pipeline=1;
39bf440 @pietern Provide custom command to benchmark against via args
pietern authored
395 } else if (!strcmp(argv[i],"-r")) {
396 if (lastarg) goto invalid;
57172ff @antirez CPP client added thanks to Brian Hammond
authored
397 config.randomkeys = 1;
39bf440 @pietern Provide custom command to benchmark against via args
pietern authored
398 config.randomkeys_keyspacelen = atoi(argv[++i]);
ecfaf6d @antirez benchmark utility now supports random keys
authored
399 if (config.randomkeys_keyspacelen < 0)
400 config.randomkeys_keyspacelen = 0;
ed9b544 @antirez first commit
authored
401 } else if (!strcmp(argv[i],"-q")) {
402 config.quiet = 1;
7b86f5e @antirez csv output for redis-benchmark
authored
403 } else if (!strcmp(argv[i],"--csv")) {
404 config.csv = 1;
ed9b544 @antirez first commit
authored
405 } else if (!strcmp(argv[i],"-l")) {
406 config.loop = 1;
266373b @antirez ae.c now supports multiple polling API modules, even if only ae_select.c...
authored
407 } else if (!strcmp(argv[i],"-I")) {
408 config.idlemode = 1;
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
409 } else if (!strcmp(argv[i],"-t")) {
410 if (lastarg) goto invalid;
411 /* We get the list of tests to run as a string in the form
412 * get,set,lrange,...,test_N. Then we add a comma before and
413 * after the string in order to make sure that searching
414 * for ",testname," will always get a match if the test is
415 * enabled. */
416 config.tests = sdsnew(",");
417 config.tests = sdscat(config.tests,(char*)argv[++i]);
418 config.tests = sdscat(config.tests,",");
419 sdstolower(config.tests);
39bf440 @pietern Provide custom command to benchmark against via args
pietern authored
420 } else if (!strcmp(argv[i],"--help")) {
421 exit_status = 0;
422 goto usage;
ed9b544 @antirez first commit
authored
423 } else {
39bf440 @pietern Provide custom command to benchmark against via args
pietern authored
424 /* Assume the user meant to provide an option when the arg starts
425 * with a dash. We're done otherwise and should use the remainder
426 * as the command and arguments for running the benchmark. */
427 if (argv[i][0] == '-') goto invalid;
428 return i;
ed9b544 @antirez first commit
authored
429 }
430 }
39bf440 @pietern Provide custom command to benchmark against via args
pietern authored
431
432 return i;
433
434 invalid:
435 printf("Invalid option \"%s\" or option argument missing\n\n",argv[i]);
436
437 usage:
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
438 printf(
439 "Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]\n\n"
440 " -h <hostname> Server hostname (default 127.0.0.1)\n"
441 " -p <port> Server port (default 6379)\n"
442 " -s <socket> Server socket (overrides host and port)\n"
443 " -c <clients> Number of parallel connections (default 50)\n"
444 " -n <requests> Total number of requests (default 10000)\n"
445 " -d <size> Data size of SET/GET value in bytes (default 2)\n"
446 " -k <boolean> 1=keep alive 0=reconnect (default 1)\n"
447 " -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD\n"
448 " Using this option the benchmark will get/set keys\n"
8dd19d8 @antirez Fixed redis-benchmark --help output typo
authored
449 " in the form mykey_rand:000000012456 instead of constant\n"
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
450 " keys, the <keyspacelen> argument determines the max\n"
451 " number of values for the random number. For instance\n"
8dd19d8 @antirez Fixed redis-benchmark --help output typo
authored
452 " if set to 10 only rand:000000000000 - rand:000000000009\n"
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
453 " range will be allowed.\n"
b947428 @antirez redis-benchmark: Initial support for pipelining.
authored
454 " -P <numreq> Pipeline <numreq> requests. Default 1 (no pipeline).\n"
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
455 " -q Quiet. Just show query/sec values\n"
456 " --csv Output in CSV format\n"
457 " -l Loop. Run the tests forever\n"
458 " -t <tests> Only run the comma separated list of tests. The test\n"
459 " names are the same as the ones produced as output.\n"
460 " -I Idle mode. Just open N idle connections and wait.\n\n"
461 "Examples:\n\n"
462 " Run the benchmark with the default configuration against 127.0.0.1:6379:\n"
463 " $ redis-benchmark\n\n"
464 " Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1:\n"
465 " $ redis-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20\n\n"
466 " Fill 127.0.0.1:6379 with about 1 million keys only using the SET test:\n"
467 " $ redis-benchmark -t set -n 1000000 -r 100000000\n\n"
468 " Benchmark 127.0.0.1:6379 for a few commands producing CSV output:\n"
469 " $ redis-benchmark -t ping,set,get -n 100000 --csv\n\n"
8dd19d8 @antirez Fixed redis-benchmark --help output typo
authored
470 " Fill a list with 10000 random elements:\n"
471 " $ redis-benchmark -r 10000 -n 10000 lpush mylist ele:rand:000000000000\n\n"
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
472 );
39bf440 @pietern Provide custom command to benchmark against via args
pietern authored
473 exit(exit_status);
ed9b544 @antirez first commit
authored
474 }
475
ed0dd55 @pietern Show the current throughput while benchmarking
pietern authored
476 int showThroughput(struct aeEventLoop *eventLoop, long long id, void *clientData) {
477 REDIS_NOTUSED(eventLoop);
478 REDIS_NOTUSED(id);
479 REDIS_NOTUSED(clientData);
480
7b86f5e @antirez csv output for redis-benchmark
authored
481 if (config.csv) return 250;
ed0dd55 @pietern Show the current throughput while benchmarking
pietern authored
482 float dt = (float)(mstime()-config.start)/1000.0;
bdbf3ac @pietern Enforce upper bound for number of requests in redis-benchmark
pietern authored
483 float rps = (float)config.requests_finished/dt;
ed0dd55 @pietern Show the current throughput while benchmarking
pietern authored
484 printf("%s: %.2f\r", config.title, rps);
485 fflush(stdout);
486 return 250; /* every 250ms */
487 }
488
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
489 /* Return true if the named test was selected using the -t command line
490 * switch, or if all the tests are selected (no -t passed by user). */
491 int test_is_selected(char *name) {
492 char buf[256];
493 int l = strlen(name);
494
495 if (config.tests == NULL) return 1;
496 buf[0] = ',';
497 memcpy(buf+1,name,l);
498 buf[l+1] = ',';
499 buf[l+2] = '\0';
500 return strstr(config.tests,buf) != NULL;
501 }
502
fc05e8c @pietern More const's in redis-benchmark.c
pietern authored
503 int main(int argc, const char **argv) {
174df6f @pietern Re-use variable data in redis-benchmark
pietern authored
504 int i;
39bf440 @pietern Provide custom command to benchmark against via args
pietern authored
505 char *data, *cmd;
506 int len;
507
ed9b544 @antirez first commit
authored
508 client c;
509
510 signal(SIGHUP, SIG_IGN);
511 signal(SIGPIPE, SIG_IGN);
512
513 config.numclients = 50;
514 config.requests = 10000;
515 config.liveclients = 0;
e074416 @antirez Max limit to 10k clients removed, this implements feature request on iss...
authored
516 config.el = aeCreateEventLoop(1024*10);
ed0dd55 @pietern Show the current throughput while benchmarking
pietern authored
517 aeCreateTimeEvent(config.el,1,showThroughput,NULL,NULL);
ed9b544 @antirez first commit
authored
518 config.keepalive = 1;
519 config.datasize = 3;
b947428 @antirez redis-benchmark: Initial support for pipelining.
authored
520 config.pipeline = 1;
57172ff @antirez CPP client added thanks to Brian Hammond
authored
521 config.randomkeys = 0;
ecfaf6d @antirez benchmark utility now supports random keys
authored
522 config.randomkeys_keyspacelen = 0;
ed9b544 @antirez first commit
authored
523 config.quiet = 0;
7b86f5e @antirez csv output for redis-benchmark
authored
524 config.csv = 0;
ed9b544 @antirez first commit
authored
525 config.loop = 0;
266373b @antirez ae.c now supports multiple polling API modules, even if only ae_select.c...
authored
526 config.idlemode = 0;
ed9b544 @antirez first commit
authored
527 config.latency = NULL;
528 config.clients = listCreate();
529 config.hostip = "127.0.0.1";
530 config.hostport = 6379;
c61e692 @pietern Support for Redis to listen on a Unix socket
pietern authored
531 config.hostsocket = NULL;
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
532 config.tests = NULL;
ed9b544 @antirez first commit
authored
533
39bf440 @pietern Provide custom command to benchmark against via args
pietern authored
534 i = parseOptions(argc,argv);
535 argc -= i;
536 argv += i;
537
8146e31 @pietern Microsecond latency resolution in redis-benchmark
pietern authored
538 config.latency = zmalloc(sizeof(long long)*config.requests);
ed9b544 @antirez first commit
authored
539
540 if (config.keepalive == 0) {
c325149 @antirez benchmark now outputs the right command line to shorten the TIME_WAIT in...
authored
541 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");
ed9b544 @antirez first commit
authored
542 }
543
266373b @antirez ae.c now supports multiple polling API modules, even if only ae_select.c...
authored
544 if (config.idlemode) {
545 printf("Creating %d idle connections and waiting forever (Ctrl+C when done)\n", config.numclients);
3c49070 @pietern Find substrings to randomize when the client is created
pietern authored
546 c = createClient("",0); /* will never receive a reply */
266373b @antirez ae.c now supports multiple polling API modules, even if only ae_select.c...
authored
547 createMissingClients(c);
548 aeMain(config.el);
549 /* and will wait for every */
550 }
551
39bf440 @pietern Provide custom command to benchmark against via args
pietern authored
552 /* Run benchmark with command in the remainder of the arguments. */
553 if (argc) {
554 sds title = sdsnew(argv[0]);
555 for (i = 1; i < argc; i++) {
556 title = sdscatlen(title, " ", 1);
294cd53 @pietern There is no debug mode in benchmark tool
pietern authored
557 title = sdscatlen(title, (char*)argv[i], strlen(argv[i]));
39bf440 @pietern Provide custom command to benchmark against via args
pietern authored
558 }
559
560 do {
561 len = redisFormatCommandArgv(&cmd,argc,argv,NULL);
562 benchmark(title,cmd,len);
563 free(cmd);
564 } while(config.loop);
1cd3c1e @pietern Use multi-bulk protocol by default in redis-benchmark
pietern authored
565
39bf440 @pietern Provide custom command to benchmark against via args
pietern authored
566 return 0;
567 }
568
569 /* Run default benchmark suite. */
570 do {
1cd3c1e @pietern Use multi-bulk protocol by default in redis-benchmark
pietern authored
571 data = zmalloc(config.datasize+1);
174df6f @pietern Re-use variable data in redis-benchmark
pietern authored
572 memset(data,'x',config.datasize);
573 data[config.datasize] = '\0';
574
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
575 if (test_is_selected("ping_inline") || test_is_selected("ping"))
576 benchmark("PING_INLINE","PING\r\n",6);
6766f45 @antirez multi bulk requests in redis-benchmark, default fsync policy changed to ...
authored
577
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
578 if (test_is_selected("ping_mbulk") || test_is_selected("ping")) {
579 len = redisFormatCommand(&cmd,"PING");
580 benchmark("PING_BULK",cmd,len);
581 free(cmd);
582 }
f2f2424 @pietern Remove code duplication
pietern authored
583
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
584 if (test_is_selected("set")) {
585 len = redisFormatCommand(&cmd,"SET foo:rand:000000000000 %s",data);
586 benchmark("SET",cmd,len);
587 free(cmd);
d69a483 @pietern Make the MSET benchmark *really* work with 10 keys
pietern authored
588 }
ea5b709 @pietern Add benchmark for MSET
pietern authored
589
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
590 if (test_is_selected("get")) {
591 len = redisFormatCommand(&cmd,"GET foo:rand:000000000000");
592 benchmark("GET",cmd,len);
593 free(cmd);
594 }
ed9b544 @antirez first commit
authored
595
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
596 if (test_is_selected("incr")) {
597 len = redisFormatCommand(&cmd,"INCR counter:rand:000000000000");
598 benchmark("INCR",cmd,len);
599 free(cmd);
600 }
ed9b544 @antirez first commit
authored
601
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
602 if (test_is_selected("lpush")) {
603 len = redisFormatCommand(&cmd,"LPUSH mylist %s",data);
604 benchmark("LPUSH",cmd,len);
605 free(cmd);
606 }
ed9b544 @antirez first commit
authored
607
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
608 if (test_is_selected("lpop")) {
609 len = redisFormatCommand(&cmd,"LPOP mylist");
610 benchmark("LPOP",cmd,len);
611 free(cmd);
612 }
ed9b544 @antirez first commit
authored
613
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
614 if (test_is_selected("sadd")) {
615 len = redisFormatCommand(&cmd,
616 "SADD myset counter:rand:000000000000");
617 benchmark("SADD",cmd,len);
618 free(cmd);
619 }
ed9b544 @antirez first commit
authored
620
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
621 if (test_is_selected("spop")) {
622 len = redisFormatCommand(&cmd,"SPOP myset");
623 benchmark("SPOP",cmd,len);
624 free(cmd);
625 }
b1ad58e @antirez redis-benchmark now implements Set commands benchmarks
authored
626
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
627 if (test_is_selected("lrange") ||
628 test_is_selected("lrange_100") ||
629 test_is_selected("lrange_300") ||
630 test_is_selected("lrange_500") ||
631 test_is_selected("lrange_600"))
632 {
633 len = redisFormatCommand(&cmd,"LPUSH mylist %s",data);
634 benchmark("LPUSH (needed to benchmark LRANGE)",cmd,len);
635 free(cmd);
636 }
b1ad58e @antirez redis-benchmark now implements Set commands benchmarks
authored
637
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
638 if (test_is_selected("lrange") || test_is_selected("lrange_100")) {
639 len = redisFormatCommand(&cmd,"LRANGE mylist 0 99");
640 benchmark("LRANGE_100 (first 100 elements)",cmd,len);
641 free(cmd);
642 }
2fd3095 @antirez multi-bulk reply support for redis-bench, and as a result LRANGE is not ...
authored
643
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
644 if (test_is_selected("lrange") || test_is_selected("lrange_300")) {
645 len = redisFormatCommand(&cmd,"LRANGE mylist 0 299");
646 benchmark("LRANGE_300 (first 300 elements)",cmd,len);
647 free(cmd);
648 }
2fd3095 @antirez multi-bulk reply support for redis-bench, and as a result LRANGE is not ...
authored
649
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
650 if (test_is_selected("lrange") || test_is_selected("lrange_500")) {
651 len = redisFormatCommand(&cmd,"LRANGE mylist 0 449");
652 benchmark("LRANGE_500 (first 450 elements)",cmd,len);
653 free(cmd);
654 }
ccb5332 @antirez more writev tests/work
authored
655
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
656 if (test_is_selected("lrange") || test_is_selected("lrange_600")) {
657 len = redisFormatCommand(&cmd,"LRANGE mylist 0 599");
658 benchmark("LRANGE_600 (first 600 elements)",cmd,len);
659 free(cmd);
660 }
cc30e36 @antirez benchmarking with different number of LRANGE elements. Ability to change...
authored
661
d9747b4 @antirez redis-benchmark: ability to run selected tests. Better help with example...
authored
662 if (test_is_selected("mset")) {
663 const char *argv[21];
664 argv[0] = "MSET";
665 for (i = 1; i < 21; i += 2) {
666 argv[i] = "foo:rand:000000000000";
667 argv[i+1] = data;
668 }
669 len = redisFormatCommandArgv(&cmd,21,argv,NULL);
670 benchmark("MSET (10 keys)",cmd,len);
671 free(cmd);
672 }
cc30e36 @antirez benchmarking with different number of LRANGE elements. Ability to change...
authored
673
7b86f5e @antirez csv output for redis-benchmark
authored
674 if (!config.csv) printf("\n");
ed9b544 @antirez first commit
authored
675 } while(config.loop);
676
677 return 0;
678 }
Something went wrong with that request. Please try again.