1
0

benchmark-ping-pongs.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "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_tcp_t tcp;
  31. uv_connect_t connect_req;
  32. uv_shutdown_t shutdown_req;
  33. } pinger_t;
  34. typedef struct buf_s {
  35. uv_buf_t uv_buf_t;
  36. struct buf_s* next;
  37. } buf_t;
  38. static char PING[] = "PING\n";
  39. static uv_loop_t* loop;
  40. static buf_t* buf_freelist = NULL;
  41. static int pinger_shutdown_cb_called;
  42. static int completed_pingers = 0;
  43. static int64_t start_time;
  44. static void buf_alloc(uv_handle_t* tcp, size_t size, uv_buf_t* buf) {
  45. buf_t* ab;
  46. ab = buf_freelist;
  47. if (ab != NULL)
  48. buf_freelist = ab->next;
  49. else {
  50. ab = malloc(size + sizeof(*ab));
  51. ab->uv_buf_t.len = size;
  52. ab->uv_buf_t.base = (char*) (ab + 1);
  53. }
  54. *buf = ab->uv_buf_t;
  55. }
  56. static void buf_free(const uv_buf_t* buf) {
  57. buf_t* ab = (buf_t*) buf->base - 1;
  58. ab->next = buf_freelist;
  59. buf_freelist = ab;
  60. }
  61. static void pinger_close_cb(uv_handle_t* handle) {
  62. pinger_t* pinger;
  63. pinger = (pinger_t*)handle->data;
  64. LOGF("ping_pongs: %d roundtrips/s\n", (1000 * pinger->pongs) / TIME);
  65. free(pinger);
  66. completed_pingers++;
  67. }
  68. static void pinger_write_cb(uv_write_t* req, int status) {
  69. ASSERT(status == 0);
  70. free(req);
  71. }
  72. static void pinger_write_ping(pinger_t* pinger) {
  73. uv_write_t* req;
  74. uv_buf_t buf;
  75. buf = uv_buf_init(PING, sizeof(PING) - 1);
  76. req = malloc(sizeof *req);
  77. if (uv_write(req, (uv_stream_t*) &pinger->tcp, &buf, 1, pinger_write_cb)) {
  78. FATAL("uv_write failed");
  79. }
  80. }
  81. static void pinger_shutdown_cb(uv_shutdown_t* req, int status) {
  82. ASSERT(status == 0);
  83. pinger_shutdown_cb_called++;
  84. /*
  85. * The close callback has not been triggered yet. We must wait for EOF
  86. * until we close the connection.
  87. */
  88. ASSERT(completed_pingers == 0);
  89. }
  90. static void pinger_read_cb(uv_stream_t* tcp,
  91. ssize_t nread,
  92. const uv_buf_t* buf) {
  93. ssize_t i;
  94. pinger_t* pinger;
  95. pinger = (pinger_t*)tcp->data;
  96. if (nread < 0) {
  97. ASSERT(nread == UV_EOF);
  98. if (buf->base) {
  99. buf_free(buf);
  100. }
  101. ASSERT(pinger_shutdown_cb_called == 1);
  102. uv_close((uv_handle_t*)tcp, pinger_close_cb);
  103. return;
  104. }
  105. /* Now we count the pings */
  106. for (i = 0; i < nread; i++) {
  107. ASSERT(buf->base[i] == PING[pinger->state]);
  108. pinger->state = (pinger->state + 1) % (sizeof(PING) - 1);
  109. if (pinger->state == 0) {
  110. pinger->pongs++;
  111. if (uv_now(loop) - start_time > TIME) {
  112. uv_shutdown(&pinger->shutdown_req,
  113. (uv_stream_t*) tcp,
  114. pinger_shutdown_cb);
  115. break;
  116. } else {
  117. pinger_write_ping(pinger);
  118. }
  119. }
  120. }
  121. buf_free(buf);
  122. }
  123. static void pinger_connect_cb(uv_connect_t* req, int status) {
  124. pinger_t *pinger = (pinger_t*)req->handle->data;
  125. ASSERT(status == 0);
  126. pinger_write_ping(pinger);
  127. if (uv_read_start(req->handle, buf_alloc, pinger_read_cb)) {
  128. FATAL("uv_read_start failed");
  129. }
  130. }
  131. static void pinger_new(void) {
  132. struct sockaddr_in client_addr;
  133. struct sockaddr_in server_addr;
  134. pinger_t *pinger;
  135. int r;
  136. ASSERT(0 == uv_ip4_addr("0.0.0.0", 0, &client_addr));
  137. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr));
  138. pinger = malloc(sizeof(*pinger));
  139. pinger->state = 0;
  140. pinger->pongs = 0;
  141. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  142. r = uv_tcp_init(loop, &pinger->tcp);
  143. ASSERT(!r);
  144. pinger->tcp.data = pinger;
  145. ASSERT(0 == uv_tcp_bind(&pinger->tcp,
  146. (const struct sockaddr*) &client_addr,
  147. 0));
  148. r = uv_tcp_connect(&pinger->connect_req,
  149. &pinger->tcp,
  150. (const struct sockaddr*) &server_addr,
  151. pinger_connect_cb);
  152. ASSERT(!r);
  153. }
  154. BENCHMARK_IMPL(ping_pongs) {
  155. loop = uv_default_loop();
  156. start_time = uv_now(loop);
  157. pinger_new();
  158. uv_run(loop, UV_RUN_DEFAULT);
  159. ASSERT(completed_pingers == 1);
  160. MAKE_VALGRIND_HAPPY();
  161. return 0;
  162. }