benchmark-million-async.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. struct async_container {
  24. unsigned async_events;
  25. unsigned handles_seen;
  26. uv_async_t async_handles[1024 * 1024];
  27. };
  28. static volatile int done;
  29. static uv_thread_t thread_id;
  30. static struct async_container* container;
  31. static unsigned fastrand(void) {
  32. static unsigned g = 0;
  33. g = g * 214013 + 2531011;
  34. return g;
  35. }
  36. static void thread_cb(void* arg) {
  37. unsigned i;
  38. while (done == 0) {
  39. i = fastrand() % ARRAY_SIZE(container->async_handles);
  40. uv_async_send(container->async_handles + i);
  41. }
  42. }
  43. static void async_cb(uv_async_t* handle, int status) {
  44. container->async_events++;
  45. handle->data = handle;
  46. }
  47. static void timer_cb(uv_timer_t* handle, int status) {
  48. unsigned i;
  49. done = 1;
  50. ASSERT(0 == uv_thread_join(&thread_id));
  51. for (i = 0; i < ARRAY_SIZE(container->async_handles); i++) {
  52. uv_async_t* handle = container->async_handles + i;
  53. if (handle->data != NULL)
  54. container->handles_seen++;
  55. uv_close((uv_handle_t*) handle, NULL);
  56. }
  57. uv_close((uv_handle_t*) handle, NULL);
  58. }
  59. BENCHMARK_IMPL(million_async) {
  60. uv_timer_t timer_handle;
  61. uv_async_t* handle;
  62. uv_loop_t* loop;
  63. int timeout;
  64. unsigned i;
  65. loop = uv_default_loop();
  66. timeout = 5000;
  67. container = malloc(sizeof(*container));
  68. ASSERT(container != NULL);
  69. container->async_events = 0;
  70. container->handles_seen = 0;
  71. for (i = 0; i < ARRAY_SIZE(container->async_handles); i++) {
  72. handle = container->async_handles + i;
  73. ASSERT(0 == uv_async_init(loop, handle, async_cb));
  74. handle->data = NULL;
  75. }
  76. ASSERT(0 == uv_timer_init(loop, &timer_handle));
  77. ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, timeout, 0));
  78. ASSERT(0 == uv_thread_create(&thread_id, thread_cb, NULL));
  79. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  80. printf("%s async events in %.1f seconds (%s/s, %s unique handles seen)\n",
  81. fmt(container->async_events),
  82. timeout / 1000.,
  83. fmt(container->async_events / (timeout / 1000.)),
  84. fmt(container->handles_seen));
  85. free(container);
  86. MAKE_VALGRIND_HAPPY();
  87. return 0;
  88. }