Mercurial
comparison third_party/libuv/test/benchmark-loop-count.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 #include <stdio.h> | |
| 26 #include <stdlib.h> | |
| 27 | |
| 28 #define NUM_TICKS (2 * 1000 * 1000) | |
| 29 #define NUM_TICKS2 (2 * 1000 * 1000 * 100) | |
| 30 | |
| 31 static unsigned long ticks; | |
| 32 static uv_idle_t idle_handle; | |
| 33 static uv_timer_t timer_handle; | |
| 34 | |
| 35 | |
| 36 static void idle_cb(uv_idle_t* handle) { | |
| 37 if (++ticks == NUM_TICKS) | |
| 38 uv_idle_stop(handle); | |
| 39 } | |
| 40 | |
| 41 static void idle_alive_cb(uv_idle_t* handle) { | |
| 42 int ticks = 0; | |
| 43 | |
| 44 while (++ticks < NUM_TICKS2) { | |
| 45 int r = uv_loop_alive(handle->loop); | |
| 46 if (r == 0) | |
| 47 abort(); | |
| 48 } | |
| 49 | |
| 50 *(int*)handle->data = ticks; | |
| 51 uv_idle_stop(handle); | |
| 52 } | |
| 53 | |
| 54 | |
| 55 static void idle2_cb(uv_idle_t* handle) { | |
| 56 ticks++; | |
| 57 } | |
| 58 | |
| 59 | |
| 60 static void timer_cb(uv_timer_t* handle) { | |
| 61 uv_idle_stop(&idle_handle); | |
| 62 uv_timer_stop(&timer_handle); | |
| 63 } | |
| 64 | |
| 65 | |
| 66 BENCHMARK_IMPL(loop_count) { | |
| 67 uv_loop_t* loop = uv_default_loop(); | |
| 68 uint64_t ns; | |
| 69 | |
| 70 uv_idle_init(loop, &idle_handle); | |
| 71 uv_idle_start(&idle_handle, idle_cb); | |
| 72 | |
| 73 ns = uv_hrtime(); | |
| 74 uv_run(loop, UV_RUN_DEFAULT); | |
| 75 ns = uv_hrtime() - ns; | |
| 76 | |
| 77 ASSERT_UINT64_EQ(ticks, NUM_TICKS); | |
| 78 | |
| 79 fprintf(stderr, "loop_count: %d ticks in %.2fs (%.0f/s)\n", | |
| 80 NUM_TICKS, | |
| 81 ns / 1e9, | |
| 82 NUM_TICKS / (ns / 1e9)); | |
| 83 fflush(stderr); | |
| 84 | |
| 85 MAKE_VALGRIND_HAPPY(loop); | |
| 86 return 0; | |
| 87 } | |
| 88 | |
| 89 | |
| 90 BENCHMARK_IMPL(loop_count_timed) { | |
| 91 uv_loop_t* loop = uv_default_loop(); | |
| 92 | |
| 93 uv_idle_init(loop, &idle_handle); | |
| 94 uv_idle_start(&idle_handle, idle2_cb); | |
| 95 | |
| 96 uv_timer_init(loop, &timer_handle); | |
| 97 uv_timer_start(&timer_handle, timer_cb, 5000, 0); | |
| 98 | |
| 99 uv_run(loop, UV_RUN_DEFAULT); | |
| 100 | |
| 101 fprintf(stderr, "loop_count: %lu ticks (%.0f ticks/s)\n", ticks, ticks / 5.0); | |
| 102 fflush(stderr); | |
| 103 | |
| 104 MAKE_VALGRIND_HAPPY(loop); | |
| 105 return 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. */ | |
| 111 BENCHMARK_IMPL(loop_alive) { | |
| 112 uv_loop_t* loop = uv_default_loop(); | |
| 113 int ticks = 0; | |
| 114 uint64_t ns; | |
| 115 | |
| 116 uv_idle_init(loop, &idle_handle); | |
| 117 idle_handle.data = &ticks; | |
| 118 uv_idle_start(&idle_handle, idle_alive_cb); | |
| 119 | |
| 120 ns = uv_hrtime(); | |
| 121 uv_run(loop, UV_RUN_DEFAULT); | |
| 122 ns = uv_hrtime() - ns; | |
| 123 | |
| 124 ASSERT_EQ(ticks, NUM_TICKS2); | |
| 125 | |
| 126 fprintf(stderr, "loop_alive: %d ticks in %.2fs (%.0f/s)\n", | |
| 127 NUM_TICKS2, | |
| 128 ns / 1e9, | |
| 129 NUM_TICKS2 / (ns / 1e9)); | |
| 130 fflush(stderr); | |
| 131 | |
| 132 MAKE_VALGRIND_HAPPY(loop); | |
| 133 return 0; | |
| 134 } |