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
22 #include "task.h"
23 #include "uv.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #define NUM_TICKS (2 * 1000 * 1000)
29
30 static unsigned long ticks;
31 static uv_idle_t idle_handle;
32 static uv_timer_t timer_handle;
33
34
idle_cb(uv_idle_t * handle)35 static void idle_cb(uv_idle_t* handle) {
36 if (++ticks == NUM_TICKS)
37 uv_idle_stop(handle);
38 }
39
40
idle2_cb(uv_idle_t * handle)41 static void idle2_cb(uv_idle_t* handle) {
42 ticks++;
43 }
44
45
timer_cb(uv_timer_t * handle)46 static void timer_cb(uv_timer_t* handle) {
47 uv_idle_stop(&idle_handle);
48 uv_timer_stop(&timer_handle);
49 }
50
51
BENCHMARK_IMPL(loop_count)52 BENCHMARK_IMPL(loop_count) {
53 uv_loop_t* loop = uv_default_loop();
54 uint64_t ns;
55
56 uv_idle_init(loop, &idle_handle);
57 uv_idle_start(&idle_handle, idle_cb);
58
59 ns = uv_hrtime();
60 uv_run(loop, UV_RUN_DEFAULT);
61 ns = uv_hrtime() - ns;
62
63 ASSERT(ticks == NUM_TICKS);
64
65 fprintf(stderr, "loop_count: %d ticks in %.2fs (%.0f/s)\n",
66 NUM_TICKS,
67 ns / 1e9,
68 NUM_TICKS / (ns / 1e9));
69 fflush(stderr);
70
71 MAKE_VALGRIND_HAPPY();
72 return 0;
73 }
74
75
BENCHMARK_IMPL(loop_count_timed)76 BENCHMARK_IMPL(loop_count_timed) {
77 uv_loop_t* loop = uv_default_loop();
78
79 uv_idle_init(loop, &idle_handle);
80 uv_idle_start(&idle_handle, idle2_cb);
81
82 uv_timer_init(loop, &timer_handle);
83 uv_timer_start(&timer_handle, timer_cb, 5000, 0);
84
85 uv_run(loop, UV_RUN_DEFAULT);
86
87 fprintf(stderr, "loop_count: %lu ticks (%.0f ticks/s)\n", ticks, ticks / 5.0);
88 fflush(stderr);
89
90 MAKE_VALGRIND_HAPPY();
91 return 0;
92 }
93