1
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
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:
10
* The above copyright notice and this permission notice shall be included in
11
* all copies or substantial portions of the Software.
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
25
struct async_container {
26
unsigned async_events;
27
unsigned handles_seen;
28
uv_async_t async_handles[1024 * 1024];
31
static volatile int done;
32
static uv_thread_t thread_id;
33
static struct async_container* container;
36
static unsigned fastrand(void) {
37
static unsigned g = 0;
38
g = g * 214013 + 2531011;
43
static void thread_cb(void* arg) {
47
i = fastrand() % ARRAY_SIZE(container->async_handles);
48
uv_async_send(container->async_handles + i);
53
static void async_cb(uv_async_t* handle, int status) {
54
container->async_events++;
55
handle->data = handle;
59
static void timer_cb(uv_timer_t* handle, int status) {
63
ASSERT(0 == uv_thread_join(&thread_id));
65
for (i = 0; i < ARRAY_SIZE(container->async_handles); i++) {
66
uv_async_t* handle = container->async_handles + i;
68
if (handle->data != NULL)
69
container->handles_seen++;
71
uv_close((uv_handle_t*) handle, NULL);
74
uv_close((uv_handle_t*) handle, NULL);
78
BENCHMARK_IMPL(million_async) {
79
uv_timer_t timer_handle;
85
loop = uv_default_loop();
88
container = malloc(sizeof(*container));
89
ASSERT(container != NULL);
90
container->async_events = 0;
91
container->handles_seen = 0;
93
for (i = 0; i < ARRAY_SIZE(container->async_handles); i++) {
94
handle = container->async_handles + i;
95
ASSERT(0 == uv_async_init(loop, handle, async_cb));
99
ASSERT(0 == uv_timer_init(loop, &timer_handle));
100
ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, timeout, 0));
101
ASSERT(0 == uv_thread_create(&thread_id, thread_cb, NULL));
102
ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
103
printf("%s async events in %.1f seconds (%s/s, %s unique handles seen)\n",
104
fmt(container->async_events),
106
fmt(container->async_events / (timeout / 1000.)),
107
fmt(container->handles_seen));
110
MAKE_VALGRIND_HAPPY();