benchmark-spawn.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /* This benchmark spawns itself 1000 times. */
  22. #include "task.h"
  23. #include "uv.h"
  24. static uv_loop_t* loop;
  25. static int N = 1000;
  26. static int done;
  27. static uv_process_t process;
  28. static uv_process_options_t options;
  29. static char exepath[1024];
  30. static size_t exepath_size = 1024;
  31. static char* args[3];
  32. static uv_pipe_t out;
  33. #define OUTPUT_SIZE 1024
  34. static char output[OUTPUT_SIZE];
  35. static int output_used;
  36. static int process_open;
  37. static int pipe_open;
  38. static void spawn(void);
  39. static void maybe_spawn(void) {
  40. if (process_open == 0 && pipe_open == 0) {
  41. done++;
  42. if (done < N) {
  43. spawn();
  44. }
  45. }
  46. }
  47. static void process_close_cb(uv_handle_t* handle) {
  48. ASSERT(process_open == 1);
  49. process_open = 0;
  50. maybe_spawn();
  51. }
  52. static void exit_cb(uv_process_t* process,
  53. int64_t exit_status,
  54. int term_signal) {
  55. ASSERT(exit_status == 42);
  56. ASSERT(term_signal == 0);
  57. uv_close((uv_handle_t*)process, process_close_cb);
  58. }
  59. static void on_alloc(uv_handle_t* handle,
  60. size_t suggested_size,
  61. uv_buf_t* buf) {
  62. buf->base = output + output_used;
  63. buf->len = OUTPUT_SIZE - output_used;
  64. }
  65. static void pipe_close_cb(uv_handle_t* pipe) {
  66. ASSERT(pipe_open == 1);
  67. pipe_open = 0;
  68. maybe_spawn();
  69. }
  70. static void on_read(uv_stream_t* pipe, ssize_t nread, const uv_buf_t* buf) {
  71. if (nread > 0) {
  72. ASSERT(pipe_open == 1);
  73. output_used += nread;
  74. } else if (nread < 0) {
  75. if (nread == UV_EOF) {
  76. uv_close((uv_handle_t*)pipe, pipe_close_cb);
  77. }
  78. }
  79. }
  80. static void spawn(void) {
  81. uv_stdio_container_t stdio[2];
  82. int r;
  83. ASSERT(process_open == 0);
  84. ASSERT(pipe_open == 0);
  85. args[0] = exepath;
  86. args[1] = "spawn_helper";
  87. args[2] = NULL;
  88. options.file = exepath;
  89. options.args = args;
  90. options.exit_cb = exit_cb;
  91. uv_pipe_init(loop, &out, 0);
  92. options.stdio = stdio;
  93. options.stdio_count = 2;
  94. options.stdio[0].flags = UV_IGNORE;
  95. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  96. options.stdio[1].data.stream = (uv_stream_t*)&out;
  97. r = uv_spawn(loop, &process, &options);
  98. ASSERT(r == 0);
  99. process_open = 1;
  100. pipe_open = 1;
  101. output_used = 0;
  102. r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
  103. ASSERT(r == 0);
  104. }
  105. BENCHMARK_IMPL(spawn) {
  106. int r;
  107. static int64_t start_time, end_time;
  108. loop = uv_default_loop();
  109. r = uv_exepath(exepath, &exepath_size);
  110. ASSERT(r == 0);
  111. exepath[exepath_size] = '\0';
  112. uv_update_time(loop);
  113. start_time = uv_now(loop);
  114. spawn();
  115. r = uv_run(loop, UV_RUN_DEFAULT);
  116. ASSERT(r == 0);
  117. uv_update_time(loop);
  118. end_time = uv_now(loop);
  119. LOGF("spawn: %.0f spawns/s\n",
  120. (double) N / (double) (end_time - start_time) * 1000.0);
  121. MAKE_VALGRIND_HAPPY();
  122. return 0;
  123. }