Blame test/benchmark-fs-stat.c

Packit b5b901
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
Packit b5b901
 *
Packit b5b901
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b5b901
 * of this software and associated documentation files (the "Software"), to
Packit b5b901
 * deal in the Software without restriction, including without limitation the
Packit b5b901
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b5b901
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b5b901
 * furnished to do so, subject to the following conditions:
Packit b5b901
 *
Packit b5b901
 * The above copyright notice and this permission notice shall be included in
Packit b5b901
 * all copies or substantial portions of the Software.
Packit b5b901
 *
Packit b5b901
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b5b901
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b5b901
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit b5b901
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit b5b901
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit b5b901
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit b5b901
 * IN THE SOFTWARE.
Packit b5b901
 */
Packit b5b901
Packit b5b901
#include "task.h"
Packit b5b901
#include "uv.h"
Packit b5b901
Packit b5b901
#include <stdio.h>
Packit b5b901
#include <stdlib.h>
Packit b5b901
Packit b5b901
#define NUM_SYNC_REQS         (10 * 1e5)
Packit b5b901
#define NUM_ASYNC_REQS        (1 * (int) 1e5)
Packit b5b901
#define MAX_CONCURRENT_REQS   32
Packit b5b901
Packit b5b901
#define sync_stat(req, path)                                                  \
Packit b5b901
  do {                                                                        \
Packit b5b901
    uv_fs_stat(NULL, (req), (path), NULL);                                    \
Packit b5b901
    uv_fs_req_cleanup((req));                                                 \
Packit b5b901
  }                                                                           \
Packit b5b901
  while (0)
Packit b5b901
Packit b5b901
struct async_req {
Packit b5b901
  const char* path;
Packit b5b901
  uv_fs_t fs_req;
Packit b5b901
  int* count;
Packit b5b901
};
Packit b5b901
Packit b5b901
Packit b5b901
static void warmup(const char* path) {
Packit b5b901
  uv_fs_t reqs[MAX_CONCURRENT_REQS];
Packit b5b901
  unsigned int i;
Packit b5b901
Packit b5b901
  /* warm up the thread pool */
Packit b5b901
  for (i = 0; i < ARRAY_SIZE(reqs); i++)
Packit b5b901
    uv_fs_stat(uv_default_loop(), reqs + i, path, uv_fs_req_cleanup);
Packit b5b901
Packit b5b901
  uv_run(uv_default_loop(), UV_RUN_DEFAULT);
Packit b5b901
Packit b5b901
  /* warm up the OS dirent cache */
Packit b5b901
  for (i = 0; i < 16; i++)
Packit b5b901
    sync_stat(reqs + 0, path);
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void sync_bench(const char* path) {
Packit b5b901
  uint64_t before;
Packit b5b901
  uint64_t after;
Packit b5b901
  uv_fs_t req;
Packit b5b901
  int i;
Packit b5b901
Packit b5b901
  /* do the sync benchmark */
Packit b5b901
  before = uv_hrtime();
Packit b5b901
Packit b5b901
  for (i = 0; i < NUM_SYNC_REQS; i++)
Packit b5b901
    sync_stat(&req, path);
Packit b5b901
Packit b5b901
  after = uv_hrtime();
Packit b5b901
Packit b5b901
  printf("%s stats (sync): %.2fs (%s/s)\n",
Packit b5b901
         fmt(1.0 * NUM_SYNC_REQS),
Packit b5b901
         (after - before) / 1e9,
Packit b5b901
         fmt((1.0 * NUM_SYNC_REQS) / ((after - before) / 1e9)));
Packit b5b901
  fflush(stdout);
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void stat_cb(uv_fs_t* fs_req) {
Packit b5b901
  struct async_req* req = container_of(fs_req, struct async_req, fs_req);
Packit b5b901
  uv_fs_req_cleanup(&req->fs_req);
Packit b5b901
  if (*req->count == 0) return;
Packit b5b901
  uv_fs_stat(uv_default_loop(), &req->fs_req, req->path, stat_cb);
Packit b5b901
  (*req->count)--;
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void async_bench(const char* path) {
Packit b5b901
  struct async_req reqs[MAX_CONCURRENT_REQS];
Packit b5b901
  struct async_req* req;
Packit b5b901
  uint64_t before;
Packit b5b901
  uint64_t after;
Packit b5b901
  int count;
Packit b5b901
  int i;
Packit b5b901
Packit b5b901
  for (i = 1; i <= MAX_CONCURRENT_REQS; i++) {
Packit b5b901
    count = NUM_ASYNC_REQS;
Packit b5b901
Packit b5b901
    for (req = reqs; req < reqs + i; req++) {
Packit b5b901
      req->path = path;
Packit b5b901
      req->count = &count;
Packit b5b901
      uv_fs_stat(uv_default_loop(), &req->fs_req, req->path, stat_cb);
Packit b5b901
    }
Packit b5b901
Packit b5b901
    before = uv_hrtime();
Packit b5b901
    uv_run(uv_default_loop(), UV_RUN_DEFAULT);
Packit b5b901
    after = uv_hrtime();
Packit b5b901
Packit b5b901
    printf("%s stats (%d concurrent): %.2fs (%s/s)\n",
Packit b5b901
           fmt(1.0 * NUM_ASYNC_REQS),
Packit b5b901
           i,
Packit b5b901
           (after - before) / 1e9,
Packit b5b901
           fmt((1.0 * NUM_ASYNC_REQS) / ((after - before) / 1e9)));
Packit b5b901
    fflush(stdout);
Packit b5b901
  }
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
/* This benchmark aims to measure the overhead of doing I/O syscalls from
Packit b5b901
 * the thread pool. The stat() syscall was chosen because its results are
Packit b5b901
 * easy for the operating system to cache, taking the actual I/O overhead
Packit b5b901
 * out of the equation.
Packit b5b901
 */
Packit b5b901
BENCHMARK_IMPL(fs_stat) {
Packit b5b901
  const char path[] = ".";
Packit b5b901
  warmup(path);
Packit b5b901
  sync_bench(path);
Packit b5b901
  async_bench(path);
Packit b5b901
  MAKE_VALGRIND_HAPPY();
Packit b5b901
  return 0;
Packit b5b901
}