benchmark-pump.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "task.h"
  22. #include "uv.h"
  23. #include <math.h>
  24. #include <stdio.h>
  25. static int TARGET_CONNECTIONS;
  26. #define WRITE_BUFFER_SIZE 8192
  27. #define MAX_SIMULTANEOUS_CONNECTS 100
  28. #define PRINT_STATS 0
  29. #define STATS_INTERVAL 1000 /* msec */
  30. #define STATS_COUNT 5
  31. static void do_write(uv_stream_t*);
  32. static void maybe_connect_some();
  33. static uv_req_t* req_alloc();
  34. static void req_free(uv_req_t* uv_req);
  35. static uv_buf_t buf_alloc(uv_handle_t*, size_t size);
  36. static void buf_free(uv_buf_t uv_buf_t);
  37. static uv_loop_t* loop;
  38. static uv_tcp_t tcpServer;
  39. static uv_pipe_t pipeServer;
  40. static uv_stream_t* server;
  41. static struct sockaddr_in listen_addr;
  42. static struct sockaddr_in connect_addr;
  43. static int64_t start_time;
  44. static int max_connect_socket = 0;
  45. static int max_read_sockets = 0;
  46. static int read_sockets = 0;
  47. static int write_sockets = 0;
  48. static int64_t nrecv = 0;
  49. static int64_t nrecv_total = 0;
  50. static int64_t nsent = 0;
  51. static int64_t nsent_total = 0;
  52. static int stats_left = 0;
  53. static char write_buffer[WRITE_BUFFER_SIZE];
  54. /* Make this as large as you need. */
  55. #define MAX_WRITE_HANDLES 1000
  56. static stream_type type;
  57. static uv_tcp_t tcp_write_handles[MAX_WRITE_HANDLES];
  58. static uv_pipe_t pipe_write_handles[MAX_WRITE_HANDLES];
  59. static uv_timer_t timer_handle;
  60. static double gbit(int64_t bytes, int64_t passed_ms) {
  61. double gbits = ((double)bytes / (1024 * 1024 * 1024)) * 8;
  62. return gbits / ((double)passed_ms / 1000);
  63. }
  64. static void show_stats(uv_timer_t* handle, int status) {
  65. int64_t diff;
  66. int i;
  67. #if PRINT_STATS
  68. LOGF("connections: %d, write: %.1f gbit/s\n",
  69. write_sockets,
  70. gbit(nsent, STATS_INTERVAL));
  71. #endif
  72. /* Exit if the show is over */
  73. if (!--stats_left) {
  74. uv_update_time(loop);
  75. diff = uv_now(loop) - start_time;
  76. LOGF("%s_pump%d_client: %.1f gbit/s\n", type == TCP ? "tcp" : "pipe", write_sockets,
  77. gbit(nsent_total, diff));
  78. for (i = 0; i < write_sockets; i++) {
  79. uv_close(type == TCP ? (uv_handle_t*)&tcp_write_handles[i] : (uv_handle_t*)&pipe_write_handles[i], NULL);
  80. }
  81. exit(0);
  82. }
  83. /* Reset read and write counters */
  84. nrecv = 0;
  85. nsent = 0;
  86. }
  87. static void read_show_stats(void) {
  88. int64_t diff;
  89. uv_update_time(loop);
  90. diff = uv_now(loop) - start_time;
  91. LOGF("%s_pump%d_server: %.1f gbit/s\n", type == TCP ? "tcp" : "pipe", max_read_sockets,
  92. gbit(nrecv_total, diff));
  93. }
  94. static void read_sockets_close_cb(uv_handle_t* handle) {
  95. free(handle);
  96. read_sockets--;
  97. /* If it's past the first second and everyone has closed their connection
  98. * Then print stats.
  99. */
  100. if (uv_now(loop) - start_time > 1000 && read_sockets == 0) {
  101. read_show_stats();
  102. uv_close((uv_handle_t*)server, NULL);
  103. }
  104. }
  105. static void start_stats_collection(void) {
  106. int r;
  107. /* Show-stats timer */
  108. stats_left = STATS_COUNT;
  109. r = uv_timer_init(loop, &timer_handle);
  110. ASSERT(r == 0);
  111. r = uv_timer_start(&timer_handle, show_stats, STATS_INTERVAL, STATS_INTERVAL);
  112. ASSERT(r == 0);
  113. uv_update_time(loop);
  114. start_time = uv_now(loop);
  115. }
  116. static void read_cb(uv_stream_t* stream, ssize_t bytes, uv_buf_t buf) {
  117. if (nrecv_total == 0) {
  118. ASSERT(start_time == 0);
  119. uv_update_time(loop);
  120. start_time = uv_now(loop);
  121. }
  122. if (bytes < 0) {
  123. uv_close((uv_handle_t*)stream, read_sockets_close_cb);
  124. return;
  125. }
  126. buf_free(buf);
  127. nrecv += bytes;
  128. nrecv_total += bytes;
  129. }
  130. static void write_cb(uv_write_t* req, int status) {
  131. ASSERT(status == 0);
  132. req_free((uv_req_t*) req);
  133. nsent += sizeof write_buffer;
  134. nsent_total += sizeof write_buffer;
  135. do_write((uv_stream_t*) req->handle);
  136. }
  137. static void do_write(uv_stream_t* stream) {
  138. uv_write_t* req;
  139. uv_buf_t buf;
  140. int r;
  141. buf.base = (char*) &write_buffer;
  142. buf.len = sizeof write_buffer;
  143. req = (uv_write_t*) req_alloc();
  144. r = uv_write(req, stream, &buf, 1, write_cb);
  145. ASSERT(r == 0);
  146. }
  147. static void connect_cb(uv_connect_t* req, int status) {
  148. int i;
  149. if (status) LOG(uv_strerror(uv_last_error(loop)));
  150. ASSERT(status == 0);
  151. write_sockets++;
  152. req_free((uv_req_t*) req);
  153. maybe_connect_some();
  154. if (write_sockets == TARGET_CONNECTIONS) {
  155. start_stats_collection();
  156. /* Yay! start writing */
  157. for (i = 0; i < write_sockets; i++) {
  158. do_write(type == TCP ? (uv_stream_t*)&tcp_write_handles[i] : (uv_stream_t*)&pipe_write_handles[i]);
  159. }
  160. }
  161. }
  162. static void maybe_connect_some(void) {
  163. uv_connect_t* req;
  164. uv_tcp_t* tcp;
  165. uv_pipe_t* pipe;
  166. int r;
  167. while (max_connect_socket < TARGET_CONNECTIONS &&
  168. max_connect_socket < write_sockets + MAX_SIMULTANEOUS_CONNECTS) {
  169. if (type == TCP) {
  170. tcp = &tcp_write_handles[max_connect_socket++];
  171. r = uv_tcp_init(loop, tcp);
  172. ASSERT(r == 0);
  173. req = (uv_connect_t*) req_alloc();
  174. r = uv_tcp_connect(req, tcp, connect_addr, connect_cb);
  175. ASSERT(r == 0);
  176. } else {
  177. pipe = &pipe_write_handles[max_connect_socket++];
  178. r = uv_pipe_init(loop, pipe, 0);
  179. ASSERT(r == 0);
  180. req = (uv_connect_t*) req_alloc();
  181. uv_pipe_connect(req, pipe, TEST_PIPENAME, connect_cb);
  182. }
  183. }
  184. }
  185. static void connection_cb(uv_stream_t* s, int status) {
  186. uv_stream_t* stream;
  187. int r;
  188. ASSERT(server == s);
  189. ASSERT(status == 0);
  190. if (type == TCP) {
  191. stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t));
  192. r = uv_tcp_init(loop, (uv_tcp_t*)stream);
  193. ASSERT(r == 0);
  194. } else {
  195. stream = (uv_stream_t*)malloc(sizeof(uv_pipe_t));
  196. r = uv_pipe_init(loop, (uv_pipe_t*)stream, 0);
  197. ASSERT(r == 0);
  198. }
  199. r = uv_accept(s, stream);
  200. ASSERT(r == 0);
  201. r = uv_read_start(stream, buf_alloc, read_cb);
  202. ASSERT(r == 0);
  203. read_sockets++;
  204. max_read_sockets++;
  205. }
  206. /*
  207. * Request allocator
  208. */
  209. typedef struct req_list_s {
  210. union uv_any_req uv_req;
  211. struct req_list_s* next;
  212. } req_list_t;
  213. static req_list_t* req_freelist = NULL;
  214. static uv_req_t* req_alloc(void) {
  215. req_list_t* req;
  216. req = req_freelist;
  217. if (req != NULL) {
  218. req_freelist = req->next;
  219. return (uv_req_t*) req;
  220. }
  221. req = (req_list_t*) malloc(sizeof *req);
  222. return (uv_req_t*) req;
  223. }
  224. static void req_free(uv_req_t* uv_req) {
  225. req_list_t* req = (req_list_t*) uv_req;
  226. req->next = req_freelist;
  227. req_freelist = req;
  228. }
  229. /*
  230. * Buffer allocator
  231. */
  232. typedef struct buf_list_s {
  233. uv_buf_t uv_buf_t;
  234. struct buf_list_s* next;
  235. } buf_list_t;
  236. static buf_list_t* buf_freelist = NULL;
  237. static uv_buf_t buf_alloc(uv_handle_t* handle, size_t size) {
  238. buf_list_t* buf;
  239. buf = buf_freelist;
  240. if (buf != NULL) {
  241. buf_freelist = buf->next;
  242. return buf->uv_buf_t;
  243. }
  244. buf = (buf_list_t*) malloc(size + sizeof *buf);
  245. buf->uv_buf_t.len = (unsigned int)size;
  246. buf->uv_buf_t.base = ((char*) buf) + sizeof *buf;
  247. return buf->uv_buf_t;
  248. }
  249. static void buf_free(uv_buf_t uv_buf_t) {
  250. buf_list_t* buf = (buf_list_t*) (uv_buf_t.base - sizeof *buf);
  251. buf->next = buf_freelist;
  252. buf_freelist = buf;
  253. }
  254. HELPER_IMPL(tcp_pump_server) {
  255. int r;
  256. type = TCP;
  257. loop = uv_default_loop();
  258. listen_addr = uv_ip4_addr("0.0.0.0", TEST_PORT);
  259. /* Server */
  260. server = (uv_stream_t*)&tcpServer;
  261. r = uv_tcp_init(loop, &tcpServer);
  262. ASSERT(r == 0);
  263. r = uv_tcp_bind(&tcpServer, listen_addr);
  264. ASSERT(r == 0);
  265. r = uv_listen((uv_stream_t*)&tcpServer, MAX_WRITE_HANDLES, connection_cb);
  266. ASSERT(r == 0);
  267. uv_run(loop, UV_RUN_DEFAULT);
  268. return 0;
  269. }
  270. HELPER_IMPL(pipe_pump_server) {
  271. int r;
  272. type = PIPE;
  273. loop = uv_default_loop();
  274. /* Server */
  275. server = (uv_stream_t*)&pipeServer;
  276. r = uv_pipe_init(loop, &pipeServer, 0);
  277. ASSERT(r == 0);
  278. r = uv_pipe_bind(&pipeServer, TEST_PIPENAME);
  279. ASSERT(r == 0);
  280. r = uv_listen((uv_stream_t*)&pipeServer, MAX_WRITE_HANDLES, connection_cb);
  281. ASSERT(r == 0);
  282. uv_run(loop, UV_RUN_DEFAULT);
  283. MAKE_VALGRIND_HAPPY();
  284. return 0;
  285. }
  286. static void tcp_pump(int n) {
  287. ASSERT(n <= MAX_WRITE_HANDLES);
  288. TARGET_CONNECTIONS = n;
  289. type = TCP;
  290. loop = uv_default_loop();
  291. connect_addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  292. /* Start making connections */
  293. maybe_connect_some();
  294. uv_run(loop, UV_RUN_DEFAULT);
  295. MAKE_VALGRIND_HAPPY();
  296. }
  297. static void pipe_pump(int n) {
  298. ASSERT(n <= MAX_WRITE_HANDLES);
  299. TARGET_CONNECTIONS = n;
  300. type = PIPE;
  301. loop = uv_default_loop();
  302. /* Start making connections */
  303. maybe_connect_some();
  304. uv_run(loop, UV_RUN_DEFAULT);
  305. MAKE_VALGRIND_HAPPY();
  306. }
  307. BENCHMARK_IMPL(tcp_pump100_client) {
  308. tcp_pump(100);
  309. return 0;
  310. }
  311. BENCHMARK_IMPL(tcp_pump1_client) {
  312. tcp_pump(1);
  313. return 0;
  314. }
  315. BENCHMARK_IMPL(pipe_pump100_client) {
  316. pipe_pump(100);
  317. return 0;
  318. }
  319. BENCHMARK_IMPL(pipe_pump1_client) {
  320. pipe_pump(1);
  321. return 0;
  322. }