benchmark-ping-udp.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Copyright libuv project 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 "uv.h"
  22. #include "task.h"
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. /* Run the benchmark for this many ms */
  26. #define TIME 5000
  27. typedef struct {
  28. int pongs;
  29. int state;
  30. uv_udp_t udp;
  31. struct sockaddr_in server_addr;
  32. } pinger_t;
  33. typedef struct buf_s {
  34. uv_buf_t uv_buf_t;
  35. struct buf_s* next;
  36. } buf_t;
  37. static char PING[] = "PING\n";
  38. static uv_loop_t* loop;
  39. static int completed_pingers;
  40. static unsigned long completed_pings;
  41. static int64_t start_time;
  42. static void buf_alloc(uv_handle_t* tcp, size_t size, uv_buf_t* buf) {
  43. static char slab[64 * 1024];
  44. buf->base = slab;
  45. buf->len = sizeof(slab);
  46. }
  47. static void buf_free(const uv_buf_t* buf) {
  48. }
  49. static void pinger_close_cb(uv_handle_t* handle) {
  50. pinger_t* pinger;
  51. pinger = (pinger_t*)handle->data;
  52. #if DEBUG
  53. fprintf(stderr, "ping_pongs: %d roundtrips/s\n",
  54. pinger->pongs / (TIME / 1000));
  55. #endif
  56. completed_pings += pinger->pongs;
  57. completed_pingers++;
  58. free(pinger);
  59. }
  60. static void pinger_write_ping(pinger_t* pinger) {
  61. uv_buf_t buf;
  62. int r;
  63. buf = uv_buf_init(PING, sizeof(PING) - 1);
  64. r = uv_udp_try_send(&pinger->udp, &buf, 1,
  65. (const struct sockaddr*) &pinger->server_addr);
  66. if (r < 0)
  67. FATAL("uv_udp_send failed");
  68. }
  69. static void pinger_read_cb(uv_udp_t* udp,
  70. ssize_t nread,
  71. const uv_buf_t* buf,
  72. const struct sockaddr* addr,
  73. unsigned flags) {
  74. ssize_t i;
  75. pinger_t* pinger;
  76. pinger = (pinger_t*)udp->data;
  77. /* Now we count the pings */
  78. for (i = 0; i < nread; i++) {
  79. ASSERT(buf->base[i] == PING[pinger->state]);
  80. pinger->state = (pinger->state + 1) % (sizeof(PING) - 1);
  81. if (pinger->state == 0) {
  82. pinger->pongs++;
  83. if (uv_now(loop) - start_time > TIME) {
  84. uv_close((uv_handle_t*)udp, pinger_close_cb);
  85. break;
  86. }
  87. pinger_write_ping(pinger);
  88. }
  89. }
  90. buf_free(buf);
  91. }
  92. static void udp_pinger_new(void) {
  93. pinger_t* pinger = malloc(sizeof(*pinger));
  94. int r;
  95. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &pinger->server_addr));
  96. pinger->state = 0;
  97. pinger->pongs = 0;
  98. /* Try to do NUM_PINGS ping-pongs (connection-less). */
  99. r = uv_udp_init(loop, &pinger->udp);
  100. ASSERT(r == 0);
  101. pinger->udp.data = pinger;
  102. /* Start pinging */
  103. if (0 != uv_udp_recv_start(&pinger->udp, buf_alloc, pinger_read_cb)) {
  104. FATAL("uv_udp_read_start failed");
  105. }
  106. pinger_write_ping(pinger);
  107. }
  108. static int ping_udp(unsigned pingers) {
  109. unsigned i;
  110. loop = uv_default_loop();
  111. start_time = uv_now(loop);
  112. for (i = 0; i < pingers; ++i) {
  113. udp_pinger_new();
  114. }
  115. uv_run(loop, UV_RUN_DEFAULT);
  116. ASSERT(completed_pingers >= 1);
  117. fprintf(stderr, "ping_pongs: %d pingers, ~ %lu roundtrips/s\n",
  118. completed_pingers, completed_pings / (TIME/1000));
  119. MAKE_VALGRIND_HAPPY();
  120. return 0;
  121. }
  122. #define X(PINGERS) \
  123. BENCHMARK_IMPL(ping_udp##PINGERS) {\
  124. return ping_udp(PINGERS); \
  125. }
  126. X(1)
  127. X(10)
  128. X(100)
  129. #undef X