1
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
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:
10
* The above copyright notice and this permission notice shall be included in
11
* all copies or substantial portions of the Software.
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
22
/* This benchmark spawns itself 1000 times. */
27
static uv_loop_t* loop;
32
static uv_process_t process;
33
static uv_process_options_t options = { 0 };
34
static char exepath[1024];
35
static size_t exepath_size = 1024;
39
#define OUTPUT_SIZE 1024
40
static char output[OUTPUT_SIZE];
41
static int output_used;
43
static int process_open;
51
if (process_open == 0 && pipe_open == 0) {
60
static void process_close_cb(uv_handle_t* handle) {
61
ASSERT(process_open == 1);
67
static void exit_cb(uv_process_t* process, int exit_status, int term_signal) {
68
ASSERT(exit_status == 42);
69
ASSERT(term_signal == 0);
70
uv_close((uv_handle_t*)process, process_close_cb);
74
uv_buf_t on_alloc(uv_handle_t* handle, size_t suggested_size) {
76
buf.base = output + output_used;
77
buf.len = OUTPUT_SIZE - output_used;
82
void pipe_close_cb(uv_handle_t* pipe) {
83
ASSERT(pipe_open == 1);
89
void on_read(uv_stream_t* pipe, ssize_t nread, uv_buf_t buf) {
90
uv_err_t err = uv_last_error(loop);
93
ASSERT(pipe_open == 1);
95
} else if (nread < 0) {
96
if (err.code == UV_EOF) {
97
uv_close((uv_handle_t*)pipe, pipe_close_cb);
103
static void spawn() {
106
ASSERT(process_open == 0);
107
ASSERT(pipe_open == 0);
110
args[1] = "spawn_helper";
112
options.file = exepath;
114
options.exit_cb = exit_cb;
116
uv_pipe_init(loop, &out, 0);
117
options.stdout_stream = &out;
119
r = uv_spawn(loop, &process, options);
126
r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
131
BENCHMARK_IMPL(spawn) {
133
static int64_t start_time, end_time;
135
loop = uv_default_loop();
137
r = uv_exepath(exepath, &exepath_size);
139
exepath[exepath_size] = '\0';
141
uv_update_time(loop);
142
start_time = uv_now(loop);
149
uv_update_time(loop);
150
end_time = uv_now(loop);
152
LOGF("spawn: %.0f spawns/s\n",
153
(double) N / (double) (end_time - start_time) * 1000.0);