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, int exit_status, int term_signal) {
  53. ASSERT(exit_status == 42);
  54. ASSERT(term_signal == 0);
  55. uv_close((uv_handle_t*)process, process_close_cb);
  56. }
  57. static uv_buf_t on_alloc(uv_handle_t* handle, size_t suggested_size) {
  58. uv_buf_t buf;
  59. buf.base = output + output_used;
  60. buf.len = OUTPUT_SIZE - output_used;
  61. return buf;
  62. }
  63. static void pipe_close_cb(uv_handle_t* pipe) {
  64. ASSERT(pipe_open == 1);
  65. pipe_open = 0;
  66. maybe_spawn();
  67. }
  68. static void on_read(uv_stream_t* pipe, ssize_t nread, uv_buf_t buf) {
  69. uv_err_t err = uv_last_error(loop);
  70. if (nread > 0) {
  71. ASSERT(pipe_open == 1);
  72. output_used += nread;
  73. } else if (nread < 0) {
  74. if (err.code == UV_EOF) {
  75. uv_close((uv_handle_t*)pipe, pipe_close_cb);
  76. }
  77. }
  78. }
  79. static void spawn(void) {
  80. uv_stdio_container_t stdio[2];
  81. int r;
  82. ASSERT(process_open == 0);
  83. ASSERT(pipe_open == 0);
  84. args[0] = exepath;
  85. args[1] = "spawn_helper";
  86. args[2] = NULL;
  87. options.file = exepath;
  88. options.args = args;
  89. options.exit_cb = exit_cb;
  90. uv_pipe_init(loop, &out, 0);
  91. options.stdio = stdio;
  92. options.stdio_count = 2;
  93. options.stdio[0].flags = UV_IGNORE;
  94. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  95. options.stdio[1].data.stream = (uv_stream_t*)&out;
  96. r = uv_spawn(loop, &process, options);
  97. ASSERT(r == 0);
  98. process_open = 1;
  99. pipe_open = 1;
  100. output_used = 0;
  101. r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
  102. ASSERT(r == 0);
  103. }
  104. BENCHMARK_IMPL(spawn) {
  105. int r;
  106. static int64_t start_time, end_time;
  107. loop = uv_default_loop();
  108. r = uv_exepath(exepath, &exepath_size);
  109. ASSERT(r == 0);
  110. exepath[exepath_size] = '\0';
  111. uv_update_time(loop);
  112. start_time = uv_now(loop);
  113. spawn();
  114. r = uv_run(loop, UV_RUN_DEFAULT);
  115. ASSERT(r == 0);
  116. uv_update_time(loop);
  117. end_time = uv_now(loop);
  118. LOGF("spawn: %.0f spawns/s\n",
  119. (double) N / (double) (end_time - start_time) * 1000.0);
  120. MAKE_VALGRIND_HAPPY();
  121. return 0;
  122. }