1
0

benchmark-ping-pongs.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 uv_buf_t buf_alloc(uv_handle_t* tcp, size_t size) {
  45. buf_t* ab;
  46. ab = buf_freelist;
  47. if (ab != NULL) {
  48. buf_freelist = ab->next;
  49. return ab->uv_buf_t;
  50. }
  51. ab = (buf_t*) malloc(size + sizeof *ab);
  52. ab->uv_buf_t.len = size;
  53. ab->uv_buf_t.base = ((char*) ab) + sizeof *ab;
  54. return ab->uv_buf_t;
  55. }
  56. static void buf_free(uv_buf_t uv_buf_t) {
  57. buf_t* ab = (buf_t*) (uv_buf_t.base - sizeof *ab);
  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, ssize_t nread, uv_buf_t buf) {
  91. ssize_t i;
  92. pinger_t* pinger;
  93. pinger = (pinger_t*)tcp->data;
  94. if (nread < 0) {
  95. ASSERT(uv_last_error(loop).code == UV_EOF);
  96. if (buf.base) {
  97. buf_free(buf);
  98. }
  99. ASSERT(pinger_shutdown_cb_called == 1);
  100. uv_close((uv_handle_t*)tcp, pinger_close_cb);
  101. return;
  102. }
  103. /* Now we count the pings */
  104. for (i = 0; i < nread; i++) {
  105. ASSERT(buf.base[i] == PING[pinger->state]);
  106. pinger->state = (pinger->state + 1) % (sizeof(PING) - 1);
  107. if (pinger->state == 0) {
  108. pinger->pongs++;
  109. if (uv_now(loop) - start_time > TIME) {
  110. uv_shutdown(&pinger->shutdown_req, (uv_stream_t*) tcp, pinger_shutdown_cb);
  111. break;
  112. } else {
  113. pinger_write_ping(pinger);
  114. }
  115. }
  116. }
  117. buf_free(buf);
  118. }
  119. static void pinger_connect_cb(uv_connect_t* req, int status) {
  120. pinger_t *pinger = (pinger_t*)req->handle->data;
  121. ASSERT(status == 0);
  122. pinger_write_ping(pinger);
  123. if (uv_read_start(req->handle, buf_alloc, pinger_read_cb)) {
  124. FATAL("uv_read_start failed");
  125. }
  126. }
  127. static void pinger_new(void) {
  128. int r;
  129. struct sockaddr_in client_addr = uv_ip4_addr("0.0.0.0", 0);
  130. struct sockaddr_in server_addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  131. pinger_t *pinger;
  132. pinger = (pinger_t*)malloc(sizeof(*pinger));
  133. pinger->state = 0;
  134. pinger->pongs = 0;
  135. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  136. r = uv_tcp_init(loop, &pinger->tcp);
  137. ASSERT(!r);
  138. pinger->tcp.data = pinger;
  139. uv_tcp_bind(&pinger->tcp, client_addr);
  140. r = uv_tcp_connect(&pinger->connect_req, &pinger->tcp, server_addr, pinger_connect_cb);
  141. ASSERT(!r);
  142. }
  143. BENCHMARK_IMPL(ping_pongs) {
  144. loop = uv_default_loop();
  145. start_time = uv_now(loop);
  146. pinger_new();
  147. uv_run(loop, UV_RUN_DEFAULT);
  148. ASSERT(completed_pingers == 1);
  149. MAKE_VALGRIND_HAPPY();
  150. return 0;
  151. }