Mercurial
comparison third_party/libuv/test/benchmark-million-async.c @ 160:948de3f54cea
[ThirdParty] Added libuv
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 14 Jan 2026 19:39:52 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 159:05cf9467a1c3 | 160:948de3f54cea |
|---|---|
| 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 | |
| 36 static unsigned fastrand(void) { | |
| 37 static unsigned g = 0; | |
| 38 g = g * 214013 + 2531011; | |
| 39 return g; | |
| 40 } | |
| 41 | |
| 42 | |
| 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 | |
| 53 static void async_cb(uv_async_t* handle) { | |
| 54 container->async_events++; | |
| 55 handle->data = handle; | |
| 56 } | |
| 57 | |
| 58 | |
| 59 static void timer_cb(uv_timer_t* handle) { | |
| 60 unsigned i; | |
| 61 | |
| 62 done = 1; | |
| 63 ASSERT_OK(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 | |
| 78 BENCHMARK_IMPL(million_async) { | |
| 79 char fmtbuf[3][32]; | |
| 80 uv_timer_t timer_handle; | |
| 81 uv_async_t* handle; | |
| 82 uv_loop_t* loop; | |
| 83 int timeout; | |
| 84 unsigned i; | |
| 85 | |
| 86 loop = uv_default_loop(); | |
| 87 timeout = 5000; | |
| 88 | |
| 89 container = malloc(sizeof(*container)); | |
| 90 ASSERT_NOT_NULL(container); | |
| 91 container->async_events = 0; | |
| 92 container->handles_seen = 0; | |
| 93 | |
| 94 for (i = 0; i < ARRAY_SIZE(container->async_handles); i++) { | |
| 95 handle = container->async_handles + i; | |
| 96 ASSERT_OK(uv_async_init(loop, handle, async_cb)); | |
| 97 handle->data = NULL; | |
| 98 } | |
| 99 | |
| 100 ASSERT_OK(uv_timer_init(loop, &timer_handle)); | |
| 101 ASSERT_OK(uv_timer_start(&timer_handle, timer_cb, timeout, 0)); | |
| 102 ASSERT_OK(uv_thread_create(&thread_id, thread_cb, NULL)); | |
| 103 ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT)); | |
| 104 printf("%s async events in %.1f seconds (%s/s, %s unique handles seen)\n", | |
| 105 fmt(&fmtbuf[0], container->async_events), | |
| 106 timeout / 1000., | |
| 107 fmt(&fmtbuf[1], container->async_events / (timeout / 1000.)), | |
| 108 fmt(&fmtbuf[2], container->handles_seen)); | |
| 109 free(container); | |
| 110 | |
| 111 MAKE_VALGRIND_HAPPY(loop); | |
| 112 return 0; | |
| 113 } |