/benchmark.c

http://redis.googlecode.com/ · C · 460 lines · 380 code · 48 blank · 32 comment · 80 complexity · c96ce71da8d70bf4e46a1b92e594d1d5 MD5 · raw file

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