benchmark-pump.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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(void);
  33. static uv_req_t* req_alloc(void);
  34. static void req_free(uv_req_t* uv_req);
  35. static void buf_alloc(uv_handle_t* handle, size_t size, uv_buf_t* buf);
  36. static void buf_free(const uv_buf_t* buf);
  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) {
  65. int64_t diff;
  66. int i;
  67. #if PRINT_STATS
  68. fprintf(stderr, "connections: %d, write: %.1f gbit/s\n",
  69. write_sockets,
  70. gbit(nsent, STATS_INTERVAL));
  71. fflush(stderr);
  72. #endif
  73. /* Exit if the show is over */
  74. if (!--stats_left) {
  75. uv_update_time(loop);
  76. diff = uv_now(loop) - start_time;
  77. fprintf(stderr, "%s_pump%d_client: %.1f gbit/s\n",
  78. type == TCP ? "tcp" : "pipe",
  79. write_sockets,
  80. gbit(nsent_total, diff));
  81. fflush(stderr);
  82. for (i = 0; i < write_sockets; i++) {
  83. if (type == TCP)
  84. uv_close((uv_handle_t*) &tcp_write_handles[i], NULL);
  85. else
  86. uv_close((uv_handle_t*) &pipe_write_handles[i], NULL);
  87. }
  88. exit(0);
  89. }
  90. /* Reset read and write counters */
  91. nrecv = 0;
  92. nsent = 0;
  93. }
  94. static void read_show_stats(void) {
  95. int64_t diff;
  96. uv_update_time(loop);
  97. diff = uv_now(loop) - start_time;
  98. fprintf(stderr, "%s_pump%d_server: %.1f gbit/s\n",
  99. type == TCP ? "tcp" : "pipe",
  100. max_read_sockets,
  101. gbit(nrecv_total, diff));
  102. fflush(stderr);
  103. }
  104. static void read_sockets_close_cb(uv_handle_t* handle) {
  105. free(handle);
  106. read_sockets--;
  107. /* If it's past the first second and everyone has closed their connection
  108. * Then print stats.
  109. */
  110. if (uv_now(loop) - start_time > 1000 && read_sockets == 0) {
  111. read_show_stats();
  112. uv_close((uv_handle_t*)server, NULL);
  113. }
  114. }
  115. static void start_stats_collection(void) {
  116. int r;
  117. /* Show-stats timer */
  118. stats_left = STATS_COUNT;
  119. r = uv_timer_init(loop, &timer_handle);
  120. ASSERT(r == 0);
  121. r = uv_timer_start(&timer_handle, show_stats, STATS_INTERVAL, STATS_INTERVAL);
  122. ASSERT(r == 0);
  123. uv_update_time(loop);
  124. start_time = uv_now(loop);
  125. }
  126. static void read_cb(uv_stream_t* stream, ssize_t bytes, const uv_buf_t* buf) {
  127. if (nrecv_total == 0) {
  128. ASSERT(start_time == 0);
  129. uv_update_time(loop);
  130. start_time = uv_now(loop);
  131. }
  132. if (bytes < 0) {
  133. uv_close((uv_handle_t*)stream, read_sockets_close_cb);
  134. return;
  135. }
  136. buf_free(buf);
  137. nrecv += bytes;
  138. nrecv_total += bytes;
  139. }
  140. static void write_cb(uv_write_t* req, int status) {
  141. ASSERT(status == 0);
  142. req_free((uv_req_t*) req);
  143. nsent += sizeof write_buffer;
  144. nsent_total += sizeof write_buffer;
  145. do_write((uv_stream_t*) req->handle);
  146. }
  147. static void do_write(uv_stream_t* stream) {
  148. uv_write_t* req;
  149. uv_buf_t buf;
  150. int r;
  151. buf.base = (char*) &write_buffer;
  152. buf.len = sizeof write_buffer;
  153. req = (uv_write_t*) req_alloc();
  154. r = uv_write(req, stream, &buf, 1, write_cb);
  155. ASSERT(r == 0);
  156. }
  157. static void connect_cb(uv_connect_t* req, int status) {
  158. int i;
  159. if (status) {
  160. fprintf(stderr, "%s", uv_strerror(status));
  161. fflush(stderr);
  162. }
  163. ASSERT(status == 0);
  164. write_sockets++;
  165. req_free((uv_req_t*) req);
  166. maybe_connect_some();
  167. if (write_sockets == TARGET_CONNECTIONS) {
  168. start_stats_collection();
  169. /* Yay! start writing */
  170. for (i = 0; i < write_sockets; i++) {
  171. if (type == TCP)
  172. do_write((uv_stream_t*) &tcp_write_handles[i]);
  173. else
  174. do_write((uv_stream_t*) &pipe_write_handles[i]);
  175. }
  176. }
  177. }
  178. static void maybe_connect_some(void) {
  179. uv_connect_t* req;
  180. uv_tcp_t* tcp;
  181. uv_pipe_t* pipe;
  182. int r;
  183. while (max_connect_socket < TARGET_CONNECTIONS &&
  184. max_connect_socket < write_sockets + MAX_SIMULTANEOUS_CONNECTS) {
  185. if (type == TCP) {
  186. tcp = &tcp_write_handles[max_connect_socket++];
  187. r = uv_tcp_init(loop, tcp);
  188. ASSERT(r == 0);
  189. req = (uv_connect_t*) req_alloc();
  190. r = uv_tcp_connect(req,
  191. tcp,
  192. (const struct sockaddr*) &connect_addr,
  193. connect_cb);
  194. ASSERT(r == 0);
  195. } else {
  196. pipe = &pipe_write_handles[max_connect_socket++];
  197. r = uv_pipe_init(loop, pipe, 0);
  198. ASSERT(r == 0);
  199. req = (uv_connect_t*) req_alloc();
  200. uv_pipe_connect(req, pipe, TEST_PIPENAME, connect_cb);
  201. }
  202. }
  203. }
  204. static void connection_cb(uv_stream_t* s, int status) {
  205. uv_stream_t* stream;
  206. int r;
  207. ASSERT(server == s);
  208. ASSERT(status == 0);
  209. if (type == TCP) {
  210. stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t));
  211. r = uv_tcp_init(loop, (uv_tcp_t*)stream);
  212. ASSERT(r == 0);
  213. } else {
  214. stream = (uv_stream_t*)malloc(sizeof(uv_pipe_t));
  215. r = uv_pipe_init(loop, (uv_pipe_t*)stream, 0);
  216. ASSERT(r == 0);
  217. }
  218. r = uv_accept(s, stream);
  219. ASSERT(r == 0);
  220. r = uv_read_start(stream, buf_alloc, read_cb);
  221. ASSERT(r == 0);
  222. read_sockets++;
  223. max_read_sockets++;
  224. }
  225. /*
  226. * Request allocator
  227. */
  228. typedef struct req_list_s {
  229. union uv_any_req uv_req;
  230. struct req_list_s* next;
  231. } req_list_t;
  232. static req_list_t* req_freelist = NULL;
  233. static uv_req_t* req_alloc(void) {
  234. req_list_t* req;
  235. req = req_freelist;
  236. if (req != NULL) {
  237. req_freelist = req->next;
  238. return (uv_req_t*) req;
  239. }
  240. req = (req_list_t*) malloc(sizeof *req);
  241. return (uv_req_t*) req;
  242. }
  243. static void req_free(uv_req_t* uv_req) {
  244. req_list_t* req = (req_list_t*) uv_req;
  245. req->next = req_freelist;
  246. req_freelist = req;
  247. }
  248. /*
  249. * Buffer allocator
  250. */
  251. typedef struct buf_list_s {
  252. uv_buf_t uv_buf_t;
  253. struct buf_list_s* next;
  254. } buf_list_t;
  255. static buf_list_t* buf_freelist = NULL;
  256. static void buf_alloc(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
  257. buf_list_t* ab;
  258. ab = buf_freelist;
  259. if (ab != NULL)
  260. buf_freelist = ab->next;
  261. else {
  262. ab = malloc(size + sizeof(*ab));
  263. ab->uv_buf_t.len = size;
  264. ab->uv_buf_t.base = (char*) (ab + 1);
  265. }
  266. *buf = ab->uv_buf_t;
  267. }
  268. static void buf_free(const uv_buf_t* buf) {
  269. buf_list_t* ab = (buf_list_t*) buf->base - 1;
  270. ab->next = buf_freelist;
  271. buf_freelist = ab;
  272. }
  273. HELPER_IMPL(tcp_pump_server) {
  274. int r;
  275. type = TCP;
  276. loop = uv_default_loop();
  277. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &listen_addr));
  278. /* Server */
  279. server = (uv_stream_t*)&tcpServer;
  280. r = uv_tcp_init(loop, &tcpServer);
  281. ASSERT(r == 0);
  282. r = uv_tcp_bind(&tcpServer, (const struct sockaddr*) &listen_addr, 0);
  283. ASSERT(r == 0);
  284. r = uv_listen((uv_stream_t*)&tcpServer, MAX_WRITE_HANDLES, connection_cb);
  285. ASSERT(r == 0);
  286. uv_run(loop, UV_RUN_DEFAULT);
  287. return 0;
  288. }
  289. HELPER_IMPL(pipe_pump_server) {
  290. int r;
  291. type = PIPE;
  292. loop = uv_default_loop();
  293. /* Server */
  294. server = (uv_stream_t*)&pipeServer;
  295. r = uv_pipe_init(loop, &pipeServer, 0);
  296. ASSERT(r == 0);
  297. r = uv_pipe_bind(&pipeServer, TEST_PIPENAME);
  298. ASSERT(r == 0);
  299. r = uv_listen((uv_stream_t*)&pipeServer, MAX_WRITE_HANDLES, connection_cb);
  300. ASSERT(r == 0);
  301. uv_run(loop, UV_RUN_DEFAULT);
  302. MAKE_VALGRIND_HAPPY();
  303. return 0;
  304. }
  305. static void tcp_pump(int n) {
  306. ASSERT(n <= MAX_WRITE_HANDLES);
  307. TARGET_CONNECTIONS = n;
  308. type = TCP;
  309. loop = uv_default_loop();
  310. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &connect_addr));
  311. /* Start making connections */
  312. maybe_connect_some();
  313. uv_run(loop, UV_RUN_DEFAULT);
  314. MAKE_VALGRIND_HAPPY();
  315. }
  316. static void pipe_pump(int n) {
  317. ASSERT(n <= MAX_WRITE_HANDLES);
  318. TARGET_CONNECTIONS = n;
  319. type = PIPE;
  320. loop = uv_default_loop();
  321. /* Start making connections */
  322. maybe_connect_some();
  323. uv_run(loop, UV_RUN_DEFAULT);
  324. MAKE_VALGRIND_HAPPY();
  325. }
  326. BENCHMARK_IMPL(tcp_pump100_client) {
  327. tcp_pump(100);
  328. return 0;
  329. }
  330. BENCHMARK_IMPL(tcp_pump1_client) {
  331. tcp_pump(1);
  332. return 0;
  333. }
  334. BENCHMARK_IMPL(pipe_pump100_client) {
  335. pipe_pump(100);
  336. return 0;
  337. }
  338. BENCHMARK_IMPL(pipe_pump1_client) {
  339. pipe_pump(1);
  340. return 0;
  341. }