1
0

benchmark-tcp-write-batch.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <stdio.h>
  24. #include <stdlib.h>
  25. #define WRITE_REQ_DATA "Hello, world."
  26. #define NUM_WRITE_REQS (1000 * 1000)
  27. typedef struct {
  28. uv_write_t req;
  29. uv_buf_t buf;
  30. } write_req;
  31. static write_req* write_reqs;
  32. static uv_tcp_t tcp_client;
  33. static uv_connect_t connect_req;
  34. static uv_shutdown_t shutdown_req;
  35. static int shutdown_cb_called = 0;
  36. static int connect_cb_called = 0;
  37. static int write_cb_called = 0;
  38. static int close_cb_called = 0;
  39. static void connect_cb(uv_connect_t* req, int status);
  40. static void write_cb(uv_write_t* req, int status);
  41. static void shutdown_cb(uv_shutdown_t* req, int status);
  42. static void close_cb(uv_handle_t* handle);
  43. static void connect_cb(uv_connect_t* req, int status) {
  44. write_req* w;
  45. int i;
  46. int r;
  47. ASSERT(req->handle == (uv_stream_t*)&tcp_client);
  48. for (i = 0; i < NUM_WRITE_REQS; i++) {
  49. w = &write_reqs[i];
  50. r = uv_write(&w->req, req->handle, &w->buf, 1, write_cb);
  51. ASSERT(r == 0);
  52. }
  53. r = uv_shutdown(&shutdown_req, req->handle, shutdown_cb);
  54. ASSERT(r == 0);
  55. connect_cb_called++;
  56. }
  57. static void write_cb(uv_write_t* req, int status) {
  58. ASSERT(req != NULL);
  59. ASSERT(status == 0);
  60. write_cb_called++;
  61. }
  62. static void shutdown_cb(uv_shutdown_t* req, int status) {
  63. ASSERT(req->handle == (uv_stream_t*)&tcp_client);
  64. ASSERT(req->handle->write_queue_size == 0);
  65. uv_close((uv_handle_t*)req->handle, close_cb);
  66. free(write_reqs);
  67. shutdown_cb_called++;
  68. }
  69. static void close_cb(uv_handle_t* handle) {
  70. ASSERT(handle == (uv_handle_t*)&tcp_client);
  71. close_cb_called++;
  72. }
  73. BENCHMARK_IMPL(tcp_write_batch) {
  74. struct sockaddr_in addr;
  75. uv_loop_t* loop;
  76. uint64_t start;
  77. uint64_t stop;
  78. int i;
  79. int r;
  80. write_reqs = malloc(sizeof(*write_reqs) * NUM_WRITE_REQS);
  81. ASSERT(write_reqs != NULL);
  82. /* Prepare the data to write out. */
  83. for (i = 0; i < NUM_WRITE_REQS; i++) {
  84. write_reqs[i].buf = uv_buf_init(WRITE_REQ_DATA,
  85. sizeof(WRITE_REQ_DATA) - 1);
  86. }
  87. loop = uv_default_loop();
  88. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  89. r = uv_tcp_init(loop, &tcp_client);
  90. ASSERT(r == 0);
  91. r = uv_tcp_connect(&connect_req,
  92. &tcp_client,
  93. (const struct sockaddr*) &addr,
  94. connect_cb);
  95. ASSERT(r == 0);
  96. start = uv_hrtime();
  97. r = uv_run(loop, UV_RUN_DEFAULT);
  98. ASSERT(r == 0);
  99. stop = uv_hrtime();
  100. ASSERT(connect_cb_called == 1);
  101. ASSERT(write_cb_called == NUM_WRITE_REQS);
  102. ASSERT(shutdown_cb_called == 1);
  103. ASSERT(close_cb_called == 1);
  104. printf("%ld write requests in %.2fs.\n",
  105. (long)NUM_WRITE_REQS,
  106. (stop - start) / 1e9);
  107. MAKE_VALGRIND_HAPPY();
  108. return 0;
  109. }