libuv-svace-build
134 строки · 3.5 Кб
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#define NUM_TICKS2 (2 * 1000 * 1000 * 100)30
31static unsigned long ticks;32static uv_idle_t idle_handle;33static uv_timer_t timer_handle;34
35
36static void idle_cb(uv_idle_t* handle) {37if (++ticks == NUM_TICKS)38uv_idle_stop(handle);39}
40
41static void idle_alive_cb(uv_idle_t* handle) {42int ticks = 0;43
44while (++ticks < NUM_TICKS2) {45int r = uv_loop_alive(handle->loop);46if (r == 0)47abort();48}49
50*(int*)handle->data = ticks;51uv_idle_stop(handle);52}
53
54
55static void idle2_cb(uv_idle_t* handle) {56ticks++;57}
58
59
60static void timer_cb(uv_timer_t* handle) {61uv_idle_stop(&idle_handle);62uv_timer_stop(&timer_handle);63}
64
65
66BENCHMARK_IMPL(loop_count) {67uv_loop_t* loop = uv_default_loop();68uint64_t ns;69
70uv_idle_init(loop, &idle_handle);71uv_idle_start(&idle_handle, idle_cb);72
73ns = uv_hrtime();74uv_run(loop, UV_RUN_DEFAULT);75ns = uv_hrtime() - ns;76
77ASSERT_UINT64_EQ(ticks, NUM_TICKS);78
79fprintf(stderr, "loop_count: %d ticks in %.2fs (%.0f/s)\n",80NUM_TICKS,81ns / 1e9,82NUM_TICKS / (ns / 1e9));83fflush(stderr);84
85MAKE_VALGRIND_HAPPY(loop);86return 0;87}
88
89
90BENCHMARK_IMPL(loop_count_timed) {91uv_loop_t* loop = uv_default_loop();92
93uv_idle_init(loop, &idle_handle);94uv_idle_start(&idle_handle, idle2_cb);95
96uv_timer_init(loop, &timer_handle);97uv_timer_start(&timer_handle, timer_cb, 5000, 0);98
99uv_run(loop, UV_RUN_DEFAULT);100
101fprintf(stderr, "loop_count: %lu ticks (%.0f ticks/s)\n", ticks, ticks / 5.0);102fflush(stderr);103
104MAKE_VALGRIND_HAPPY(loop);105return 0;106}
107
108/* Measure the performance of running uv_loop_alive(). Adding this so we can get
109* some sort of metric for the impact of switching active_reqs.count to use
110* atomics. No other code sits in a hot path. */
111BENCHMARK_IMPL(loop_alive) {112uv_loop_t* loop = uv_default_loop();113int ticks = 0;114uint64_t ns;115
116uv_idle_init(loop, &idle_handle);117idle_handle.data = &ticks;118uv_idle_start(&idle_handle, idle_alive_cb);119
120ns = uv_hrtime();121uv_run(loop, UV_RUN_DEFAULT);122ns = uv_hrtime() - ns;123
124ASSERT_EQ(ticks, NUM_TICKS2);125
126fprintf(stderr, "loop_alive: %d ticks in %.2fs (%.0f/s)\n",127NUM_TICKS2,128ns / 1e9,129NUM_TICKS2 / (ns / 1e9));130fflush(stderr);131
132MAKE_VALGRIND_HAPPY(loop);133return 0;134}
135