• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 struct async_container {
26   unsigned async_events;
27   unsigned handles_seen;
28   uv_async_t async_handles[1024 * 1024];
29 };
30 
31 static volatile int done;
32 static uv_thread_t thread_id;
33 static struct async_container* container;
34 
35 
fastrand(void)36 static unsigned fastrand(void) {
37   static unsigned g = 0;
38   g = g * 214013 + 2531011;
39   return g;
40 }
41 
42 
thread_cb(void * arg)43 static void thread_cb(void* arg) {
44   unsigned i;
45 
46   while (done == 0) {
47     i = fastrand() % ARRAY_SIZE(container->async_handles);
48     uv_async_send(container->async_handles + i);
49   }
50 }
51 
52 
async_cb(uv_async_t * handle)53 static void async_cb(uv_async_t* handle) {
54   container->async_events++;
55   handle->data = handle;
56 }
57 
58 
timer_cb(uv_timer_t * handle)59 static void timer_cb(uv_timer_t* handle) {
60   unsigned i;
61 
62   done = 1;
63   ASSERT(0 == uv_thread_join(&thread_id));
64 
65   for (i = 0; i < ARRAY_SIZE(container->async_handles); i++) {
66     uv_async_t* handle = container->async_handles + i;
67 
68     if (handle->data != NULL)
69       container->handles_seen++;
70 
71     uv_close((uv_handle_t*) handle, NULL);
72   }
73 
74   uv_close((uv_handle_t*) handle, NULL);
75 }
76 
77 
BENCHMARK_IMPL(million_async)78 BENCHMARK_IMPL(million_async) {
79   uv_timer_t timer_handle;
80   uv_async_t* handle;
81   uv_loop_t* loop;
82   int timeout;
83   unsigned i;
84 
85   loop = uv_default_loop();
86   timeout = 5000;
87 
88   container = malloc(sizeof(*container));
89   ASSERT_NOT_NULL(container);
90   container->async_events = 0;
91   container->handles_seen = 0;
92 
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));
96     handle->data = NULL;
97   }
98 
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),
105           timeout / 1000.,
106           fmt(container->async_events / (timeout / 1000.)),
107           fmt(container->handles_seen));
108   free(container);
109 
110   MAKE_VALGRIND_HAPPY();
111   return 0;
112 }
113