benchmark-async.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #define NUM_PINGS (1000 * 1000)
  26. struct ctx {
  27. uv_loop_t* loop;
  28. uv_thread_t thread;
  29. uv_async_t main_async; /* wake up main thread */
  30. uv_async_t worker_async; /* wake up worker */
  31. unsigned int nthreads;
  32. unsigned int main_sent;
  33. unsigned int main_seen;
  34. unsigned int worker_sent;
  35. unsigned int worker_seen;
  36. };
  37. static void worker_async_cb(uv_async_t* handle, int status) {
  38. struct ctx* ctx = container_of(handle, struct ctx, worker_async);
  39. ASSERT(0 == uv_async_send(&ctx->main_async));
  40. ctx->worker_sent++;
  41. ctx->worker_seen++;
  42. if (ctx->worker_sent >= NUM_PINGS)
  43. uv_close((uv_handle_t*) &ctx->worker_async, NULL);
  44. }
  45. static void main_async_cb(uv_async_t* handle, int status) {
  46. struct ctx* ctx = container_of(handle, struct ctx, main_async);
  47. ASSERT(0 == uv_async_send(&ctx->worker_async));
  48. ctx->main_sent++;
  49. ctx->main_seen++;
  50. if (ctx->main_sent >= NUM_PINGS)
  51. uv_close((uv_handle_t*) &ctx->main_async, NULL);
  52. }
  53. static void worker(void* arg) {
  54. struct ctx* ctx = arg;
  55. ASSERT(0 == uv_async_send(&ctx->main_async));
  56. ASSERT(0 == uv_run(ctx->loop, UV_RUN_DEFAULT));
  57. }
  58. static int test_async(int nthreads) {
  59. struct ctx* threads;
  60. struct ctx* ctx;
  61. uint64_t time;
  62. int i;
  63. threads = calloc(nthreads, sizeof(threads[0]));
  64. ASSERT(threads != NULL);
  65. for (i = 0; i < nthreads; i++) {
  66. ctx = threads + i;
  67. ctx->nthreads = nthreads;
  68. ctx->loop = uv_loop_new();
  69. ASSERT(ctx->loop != NULL);
  70. ASSERT(0 == uv_async_init(ctx->loop, &ctx->worker_async, worker_async_cb));
  71. ASSERT(0 == uv_async_init(uv_default_loop(), &ctx->main_async, main_async_cb));
  72. ASSERT(0 == uv_thread_create(&ctx->thread, worker, ctx));
  73. }
  74. time = uv_hrtime();
  75. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  76. for (i = 0; i < nthreads; i++)
  77. ASSERT(0 == uv_thread_join(&threads[i].thread));
  78. time = uv_hrtime() - time;
  79. for (i = 0; i < nthreads; i++) {
  80. ctx = threads + i;
  81. ASSERT(ctx->worker_sent == NUM_PINGS);
  82. ASSERT(ctx->worker_seen == NUM_PINGS);
  83. ASSERT(ctx->main_sent == (unsigned int) NUM_PINGS);
  84. ASSERT(ctx->main_seen == (unsigned int) NUM_PINGS);
  85. }
  86. printf("async%d: %.2f sec (%s/sec)\n",
  87. nthreads,
  88. time / 1e9,
  89. fmt(NUM_PINGS / (time / 1e9)));
  90. free(threads);
  91. MAKE_VALGRIND_HAPPY();
  92. return 0;
  93. }
  94. BENCHMARK_IMPL(async1) {
  95. return test_async(1);
  96. }
  97. BENCHMARK_IMPL(async2) {
  98. return test_async(2);
  99. }
  100. BENCHMARK_IMPL(async4) {
  101. return test_async(4);
  102. }
  103. BENCHMARK_IMPL(async8) {
  104. return test_async(8);
  105. }