redisContext *context;
int state;
sds obuf;
+ char *randptr;
unsigned int written; /* bytes of 'obuf' already written */
- int replytype;
long long start; /* start time of a request */
long long latency; /* request latency */
} *client;
}
static void randomizeClientKey(client c) {
- char *p;
+ char *p, *newline;
char buf[32];
long r;
- p = strstr(c->obuf, "_rand");
- if (!p) return;
- p += 5;
+ if (c->randptr == NULL) return;
+
+ /* Check if we have to randomize (only once per connection) */
+ if (c->randptr == (void*)-1) {
+ p = strstr(c->obuf,":rand:");
+ if (!p) {
+ c->randptr = NULL;
+ return;
+ } else {
+ newline = strstr(p,"\r\n");
+ assert(newline-(p+6) == 12); /* 12 chars for randomness */
+ c->randptr = p+6;
+ }
+ }
+
+ /* Set random number in output buffer */
r = random() % config.randomkeys_keyspacelen;
- sprintf(buf,"%ld",r);
- memcpy(p,buf,strlen(buf));
+ snprintf(buf,sizeof(buf),"%012ld",r);
+ memcpy(c->randptr,buf,12);
}
static void clientDone(client c) {
}
}
-static client createClient(int replytype) {
+static client createClient() {
client c = zmalloc(sizeof(struct _client));
if (config.hostsocket == NULL) {
c->context = redisConnectNonBlock(config.hostip,config.hostport);
fprintf(stderr,"%s: %s\n",config.hostsocket,c->context->errstr);
exit(1);
}
- c->replytype = replytype;
c->state = CLIENT_CONNECTING;
- c->obuf = sdsempty();
+ c->obuf = NULL;
+ c->randptr = (void*)-1;
c->written = 0;
redisSetReplyObjectFunctions(c->context,NULL);
aeCreateFileEvent(config.el,c->context->fd,AE_WRITABLE,writeHandler,c);
}
static void createMissingClients(client c) {
+ int n = 0;
+
while(config.liveclients < config.numclients) {
- client new = createClient(c->replytype);
- sdsfree(new->obuf);
+ client new = createClient();
new->obuf = sdsdup(c->obuf);
if (config.randomkeys) randomizeClientKey(c);
+
+ /* Listen backlog is quite limited on most systems */
+ if (++n > 64) {
+ usleep(50000);
+ n = 0;
+ }
}
}
}
}
-static void prepareForBenchmark(char *title) {
+static void benchmark(char *title, char *cmd, int len) {
+ client c;
+
config.title = title;
- config.start = mstime();
config.donerequests = 0;
-}
-static void endBenchmark(void) {
+ c = createClient();
+ c->obuf = sdsnewlen(cmd,len);
+ createMissingClients(c);
+
+ config.start = mstime();
+ aeMain(config.el);
config.totlatency = mstime()-config.start;
+
showLatencyReport();
freeAllClients();
}
if (config.idlemode) {
printf("Creating %d idle connections and waiting forever (Ctrl+C when done)\n", config.numclients);
- prepareForBenchmark("IDLE");
c = createClient(0); /* will never receive a reply */
c->obuf = sdsempty();
createMissingClients(c);
}
do {
- char *data = zmalloc(config.datasize+1);
+ char *data, *cmd;
+ int len;
+
+ data = zmalloc(config.datasize+1);
memset(data,'x',config.datasize);
data[config.datasize] = '\0';
- prepareForBenchmark("PING");
- c = createClient(REDIS_REPLY_STATUS);
- c->obuf = sdscat(c->obuf,"PING\r\n");
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ benchmark("PING (inline)","PING\r\n",6);
- prepareForBenchmark("PING (multi bulk)");
- c = createClient(REDIS_REPLY_STATUS);
- c->obuf = sdscat(c->obuf,"*1\r\n$4\r\nPING\r\n");
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ len = redisFormatCommand(&cmd,"PING");
+ benchmark("PING",cmd,len);
+ free(cmd);
- prepareForBenchmark("MSET (10 keys, multi bulk)");
- c = createClient(REDIS_REPLY_ARRAY);
- c->obuf = sdscatprintf(c->obuf,"*%d\r\n$4\r\nMSET\r\n", 11);
- for (i = 0; i < 10; i++) {
- c->obuf = sdscatprintf(c->obuf,"$%d\r\n%s\r\n",config.datasize,data);
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ const char *argv[21];
+ argv[0] = "MSET";
+ for (i = 1; i < 21; i += 2) {
+ argv[i] = "foo:rand:000000000000";
+ argv[i+1] = data;
+ }
+ len = redisFormatCommandArgv(&cmd,21,argv,NULL);
+ benchmark("MSET (10 keys)",cmd,len);
+ free(cmd);
- prepareForBenchmark("SET");
- c = createClient(REDIS_REPLY_STATUS);
- c->obuf = sdscat(c->obuf,"*3\r\n$3\r\nSET\r\n$20\r\nfoo_rand000000000000\r\n");
- c->obuf = sdscatprintf(c->obuf,"$%d\r\n%s\r\n",config.datasize,data);
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ len = redisFormatCommand(&cmd,"SET foo:rand:000000000000 %s",data);
+ benchmark("SET",cmd,len);
+ free(cmd);
- prepareForBenchmark("GET");
- c = createClient(REDIS_REPLY_STRING);
- c->obuf = sdscat(c->obuf,"GET foo_rand000000000000\r\n");
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ len = redisFormatCommand(&cmd,"GET foo:rand:000000000000");
+ benchmark("GET",cmd,len);
+ free(cmd);
- prepareForBenchmark("INCR");
- c = createClient(REDIS_REPLY_INTEGER);
- c->obuf = sdscat(c->obuf,"INCR counter_rand000000000000\r\n");
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ len = redisFormatCommand(&cmd,"INCR counter:rand:000000000000");
+ benchmark("INCR",cmd,len);
+ free(cmd);
- prepareForBenchmark("LPUSH");
- c = createClient(REDIS_REPLY_INTEGER);
- c->obuf = sdscat(c->obuf,"LPUSH mylist bar\r\n");
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ len = redisFormatCommand(&cmd,"LPUSH mylist %s",data);
+ benchmark("LPUSH",cmd,len);
+ free(cmd);
- prepareForBenchmark("LPOP");
- c = createClient(REDIS_REPLY_STRING);
- c->obuf = sdscat(c->obuf,"LPOP mylist\r\n");
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ len = redisFormatCommand(&cmd,"LPOP mylist");
+ benchmark("LPOP",cmd,len);
+ free(cmd);
- prepareForBenchmark("SADD");
- c = createClient(REDIS_REPLY_STATUS);
- c->obuf = sdscat(c->obuf,"SADD myset counter_rand000000000000\r\n");
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ len = redisFormatCommand(&cmd,"SADD myset counter:rand:000000000000");
+ benchmark("SADD",cmd,len);
+ free(cmd);
- prepareForBenchmark("SPOP");
- c = createClient(REDIS_REPLY_STRING);
- c->obuf = sdscat(c->obuf,"SPOP myset\r\n");
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ len = redisFormatCommand(&cmd,"SPOP myset");
+ benchmark("SPOP",cmd,len);
+ free(cmd);
- prepareForBenchmark("LPUSH (again, in order to bench LRANGE)");
- c = createClient(REDIS_REPLY_STATUS);
- c->obuf = sdscat(c->obuf,"LPUSH mylist bar\r\n");
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ len = redisFormatCommand(&cmd,"LPUSH mylist %s",data);
+ benchmark("LPUSH (again, in order to bench LRANGE)",cmd,len);
+ free(cmd);
- prepareForBenchmark("LRANGE (first 100 elements)");
- c = createClient(REDIS_REPLY_ARRAY);
- c->obuf = sdscat(c->obuf,"LRANGE mylist 0 99\r\n");
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ len = redisFormatCommand(&cmd,"LRANGE mylist 0 99");
+ benchmark("LRANGE (first 100 elements)",cmd,len);
+ free(cmd);
- prepareForBenchmark("LRANGE (first 300 elements)");
- c = createClient(REDIS_REPLY_ARRAY);
- c->obuf = sdscat(c->obuf,"LRANGE mylist 0 299\r\n");
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ len = redisFormatCommand(&cmd,"LRANGE mylist 0 299");
+ benchmark("LRANGE (first 300 elements)",cmd,len);
+ free(cmd);
- prepareForBenchmark("LRANGE (first 450 elements)");
- c = createClient(REDIS_REPLY_ARRAY);
- c->obuf = sdscat(c->obuf,"LRANGE mylist 0 449\r\n");
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ len = redisFormatCommand(&cmd,"LRANGE mylist 0 449");
+ benchmark("LRANGE (first 450 elements)",cmd,len);
+ free(cmd);
- prepareForBenchmark("LRANGE (first 600 elements)");
- c = createClient(REDIS_REPLY_ARRAY);
- c->obuf = sdscat(c->obuf,"LRANGE mylist 0 599\r\n");
- createMissingClients(c);
- aeMain(config.el);
- endBenchmark();
+ len = redisFormatCommand(&cmd,"LRANGE mylist 0 599");
+ benchmark("LRANGE (first 600 elements)",cmd,len);
+ free(cmd);
printf("\n");
} while(config.loop);