• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
warmup(const char * path)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 
sync_bench(const char * path)62 static void sync_bench(const char* path) {
63   uint64_t before;
64   uint64_t after;
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 
stat_cb(uv_fs_t * fs_req)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 
async_bench(const char * path)93 static void async_bench(const char* path) {
94   struct async_req reqs[MAX_CONCURRENT_REQS];
95   struct async_req* req;
96   uint64_t before;
97   uint64_t after;
98   int count;
99   int i;
100 
101   for (i = 1; i <= MAX_CONCURRENT_REQS; i++) {
102     count = NUM_ASYNC_REQS;
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();
111     uv_run(uv_default_loop(), UV_RUN_DEFAULT);
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  */
BENCHMARK_IMPL(fs_stat)129 BENCHMARK_IMPL(fs_stat) {
130   const char path[] = ".";
131   warmup(path);
132   sync_bench(path);
133   async_bench(path);
134   MAKE_VALGRIND_HAPPY();
135   return 0;
136 }
137