libuv-svace-build
138 строк · 4.1 Кб
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 3231
32#define sync_stat(req, path) \33do { \34uv_fs_stat(NULL, (req), (path), NULL); \35uv_fs_req_cleanup((req)); \36} \37while (0)38
39struct async_req {40const char* path;41uv_fs_t fs_req;42int* count;43};44
45
46static void warmup(const char* path) {47uv_fs_t reqs[MAX_CONCURRENT_REQS];48unsigned int i;49
50/* warm up the thread pool */51for (i = 0; i < ARRAY_SIZE(reqs); i++)52uv_fs_stat(uv_default_loop(), reqs + i, path, uv_fs_req_cleanup);53
54uv_run(uv_default_loop(), UV_RUN_DEFAULT);55
56/* warm up the OS dirent cache */57for (i = 0; i < 16; i++)58sync_stat(reqs + 0, path);59}
60
61
62static void sync_bench(const char* path) {63char fmtbuf[2][32];64uint64_t before;65uint64_t after;66uv_fs_t req;67int i;68
69/* do the sync benchmark */70before = uv_hrtime();71
72for (i = 0; i < NUM_SYNC_REQS; i++)73sync_stat(&req, path);74
75after = uv_hrtime();76
77printf("%s stats (sync): %.2fs (%s/s)\n",78fmt(&fmtbuf[0], 1.0 * NUM_SYNC_REQS),79(after - before) / 1e9,80fmt(&fmtbuf[1], (1.0 * NUM_SYNC_REQS) / ((after - before) / 1e9)));81fflush(stdout);82}
83
84
85static void stat_cb(uv_fs_t* fs_req) {86struct async_req* req = container_of(fs_req, struct async_req, fs_req);87uv_fs_req_cleanup(&req->fs_req);88if (*req->count == 0) return;89uv_fs_stat(uv_default_loop(), &req->fs_req, req->path, stat_cb);90(*req->count)--;91}
92
93
94static void async_bench(const char* path) {95struct async_req reqs[MAX_CONCURRENT_REQS];96struct async_req* req;97char fmtbuf[2][32];98uint64_t before;99uint64_t after;100int count;101int i;102
103for (i = 1; i <= MAX_CONCURRENT_REQS; i++) {104count = NUM_ASYNC_REQS;105
106for (req = reqs; req < reqs + i; req++) {107req->path = path;108req->count = &count;109uv_fs_stat(uv_default_loop(), &req->fs_req, req->path, stat_cb);110}111
112before = uv_hrtime();113uv_run(uv_default_loop(), UV_RUN_DEFAULT);114after = uv_hrtime();115
116printf("%s stats (%d concurrent): %.2fs (%s/s)\n",117fmt(&fmtbuf[0], 1.0 * NUM_ASYNC_REQS),118i,119(after - before) / 1e9,120fmt(&fmtbuf[1], (1.0 * NUM_ASYNC_REQS) / ((after - before) / 1e9)));121fflush(stdout);122}123}
124
125
126/* This benchmark aims to measure the overhead of doing I/O syscalls from
127* the thread pool. The stat() syscall was chosen because its results are
128* easy for the operating system to cache, taking the actual I/O overhead
129* out of the equation.
130*/
131BENCHMARK_IMPL(fs_stat) {132const char path[] = ".";133warmup(path);134sync_bench(path);135async_bench(path);136MAKE_VALGRIND_HAPPY(uv_default_loop());137return 0;138}
139