benchmark-udp-pummel.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #define EXPECTED "RANG TANG DING DONG I AM THE JAPANESE SANDMAN"
  27. #define TEST_DURATION 5000 /* ms */
  28. #define BASE_PORT 12345
  29. struct sender_state {
  30. struct sockaddr_in addr;
  31. uv_udp_send_t send_req;
  32. uv_udp_t udp_handle;
  33. };
  34. struct receiver_state {
  35. struct sockaddr_in addr;
  36. uv_udp_t udp_handle;
  37. };
  38. /* not used in timed mode */
  39. static unsigned int packet_counter = (unsigned int) 1e6;
  40. static int n_senders_;
  41. static int n_receivers_;
  42. static uv_buf_t bufs[5];
  43. static struct sender_state senders[1024];
  44. static struct receiver_state receivers[1024];
  45. static unsigned int send_cb_called;
  46. static unsigned int recv_cb_called;
  47. static unsigned int close_cb_called;
  48. static int timed;
  49. static int exiting;
  50. static void alloc_cb(uv_handle_t* handle,
  51. size_t suggested_size,
  52. uv_buf_t* buf) {
  53. static char slab[65536];
  54. ASSERT(suggested_size <= sizeof(slab));
  55. buf->base = slab;
  56. buf->len = sizeof(slab);
  57. }
  58. static void send_cb(uv_udp_send_t* req, int status) {
  59. struct sender_state* s;
  60. ASSERT(req != NULL);
  61. if (status != 0) {
  62. ASSERT(status == UV_ECANCELED);
  63. return;
  64. }
  65. if (exiting)
  66. return;
  67. s = container_of(req, struct sender_state, send_req);
  68. ASSERT(req->handle == &s->udp_handle);
  69. if (timed)
  70. goto send;
  71. if (packet_counter == 0) {
  72. uv_close((uv_handle_t*)&s->udp_handle, NULL);
  73. return;
  74. }
  75. packet_counter--;
  76. send:
  77. ASSERT(0 == uv_udp_send(&s->send_req,
  78. &s->udp_handle,
  79. bufs,
  80. ARRAY_SIZE(bufs),
  81. (const struct sockaddr*) &s->addr,
  82. send_cb));
  83. send_cb_called++;
  84. }
  85. static void recv_cb(uv_udp_t* handle,
  86. ssize_t nread,
  87. const uv_buf_t* buf,
  88. const struct sockaddr* addr,
  89. unsigned flags) {
  90. if (nread == 0)
  91. return;
  92. if (nread < 0) {
  93. ASSERT(nread == UV_ECANCELED);
  94. return;
  95. }
  96. ASSERT(addr->sa_family == AF_INET);
  97. ASSERT(!memcmp(buf->base, EXPECTED, nread));
  98. recv_cb_called++;
  99. }
  100. static void close_cb(uv_handle_t* handle) {
  101. ASSERT(handle != NULL);
  102. close_cb_called++;
  103. }
  104. static void timeout_cb(uv_timer_t* timer, int status) {
  105. int i;
  106. exiting = 1;
  107. for (i = 0; i < n_senders_; i++)
  108. uv_close((uv_handle_t*)&senders[i].udp_handle, close_cb);
  109. for (i = 0; i < n_receivers_; i++)
  110. uv_close((uv_handle_t*)&receivers[i].udp_handle, close_cb);
  111. }
  112. static int pummel(unsigned int n_senders,
  113. unsigned int n_receivers,
  114. unsigned long timeout) {
  115. uv_timer_t timer_handle;
  116. uint64_t duration;
  117. uv_loop_t* loop;
  118. unsigned int i;
  119. ASSERT(n_senders <= ARRAY_SIZE(senders));
  120. ASSERT(n_receivers <= ARRAY_SIZE(receivers));
  121. loop = uv_default_loop();
  122. n_senders_ = n_senders;
  123. n_receivers_ = n_receivers;
  124. if (timeout) {
  125. ASSERT(0 == uv_timer_init(loop, &timer_handle));
  126. ASSERT(0 == uv_timer_start(&timer_handle, timeout_cb, timeout, 0));
  127. /* Timer should not keep loop alive. */
  128. uv_unref((uv_handle_t*)&timer_handle);
  129. timed = 1;
  130. }
  131. for (i = 0; i < n_receivers; i++) {
  132. struct receiver_state* s = receivers + i;
  133. struct sockaddr_in addr;
  134. ASSERT(0 == uv_ip4_addr("0.0.0.0", BASE_PORT + i, &addr));
  135. ASSERT(0 == uv_udp_init(loop, &s->udp_handle));
  136. ASSERT(0 == uv_udp_bind(&s->udp_handle, (const struct sockaddr*) &addr, 0));
  137. ASSERT(0 == uv_udp_recv_start(&s->udp_handle, alloc_cb, recv_cb));
  138. uv_unref((uv_handle_t*)&s->udp_handle);
  139. }
  140. bufs[0] = uv_buf_init(EXPECTED + 0, 10);
  141. bufs[1] = uv_buf_init(EXPECTED + 10, 10);
  142. bufs[2] = uv_buf_init(EXPECTED + 20, 10);
  143. bufs[3] = uv_buf_init(EXPECTED + 30, 10);
  144. bufs[4] = uv_buf_init(EXPECTED + 40, 5);
  145. for (i = 0; i < n_senders; i++) {
  146. struct sender_state* s = senders + i;
  147. ASSERT(0 == uv_ip4_addr("127.0.0.1",
  148. BASE_PORT + (i % n_receivers),
  149. &s->addr));
  150. ASSERT(0 == uv_udp_init(loop, &s->udp_handle));
  151. ASSERT(0 == uv_udp_send(&s->send_req,
  152. &s->udp_handle,
  153. bufs,
  154. ARRAY_SIZE(bufs),
  155. (const struct sockaddr*) &s->addr,
  156. send_cb));
  157. }
  158. duration = uv_hrtime();
  159. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  160. duration = uv_hrtime() - duration;
  161. /* convert from nanoseconds to milliseconds */
  162. duration = duration / (uint64_t) 1e6;
  163. printf("udp_pummel_%dv%d: %.0f/s received, %.0f/s sent. "
  164. "%u received, %u sent in %.1f seconds.\n",
  165. n_receivers,
  166. n_senders,
  167. recv_cb_called / (duration / 1000.0),
  168. send_cb_called / (duration / 1000.0),
  169. recv_cb_called,
  170. send_cb_called,
  171. duration / 1000.0);
  172. MAKE_VALGRIND_HAPPY();
  173. return 0;
  174. }
  175. #define X(a, b) \
  176. BENCHMARK_IMPL(udp_pummel_##a##v##b) { \
  177. return pummel(a, b, 0); \
  178. } \
  179. BENCHMARK_IMPL(udp_timed_pummel_##a##v##b) { \
  180. return pummel(a, b, TEST_DURATION); \
  181. }
  182. X(1, 1)
  183. X(1, 10)
  184. X(1, 100)
  185. X(1, 1000)
  186. X(10, 10)
  187. X(10, 100)
  188. X(10, 1000)
  189. X(100, 10)
  190. X(100, 100)
  191. X(100, 1000)
  192. X(1000, 1000)
  193. #undef X