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