benchmark-loop-count.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_TICKS (2 * 1000 * 1000)
  26. static unsigned long ticks;
  27. static uv_idle_t idle_handle;
  28. static uv_timer_t timer_handle;
  29. static void idle_cb(uv_idle_t* handle, int status) {
  30. if (++ticks == NUM_TICKS)
  31. uv_idle_stop(handle);
  32. }
  33. static void idle2_cb(uv_idle_t* handle, int status) {
  34. ticks++;
  35. }
  36. static void timer_cb(uv_timer_t* handle, int status) {
  37. uv_idle_stop(&idle_handle);
  38. uv_timer_stop(&timer_handle);
  39. }
  40. BENCHMARK_IMPL(loop_count) {
  41. uv_loop_t* loop = uv_default_loop();
  42. uint64_t ns;
  43. uv_idle_init(loop, &idle_handle);
  44. uv_idle_start(&idle_handle, idle_cb);
  45. ns = uv_hrtime();
  46. uv_run(loop, UV_RUN_DEFAULT);
  47. ns = uv_hrtime() - ns;
  48. ASSERT(ticks == NUM_TICKS);
  49. LOGF("loop_count: %d ticks in %.2fs (%.0f/s)\n",
  50. NUM_TICKS,
  51. ns / 1e9,
  52. NUM_TICKS / (ns / 1e9));
  53. MAKE_VALGRIND_HAPPY();
  54. return 0;
  55. }
  56. BENCHMARK_IMPL(loop_count_timed) {
  57. uv_loop_t* loop = uv_default_loop();
  58. uv_idle_init(loop, &idle_handle);
  59. uv_idle_start(&idle_handle, idle2_cb);
  60. uv_timer_init(loop, &timer_handle);
  61. uv_timer_start(&timer_handle, timer_cb, 5000, 0);
  62. uv_run(loop, UV_RUN_DEFAULT);
  63. LOGF("loop_count: %lu ticks (%.0f ticks/s)\n", ticks, ticks / 5.0);
  64. MAKE_VALGRIND_HAPPY();
  65. return 0;
  66. }