1*0e552da7Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2*0e552da7Schristos *
3*0e552da7Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
4*0e552da7Schristos * of this software and associated documentation files (the "Software"), to
5*0e552da7Schristos * deal in the Software without restriction, including without limitation the
6*0e552da7Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7*0e552da7Schristos * sell copies of the Software, and to permit persons to whom the Software is
8*0e552da7Schristos * furnished to do so, subject to the following conditions:
9*0e552da7Schristos *
10*0e552da7Schristos * The above copyright notice and this permission notice shall be included in
11*0e552da7Schristos * all copies or substantial portions of the Software.
12*0e552da7Schristos *
13*0e552da7Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*0e552da7Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*0e552da7Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*0e552da7Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17*0e552da7Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18*0e552da7Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19*0e552da7Schristos * IN THE SOFTWARE.
20*0e552da7Schristos */
21*0e552da7Schristos
22*0e552da7Schristos #include "task.h"
23*0e552da7Schristos #include "uv.h"
24*0e552da7Schristos
25*0e552da7Schristos #include <stdio.h>
26*0e552da7Schristos #include <stdlib.h>
27*0e552da7Schristos
28*0e552da7Schristos #define NUM_SYNC_REQS (10 * 1e5)
29*0e552da7Schristos #define NUM_ASYNC_REQS (1 * (int) 1e5)
30*0e552da7Schristos #define MAX_CONCURRENT_REQS 32
31*0e552da7Schristos
32*0e552da7Schristos #define sync_stat(req, path) \
33*0e552da7Schristos do { \
34*0e552da7Schristos uv_fs_stat(NULL, (req), (path), NULL); \
35*0e552da7Schristos uv_fs_req_cleanup((req)); \
36*0e552da7Schristos } \
37*0e552da7Schristos while (0)
38*0e552da7Schristos
39*0e552da7Schristos struct async_req {
40*0e552da7Schristos const char* path;
41*0e552da7Schristos uv_fs_t fs_req;
42*0e552da7Schristos int* count;
43*0e552da7Schristos };
44*0e552da7Schristos
45*0e552da7Schristos
warmup(const char * path)46*0e552da7Schristos static void warmup(const char* path) {
47*0e552da7Schristos uv_fs_t reqs[MAX_CONCURRENT_REQS];
48*0e552da7Schristos unsigned int i;
49*0e552da7Schristos
50*0e552da7Schristos /* warm up the thread pool */
51*0e552da7Schristos for (i = 0; i < ARRAY_SIZE(reqs); i++)
52*0e552da7Schristos uv_fs_stat(uv_default_loop(), reqs + i, path, uv_fs_req_cleanup);
53*0e552da7Schristos
54*0e552da7Schristos uv_run(uv_default_loop(), UV_RUN_DEFAULT);
55*0e552da7Schristos
56*0e552da7Schristos /* warm up the OS dirent cache */
57*0e552da7Schristos for (i = 0; i < 16; i++)
58*0e552da7Schristos sync_stat(reqs + 0, path);
59*0e552da7Schristos }
60*0e552da7Schristos
61*0e552da7Schristos
sync_bench(const char * path)62*0e552da7Schristos static void sync_bench(const char* path) {
63*0e552da7Schristos uint64_t before;
64*0e552da7Schristos uint64_t after;
65*0e552da7Schristos uv_fs_t req;
66*0e552da7Schristos int i;
67*0e552da7Schristos
68*0e552da7Schristos /* do the sync benchmark */
69*0e552da7Schristos before = uv_hrtime();
70*0e552da7Schristos
71*0e552da7Schristos for (i = 0; i < NUM_SYNC_REQS; i++)
72*0e552da7Schristos sync_stat(&req, path);
73*0e552da7Schristos
74*0e552da7Schristos after = uv_hrtime();
75*0e552da7Schristos
76*0e552da7Schristos printf("%s stats (sync): %.2fs (%s/s)\n",
77*0e552da7Schristos fmt(1.0 * NUM_SYNC_REQS),
78*0e552da7Schristos (after - before) / 1e9,
79*0e552da7Schristos fmt((1.0 * NUM_SYNC_REQS) / ((after - before) / 1e9)));
80*0e552da7Schristos fflush(stdout);
81*0e552da7Schristos }
82*0e552da7Schristos
83*0e552da7Schristos
stat_cb(uv_fs_t * fs_req)84*0e552da7Schristos static void stat_cb(uv_fs_t* fs_req) {
85*0e552da7Schristos struct async_req* req = container_of(fs_req, struct async_req, fs_req);
86*0e552da7Schristos uv_fs_req_cleanup(&req->fs_req);
87*0e552da7Schristos if (*req->count == 0) return;
88*0e552da7Schristos uv_fs_stat(uv_default_loop(), &req->fs_req, req->path, stat_cb);
89*0e552da7Schristos (*req->count)--;
90*0e552da7Schristos }
91*0e552da7Schristos
92*0e552da7Schristos
async_bench(const char * path)93*0e552da7Schristos static void async_bench(const char* path) {
94*0e552da7Schristos struct async_req reqs[MAX_CONCURRENT_REQS];
95*0e552da7Schristos struct async_req* req;
96*0e552da7Schristos uint64_t before;
97*0e552da7Schristos uint64_t after;
98*0e552da7Schristos int count;
99*0e552da7Schristos int i;
100*0e552da7Schristos
101*0e552da7Schristos for (i = 1; i <= MAX_CONCURRENT_REQS; i++) {
102*0e552da7Schristos count = NUM_ASYNC_REQS;
103*0e552da7Schristos
104*0e552da7Schristos for (req = reqs; req < reqs + i; req++) {
105*0e552da7Schristos req->path = path;
106*0e552da7Schristos req->count = &count;
107*0e552da7Schristos uv_fs_stat(uv_default_loop(), &req->fs_req, req->path, stat_cb);
108*0e552da7Schristos }
109*0e552da7Schristos
110*0e552da7Schristos before = uv_hrtime();
111*0e552da7Schristos uv_run(uv_default_loop(), UV_RUN_DEFAULT);
112*0e552da7Schristos after = uv_hrtime();
113*0e552da7Schristos
114*0e552da7Schristos printf("%s stats (%d concurrent): %.2fs (%s/s)\n",
115*0e552da7Schristos fmt(1.0 * NUM_ASYNC_REQS),
116*0e552da7Schristos i,
117*0e552da7Schristos (after - before) / 1e9,
118*0e552da7Schristos fmt((1.0 * NUM_ASYNC_REQS) / ((after - before) / 1e9)));
119*0e552da7Schristos fflush(stdout);
120*0e552da7Schristos }
121*0e552da7Schristos }
122*0e552da7Schristos
123*0e552da7Schristos
124*0e552da7Schristos /* This benchmark aims to measure the overhead of doing I/O syscalls from
125*0e552da7Schristos * the thread pool. The stat() syscall was chosen because its results are
126*0e552da7Schristos * easy for the operating system to cache, taking the actual I/O overhead
127*0e552da7Schristos * out of the equation.
128*0e552da7Schristos */
BENCHMARK_IMPL(fs_stat)129*0e552da7Schristos BENCHMARK_IMPL(fs_stat) {
130*0e552da7Schristos const char path[] = ".";
131*0e552da7Schristos warmup(path);
132*0e552da7Schristos sync_bench(path);
133*0e552da7Schristos async_bench(path);
134*0e552da7Schristos MAKE_VALGRIND_HAPPY();
135*0e552da7Schristos return 0;
136*0e552da7Schristos }
137