Mercurial
comparison third_party/libuv/test/benchmark-fs-stat.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_SYNC_REQS (10 * 1e5) | |
| 29 #define NUM_ASYNC_REQS (1 * (int) 1e5) | |
| 30 #define MAX_CONCURRENT_REQS 32 | |
| 31 | |
| 32 #define sync_stat(req, path) \ | |
| 33 do { \ | |
| 34 uv_fs_stat(NULL, (req), (path), NULL); \ | |
| 35 uv_fs_req_cleanup((req)); \ | |
| 36 } \ | |
| 37 while (0) | |
| 38 | |
| 39 struct async_req { | |
| 40 const char* path; | |
| 41 uv_fs_t fs_req; | |
| 42 int* count; | |
| 43 }; | |
| 44 | |
| 45 | |
| 46 static void warmup(const char* path) { | |
| 47 uv_fs_t reqs[MAX_CONCURRENT_REQS]; | |
| 48 unsigned int i; | |
| 49 | |
| 50 /* warm up the thread pool */ | |
| 51 for (i = 0; i < ARRAY_SIZE(reqs); i++) | |
| 52 uv_fs_stat(uv_default_loop(), reqs + i, path, uv_fs_req_cleanup); | |
| 53 | |
| 54 uv_run(uv_default_loop(), UV_RUN_DEFAULT); | |
| 55 | |
| 56 /* warm up the OS dirent cache */ | |
| 57 for (i = 0; i < 16; i++) | |
| 58 sync_stat(reqs + 0, path); | |
| 59 } | |
| 60 | |
| 61 | |
| 62 static void sync_bench(const char* path) { | |
| 63 char fmtbuf[2][32]; | |
| 64 uint64_t before; | |
| 65 uint64_t after; | |
| 66 uv_fs_t req; | |
| 67 int i; | |
| 68 | |
| 69 /* do the sync benchmark */ | |
| 70 before = uv_hrtime(); | |
| 71 | |
| 72 for (i = 0; i < NUM_SYNC_REQS; i++) | |
| 73 sync_stat(&req, path); | |
| 74 | |
| 75 after = uv_hrtime(); | |
| 76 | |
| 77 printf("%s stats (sync): %.2fs (%s/s)\n", | |
| 78 fmt(&fmtbuf[0], 1.0 * NUM_SYNC_REQS), | |
| 79 (after - before) / 1e9, | |
| 80 fmt(&fmtbuf[1], (1.0 * NUM_SYNC_REQS) / ((after - before) / 1e9))); | |
| 81 fflush(stdout); | |
| 82 } | |
| 83 | |
| 84 | |
| 85 static void stat_cb(uv_fs_t* fs_req) { | |
| 86 struct async_req* req = container_of(fs_req, struct async_req, fs_req); | |
| 87 uv_fs_req_cleanup(&req->fs_req); | |
| 88 if (*req->count == 0) return; | |
| 89 uv_fs_stat(uv_default_loop(), &req->fs_req, req->path, stat_cb); | |
| 90 (*req->count)--; | |
| 91 } | |
| 92 | |
| 93 | |
| 94 static void async_bench(const char* path) { | |
| 95 struct async_req reqs[MAX_CONCURRENT_REQS]; | |
| 96 struct async_req* req; | |
| 97 char fmtbuf[2][32]; | |
| 98 uint64_t before; | |
| 99 uint64_t after; | |
| 100 int count; | |
| 101 int i; | |
| 102 | |
| 103 for (i = 1; i <= MAX_CONCURRENT_REQS; i++) { | |
| 104 count = NUM_ASYNC_REQS; | |
| 105 | |
| 106 for (req = reqs; req < reqs + i; req++) { | |
| 107 req->path = path; | |
| 108 req->count = &count; | |
| 109 uv_fs_stat(uv_default_loop(), &req->fs_req, req->path, stat_cb); | |
| 110 } | |
| 111 | |
| 112 before = uv_hrtime(); | |
| 113 uv_run(uv_default_loop(), UV_RUN_DEFAULT); | |
| 114 after = uv_hrtime(); | |
| 115 | |
| 116 printf("%s stats (%d concurrent): %.2fs (%s/s)\n", | |
| 117 fmt(&fmtbuf[0], 1.0 * NUM_ASYNC_REQS), | |
| 118 i, | |
| 119 (after - before) / 1e9, | |
| 120 fmt(&fmtbuf[1], (1.0 * NUM_ASYNC_REQS) / ((after - before) / 1e9))); | |
| 121 fflush(stdout); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 | |
| 126 /* This benchmark aims to measure the overhead of doing I/O syscalls from | |
| 127 * the thread pool. The stat() syscall was chosen because its results are | |
| 128 * easy for the operating system to cache, taking the actual I/O overhead | |
| 129 * out of the equation. | |
| 130 */ | |
| 131 BENCHMARK_IMPL(fs_stat) { | |
| 132 const char path[] = "."; | |
| 133 warmup(path); | |
| 134 sync_bench(path); | |
| 135 async_bench(path); | |
| 136 MAKE_VALGRIND_HAPPY(uv_default_loop()); | |
| 137 return 0; | |
| 138 } |