1
0

benchmark-fs-stat.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "task.h"
  22. #include "uv.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #define NUM_SYNC_REQS (10 * 1e5)
  26. #define NUM_ASYNC_REQS (1 * (int) 1e5)
  27. #define MAX_CONCURRENT_REQS 32
  28. #define sync_stat(req, path) \
  29. do { \
  30. uv_fs_stat(uv_default_loop(), (req), (path), NULL); \
  31. uv_fs_req_cleanup((req)); \
  32. } \
  33. while (0)
  34. struct async_req {
  35. const char* path;
  36. uv_fs_t fs_req;
  37. int* count;
  38. };
  39. static void warmup(const char* path) {
  40. uv_fs_t reqs[MAX_CONCURRENT_REQS];
  41. unsigned int i;
  42. /* warm up the thread pool */
  43. for (i = 0; i < ARRAY_SIZE(reqs); i++)
  44. uv_fs_stat(uv_default_loop(), reqs + i, path, uv_fs_req_cleanup);
  45. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  46. /* warm up the OS dirent cache */
  47. for (i = 0; i < 16; i++)
  48. sync_stat(reqs + 0, path);
  49. }
  50. static void sync_bench(const char* path) {
  51. uint64_t before;
  52. uint64_t after;
  53. uv_fs_t req;
  54. int i;
  55. /* do the sync benchmark */
  56. before = uv_hrtime();
  57. for (i = 0; i < NUM_SYNC_REQS; i++)
  58. sync_stat(&req, path);
  59. after = uv_hrtime();
  60. printf("%s stats (sync): %.2fs (%s/s)\n",
  61. fmt(1.0 * NUM_SYNC_REQS),
  62. (after - before) / 1e9,
  63. fmt((1.0 * NUM_SYNC_REQS) / ((after - before) / 1e9)));
  64. fflush(stdout);
  65. }
  66. static void stat_cb(uv_fs_t* fs_req) {
  67. struct async_req* req = container_of(fs_req, struct async_req, fs_req);
  68. uv_fs_req_cleanup(&req->fs_req);
  69. if (*req->count == 0) return;
  70. uv_fs_stat(uv_default_loop(), &req->fs_req, req->path, stat_cb);
  71. (*req->count)--;
  72. }
  73. static void async_bench(const char* path) {
  74. struct async_req reqs[MAX_CONCURRENT_REQS];
  75. struct async_req* req;
  76. uint64_t before;
  77. uint64_t after;
  78. int count;
  79. int i;
  80. for (i = 1; i <= MAX_CONCURRENT_REQS; i++) {
  81. count = NUM_ASYNC_REQS;
  82. for (req = reqs; req < reqs + i; req++) {
  83. req->path = path;
  84. req->count = &count;
  85. uv_fs_stat(uv_default_loop(), &req->fs_req, req->path, stat_cb);
  86. }
  87. before = uv_hrtime();
  88. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  89. after = uv_hrtime();
  90. printf("%s stats (%d concurrent): %.2fs (%s/s)\n",
  91. fmt(1.0 * NUM_ASYNC_REQS),
  92. i,
  93. (after - before) / 1e9,
  94. fmt((1.0 * NUM_ASYNC_REQS) / ((after - before) / 1e9)));
  95. fflush(stdout);
  96. }
  97. }
  98. /* This benchmark aims to measure the overhead of doing I/O syscalls from
  99. * the thread pool. The stat() syscall was chosen because its results are
  100. * easy for the operating system to cache, taking the actual I/O overhead
  101. * out of the equation.
  102. */
  103. BENCHMARK_IMPL(fs_stat) {
  104. const char path[] = ".";
  105. warmup(path);
  106. sync_bench(path);
  107. async_bench(path);
  108. MAKE_VALGRIND_HAPPY();
  109. return 0;
  110. }