benchmark-udp-pummel.c 6.5 KB

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