benchmark-fs-stat.c
Go to the documentation of this file.
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;
42  int* count;
43 };
44 
45 
46 static void warmup(const char* path) {
48  unsigned int i;
49 
50  /* warm up the thread pool */
51  for (i = 0; i < ARRAY_SIZE(reqs); i++)
53 
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) {
65  uv_fs_t req;
66  int i;
67 
68  /* do the sync benchmark */
69  before = uv_hrtime();
70 
71  for (i = 0; i < NUM_SYNC_REQS; i++)
72  sync_stat(&req, path);
73 
74  after = uv_hrtime();
75 
76  printf("%s stats (sync): %.2fs (%s/s)\n",
77  fmt(1.0 * NUM_SYNC_REQS),
78  (after - before) / 1e9,
79  fmt((1.0 * NUM_SYNC_REQS) / ((after - before) / 1e9)));
80  fflush(stdout);
81 }
82 
83 
84 static void stat_cb(uv_fs_t* fs_req) {
85  struct async_req* req = container_of(fs_req, struct async_req, fs_req);
86  uv_fs_req_cleanup(&req->fs_req);
87  if (*req->count == 0) return;
88  uv_fs_stat(uv_default_loop(), &req->fs_req, req->path, stat_cb);
89  (*req->count)--;
90 }
91 
92 
93 static void async_bench(const char* path) {
95  struct async_req* req;
98  int count;
99  int i;
100 
101  for (i = 1; i <= MAX_CONCURRENT_REQS; i++) {
103 
104  for (req = reqs; req < reqs + i; req++) {
105  req->path = path;
106  req->count = &count;
107  uv_fs_stat(uv_default_loop(), &req->fs_req, req->path, stat_cb);
108  }
109 
110  before = uv_hrtime();
112  after = uv_hrtime();
113 
114  printf("%s stats (%d concurrent): %.2fs (%s/s)\n",
115  fmt(1.0 * NUM_ASYNC_REQS),
116  i,
117  (after - before) / 1e9,
118  fmt((1.0 * NUM_ASYNC_REQS) / ((after - before) / 1e9)));
119  fflush(stdout);
120  }
121 }
122 
123 
124 /* This benchmark aims to measure the overhead of doing I/O syscalls from
125  * the thread pool. The stat() syscall was chosen because its results are
126  * easy for the operating system to cache, taking the actual I/O overhead
127  * out of the equation.
128  */
129 BENCHMARK_IMPL(fs_stat) {
130  const char path[] = ".";
131  warmup(path);
132  sync_bench(path);
133  async_bench(path);
135  return 0;
136 }
reqs
static uv_udp_send_t reqs[ARRAY_SIZE(sockets)]
Definition: test-watcher-cross-stop.c:36
ARRAY_SIZE
#define ARRAY_SIZE(array)
Definition: bloaty.cc:101
task.h
demumble_test.stdout
stdout
Definition: demumble_test.py:38
BENCHMARK_IMPL
BENCHMARK_IMPL(fs_stat)
Definition: benchmark-fs-stat.c:129
uv_fs_stat
UV_EXTERN int uv_fs_stat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)
Definition: unix/fs.c:1954
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
stat_cb
static void stat_cb(uv_fs_t *fs_req)
Definition: benchmark-fs-stat.c:84
check_documentation.path
path
Definition: check_documentation.py:57
sync_bench
static void sync_bench(const char *path)
Definition: benchmark-fs-stat.c:62
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
container_of
#define container_of(ptr, type, member)
Definition: uv-common.h:57
async_req::path
const char * path
Definition: benchmark-fs-stat.c:40
uv_fs_s
Definition: uv.h:1294
warmup
static void warmup(const char *path)
Definition: benchmark-fs-stat.c:46
before
IntBeforeRegisterTypedTestSuiteP before
Definition: googletest/googletest/test/gtest-typed-test_test.cc:376
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
MAX_CONCURRENT_REQS
#define MAX_CONCURRENT_REQS
Definition: benchmark-fs-stat.c:30
NUM_ASYNC_REQS
#define NUM_ASYNC_REQS
Definition: benchmark-fs-stat.c:29
fs_req
Definition: test-thread.c:41
req
static uv_connect_t req
Definition: test-connection-fail.c:30
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
async_bench
static void async_bench(const char *path)
Definition: benchmark-fs-stat.c:93
NUM_SYNC_REQS
#define NUM_SYNC_REQS
Definition: benchmark-fs-stat.c:28
async_req
Definition: benchmark-fs-stat.c:39
sync_stat
#define sync_stat(req, path)
Definition: benchmark-fs-stat.c:32
after
IntAfterTypedTestSuiteP after
Definition: googletest/googletest/test/gtest-typed-test_test.cc:375
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
testing::internal::fmt
GTEST_API_ const char * fmt
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1808
async_req::count
int * count
Definition: benchmark-fs-stat.c:42
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
async_req::fs_req
uv_fs_t fs_req
Definition: benchmark-fs-stat.c:41
uv_hrtime
UV_EXTERN uint64_t uv_hrtime(void)
Definition: unix/core.c:107
uv_fs_req_cleanup
UV_EXTERN void uv_fs_req_cleanup(uv_fs_t *req)
Definition: unix/fs.c:2024
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:36