Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 91 lines (66 sloc) 2.442 kb
cd2a9b4 @bnoordhuis bench: measure ticks per second of idle event loop
bnoordhuis authored
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
3f37ba8 @bnoordhuis bench: add new idle loop count benchmark
bnoordhuis authored
28 #define NUM_TICKS (2 * 1000 * 1000)
29
cd2a9b4 @bnoordhuis bench: measure ticks per second of idle event loop
bnoordhuis authored
30 static unsigned long ticks;
31 static uv_idle_t idle_handle;
32 static uv_timer_t timer_handle;
33
34
db2a907 @saghul unix, windows: removed unused status parameter
saghul authored
35 static void idle_cb(uv_idle_t* handle) {
3f37ba8 @bnoordhuis bench: add new idle loop count benchmark
bnoordhuis authored
36 if (++ticks == NUM_TICKS)
37 uv_idle_stop(handle);
38 }
39
40
db2a907 @saghul unix, windows: removed unused status parameter
saghul authored
41 static void idle2_cb(uv_idle_t* handle) {
cd2a9b4 @bnoordhuis bench: measure ticks per second of idle event loop
bnoordhuis authored
42 ticks++;
43 }
44
45
db2a907 @saghul unix, windows: removed unused status parameter
saghul authored
46 static void timer_cb(uv_timer_t* handle) {
cd2a9b4 @bnoordhuis bench: measure ticks per second of idle event loop
bnoordhuis authored
47 uv_idle_stop(&idle_handle);
48 uv_timer_stop(&timer_handle);
49 }
50
51
52 BENCHMARK_IMPL(loop_count) {
53 uv_loop_t* loop = uv_default_loop();
3f37ba8 @bnoordhuis bench: add new idle loop count benchmark
bnoordhuis authored
54 uint64_t ns;
cd2a9b4 @bnoordhuis bench: measure ticks per second of idle event loop
bnoordhuis authored
55
56 uv_idle_init(loop, &idle_handle);
57 uv_idle_start(&idle_handle, idle_cb);
58
3f37ba8 @bnoordhuis bench: add new idle loop count benchmark
bnoordhuis authored
59 ns = uv_hrtime();
4ba03dd @bnoordhuis unix, windows: rename uv_run2 to uv_run
bnoordhuis authored
60 uv_run(loop, UV_RUN_DEFAULT);
3f37ba8 @bnoordhuis bench: add new idle loop count benchmark
bnoordhuis authored
61 ns = uv_hrtime() - ns;
62
63 ASSERT(ticks == NUM_TICKS);
64
65 LOGF("loop_count: %d ticks in %.2fs (%.0f/s)\n",
66 NUM_TICKS,
67 ns / 1e9,
68 NUM_TICKS / (ns / 1e9));
69
47eb034 @piscisaureus test: move loop cleanup code to the individual tests
piscisaureus authored
70 MAKE_VALGRIND_HAPPY();
3f37ba8 @bnoordhuis bench: add new idle loop count benchmark
bnoordhuis authored
71 return 0;
72 }
73
74
75 BENCHMARK_IMPL(loop_count_timed) {
76 uv_loop_t* loop = uv_default_loop();
77
78 uv_idle_init(loop, &idle_handle);
79 uv_idle_start(&idle_handle, idle2_cb);
80
81 uv_timer_init(loop, &timer_handle);
82 uv_timer_start(&timer_handle, timer_cb, 5000, 0);
83
4ba03dd @bnoordhuis unix, windows: rename uv_run2 to uv_run
bnoordhuis authored
84 uv_run(loop, UV_RUN_DEFAULT);
cd2a9b4 @bnoordhuis bench: measure ticks per second of idle event loop
bnoordhuis authored
85
86 LOGF("loop_count: %lu ticks (%.0f ticks/s)\n", ticks, ticks / 5.0);
87
47eb034 @piscisaureus test: move loop cleanup code to the individual tests
piscisaureus authored
88 MAKE_VALGRIND_HAPPY();
cd2a9b4 @bnoordhuis bench: measure ticks per second of idle event loop
bnoordhuis authored
89 return 0;
90 }
Something went wrong with that request. Please try again.