X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/ed9b544e10b84cd43348ddfab7068b610a5df1f7..244de507b304596b2e853d2294b9421e17b1fba5:/benchmark.c?ds=sidebyside diff --git a/benchmark.c b/benchmark.c index 51c7e980..0d53c935 100644 --- a/benchmark.c +++ b/benchmark.c @@ -28,6 +28,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include "fmacros.h" + #include #include #include @@ -62,6 +64,8 @@ static struct config { int donerequests; int keysize; int datasize; + int randomkeys; + int randomkeys_keyspacelen; aeEventLoop *el; char *hostip; int hostport; @@ -135,6 +139,20 @@ static void resetClient(client c) { c->written = 0; c->state = CLIENT_SENDQUERY; c->start = mstime(); + createMissingClients(c); +} + +static void randomizeClientKey(client c) { + char *p; + char buf[32]; + long r; + + p = strstr(c->obuf, "_rand"); + if (!p) return; + p += 5; + r = random() % config.randomkeys_keyspacelen; + sprintf(buf,"%ld",r); + memcpy(p,buf,strlen(buf)); } static void clientDone(client c) { @@ -151,6 +169,7 @@ static void clientDone(client c) { } if (config.keepalive) { resetClient(c); + if (config.randomkeys) randomizeClientKey(c); } else { config.liveclients--; createMissingClients(c); @@ -190,11 +209,11 @@ static void readHandler(aeEventLoop *el, int fd, void *privdata, int mask) if (c->replytype == REPLY_BULK) { *p = '\0'; *(p-1) = '\0'; - if (memcmp(c->ibuf,"nil",3) == 0) { + c->readlen = atoi(c->ibuf+1)+2; + if (c->readlen-2 == -1) { clientDone(c); return; } - c->readlen = atoi(c->ibuf)+2; c->ibuf = sdsrange(c->ibuf,(p-c->ibuf)+1,-1); } else { c->ibuf = sdstrim(c->ibuf,"\r\n"); @@ -265,6 +284,7 @@ static void createMissingClients(client c) { if (!new) continue; sdsfree(new->obuf); new->obuf = sdsdup(c->obuf); + if (config.randomkeys) randomizeClientKey(c); new->replytype = c->replytype; if (c->replytype == REPLY_BULK) new->readlen = -1; @@ -341,6 +361,12 @@ void parseOptions(int argc, char **argv) { i++; if (config.datasize < 1) config.datasize=1; if (config.datasize > 1024*1024) config.datasize = 1024*1024; + } else if (!strcmp(argv[i],"-r") && !lastarg) { + config.randomkeys = 1; + config.randomkeys_keyspacelen = atoi(argv[i+1]); + if (config.randomkeys_keyspacelen < 0) + config.randomkeys_keyspacelen = 0; + i++; } else if (!strcmp(argv[i],"-q")) { config.quiet = 1; } else if (!strcmp(argv[i],"-l")) { @@ -354,6 +380,13 @@ void parseOptions(int argc, char **argv) { printf(" -n Total number of requests (default 10000)\n"); printf(" -d Data size of SET/GET value in bytes (default 2)\n"); printf(" -k 1=keep alive 0=reconnect (default 1)\n"); + printf(" -r Use random keys for SET/GET/INCR\n"); + printf(" Using this option the benchmark will get/set keys\n"); + printf(" in the form mykey_rand000000012456 instead of constant\n"); + printf(" keys, the argument determines the max\n"); + printf(" number of values for the random number. For instance\n"); + printf(" if set to 10 only rand000000000000 - rand000000000009\n"); + printf(" range will be allowed.\n"); printf(" -q Quiet. Just show query/sec values\n"); printf(" -l Loop. Run the tests forever\n"); exit(1); @@ -374,6 +407,8 @@ int main(int argc, char **argv) { config.keepalive = 1; config.donerequests = 0; config.datasize = 3; + config.randomkeys = 0; + config.randomkeys_keyspacelen = 0; config.quiet = 0; config.loop = 0; config.latency = NULL; @@ -393,16 +428,7 @@ int main(int argc, char **argv) { prepareForBenchmark(); c = createClient(); if (!c) exit(1); - c->obuf = sdscat(c->obuf,"PING\r\n"); - c->replytype = REPLY_RETCODE; - createMissingClients(c); - aeMain(config.el); - endBenchmark("PING"); - - prepareForBenchmark(); - c = createClient(); - if (!c) exit(1); - c->obuf = sdscatprintf(c->obuf,"SET foo %d\r\n",config.datasize); + c->obuf = sdscatprintf(c->obuf,"SET foo_rand000000000000 %d\r\n",config.datasize); { char *data = zmalloc(config.datasize+2); memset(data,'x',config.datasize); @@ -418,7 +444,7 @@ int main(int argc, char **argv) { prepareForBenchmark(); c = createClient(); if (!c) exit(1); - c->obuf = sdscat(c->obuf,"GET foo\r\n"); + c->obuf = sdscat(c->obuf,"GET foo_rand000000000000\r\n"); c->replytype = REPLY_BULK; c->readlen = -1; createMissingClients(c); @@ -428,7 +454,7 @@ int main(int argc, char **argv) { prepareForBenchmark(); c = createClient(); if (!c) exit(1); - c->obuf = sdscat(c->obuf,"INCR counter\r\n"); + c->obuf = sdscat(c->obuf,"INCR counter_rand000000000000\r\n"); c->replytype = REPLY_INT; createMissingClients(c); aeMain(config.el); @@ -453,6 +479,15 @@ int main(int argc, char **argv) { aeMain(config.el); endBenchmark("LPOP"); + prepareForBenchmark(); + c = createClient(); + if (!c) exit(1); + c->obuf = sdscat(c->obuf,"PING\r\n"); + c->replytype = REPLY_RETCODE; + createMissingClients(c); + aeMain(config.el); + endBenchmark("PING"); + printf("\n"); } while(config.loop);