PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/benchmark.c

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