libuv-svace-build
113 строк · 3.2 Кб
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
25struct async_container {26unsigned async_events;27unsigned handles_seen;28uv_async_t async_handles[1024 * 1024];29};30
31static volatile int done;32static uv_thread_t thread_id;33static struct async_container* container;34
35
36static unsigned fastrand(void) {37static unsigned g = 0;38g = g * 214013 + 2531011;39return g;40}
41
42
43static void thread_cb(void* arg) {44unsigned i;45
46while (done == 0) {47i = fastrand() % ARRAY_SIZE(container->async_handles);48uv_async_send(container->async_handles + i);49}50}
51
52
53static void async_cb(uv_async_t* handle) {54container->async_events++;55handle->data = handle;56}
57
58
59static void timer_cb(uv_timer_t* handle) {60unsigned i;61
62done = 1;63ASSERT_OK(uv_thread_join(&thread_id));64
65for (i = 0; i < ARRAY_SIZE(container->async_handles); i++) {66uv_async_t* handle = container->async_handles + i;67
68if (handle->data != NULL)69container->handles_seen++;70
71uv_close((uv_handle_t*) handle, NULL);72}73
74uv_close((uv_handle_t*) handle, NULL);75}
76
77
78BENCHMARK_IMPL(million_async) {79char fmtbuf[3][32];80uv_timer_t timer_handle;81uv_async_t* handle;82uv_loop_t* loop;83int timeout;84unsigned i;85
86loop = uv_default_loop();87timeout = 5000;88
89container = malloc(sizeof(*container));90ASSERT_NOT_NULL(container);91container->async_events = 0;92container->handles_seen = 0;93
94for (i = 0; i < ARRAY_SIZE(container->async_handles); i++) {95handle = container->async_handles + i;96ASSERT_OK(uv_async_init(loop, handle, async_cb));97handle->data = NULL;98}99
100ASSERT_OK(uv_timer_init(loop, &timer_handle));101ASSERT_OK(uv_timer_start(&timer_handle, timer_cb, timeout, 0));102ASSERT_OK(uv_thread_create(&thread_id, thread_cb, NULL));103ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT));104printf("%s async events in %.1f seconds (%s/s, %s unique handles seen)\n",105fmt(&fmtbuf[0], container->async_events),106timeout / 1000.,107fmt(&fmtbuf[1], container->async_events / (timeout / 1000.)),108fmt(&fmtbuf[2], container->handles_seen));109free(container);110
111MAKE_VALGRIND_HAPPY(loop);112return 0;113}
114