1*0e552da7Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2*0e552da7Schristos *
3*0e552da7Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
4*0e552da7Schristos * of this software and associated documentation files (the "Software"), to
5*0e552da7Schristos * deal in the Software without restriction, including without limitation the
6*0e552da7Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7*0e552da7Schristos * sell copies of the Software, and to permit persons to whom the Software is
8*0e552da7Schristos * furnished to do so, subject to the following conditions:
9*0e552da7Schristos *
10*0e552da7Schristos * The above copyright notice and this permission notice shall be included in
11*0e552da7Schristos * all copies or substantial portions of the Software.
12*0e552da7Schristos *
13*0e552da7Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*0e552da7Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*0e552da7Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*0e552da7Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17*0e552da7Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18*0e552da7Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19*0e552da7Schristos * IN THE SOFTWARE.
20*0e552da7Schristos */
21*0e552da7Schristos
22*0e552da7Schristos /* This benchmark spawns itself 1000 times. */
23*0e552da7Schristos
24*0e552da7Schristos #include "task.h"
25*0e552da7Schristos #include "uv.h"
26*0e552da7Schristos
27*0e552da7Schristos static uv_loop_t* loop;
28*0e552da7Schristos
29*0e552da7Schristos static int N = 1000;
30*0e552da7Schristos static int done;
31*0e552da7Schristos
32*0e552da7Schristos static uv_process_t process;
33*0e552da7Schristos static uv_process_options_t options;
34*0e552da7Schristos static char exepath[1024];
35*0e552da7Schristos static size_t exepath_size = 1024;
36*0e552da7Schristos static char* args[3];
37*0e552da7Schristos static uv_pipe_t out;
38*0e552da7Schristos
39*0e552da7Schristos #define OUTPUT_SIZE 1024
40*0e552da7Schristos static char output[OUTPUT_SIZE];
41*0e552da7Schristos static int output_used;
42*0e552da7Schristos
43*0e552da7Schristos static int process_open;
44*0e552da7Schristos static int pipe_open;
45*0e552da7Schristos
46*0e552da7Schristos
47*0e552da7Schristos static void spawn(void);
48*0e552da7Schristos
49*0e552da7Schristos
maybe_spawn(void)50*0e552da7Schristos static void maybe_spawn(void) {
51*0e552da7Schristos if (process_open == 0 && pipe_open == 0) {
52*0e552da7Schristos done++;
53*0e552da7Schristos if (done < N) {
54*0e552da7Schristos spawn();
55*0e552da7Schristos }
56*0e552da7Schristos }
57*0e552da7Schristos }
58*0e552da7Schristos
59*0e552da7Schristos
process_close_cb(uv_handle_t * handle)60*0e552da7Schristos static void process_close_cb(uv_handle_t* handle) {
61*0e552da7Schristos ASSERT(process_open == 1);
62*0e552da7Schristos process_open = 0;
63*0e552da7Schristos maybe_spawn();
64*0e552da7Schristos }
65*0e552da7Schristos
66*0e552da7Schristos
exit_cb(uv_process_t * process,int64_t exit_status,int term_signal)67*0e552da7Schristos static void exit_cb(uv_process_t* process,
68*0e552da7Schristos int64_t exit_status,
69*0e552da7Schristos int term_signal) {
70*0e552da7Schristos ASSERT(exit_status == 42);
71*0e552da7Schristos ASSERT(term_signal == 0);
72*0e552da7Schristos uv_close((uv_handle_t*)process, process_close_cb);
73*0e552da7Schristos }
74*0e552da7Schristos
75*0e552da7Schristos
on_alloc(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)76*0e552da7Schristos static void on_alloc(uv_handle_t* handle,
77*0e552da7Schristos size_t suggested_size,
78*0e552da7Schristos uv_buf_t* buf) {
79*0e552da7Schristos buf->base = output + output_used;
80*0e552da7Schristos buf->len = OUTPUT_SIZE - output_used;
81*0e552da7Schristos }
82*0e552da7Schristos
83*0e552da7Schristos
pipe_close_cb(uv_handle_t * pipe)84*0e552da7Schristos static void pipe_close_cb(uv_handle_t* pipe) {
85*0e552da7Schristos ASSERT(pipe_open == 1);
86*0e552da7Schristos pipe_open = 0;
87*0e552da7Schristos maybe_spawn();
88*0e552da7Schristos }
89*0e552da7Schristos
90*0e552da7Schristos
on_read(uv_stream_t * pipe,ssize_t nread,const uv_buf_t * buf)91*0e552da7Schristos static void on_read(uv_stream_t* pipe, ssize_t nread, const uv_buf_t* buf) {
92*0e552da7Schristos if (nread > 0) {
93*0e552da7Schristos ASSERT(pipe_open == 1);
94*0e552da7Schristos output_used += nread;
95*0e552da7Schristos } else if (nread < 0) {
96*0e552da7Schristos if (nread == UV_EOF) {
97*0e552da7Schristos uv_close((uv_handle_t*)pipe, pipe_close_cb);
98*0e552da7Schristos }
99*0e552da7Schristos }
100*0e552da7Schristos }
101*0e552da7Schristos
102*0e552da7Schristos
spawn(void)103*0e552da7Schristos static void spawn(void) {
104*0e552da7Schristos uv_stdio_container_t stdio[2];
105*0e552da7Schristos int r;
106*0e552da7Schristos
107*0e552da7Schristos ASSERT(process_open == 0);
108*0e552da7Schristos ASSERT(pipe_open == 0);
109*0e552da7Schristos
110*0e552da7Schristos args[0] = exepath;
111*0e552da7Schristos args[1] = "spawn_helper";
112*0e552da7Schristos args[2] = NULL;
113*0e552da7Schristos options.file = exepath;
114*0e552da7Schristos options.args = args;
115*0e552da7Schristos options.exit_cb = exit_cb;
116*0e552da7Schristos
117*0e552da7Schristos uv_pipe_init(loop, &out, 0);
118*0e552da7Schristos
119*0e552da7Schristos options.stdio = stdio;
120*0e552da7Schristos options.stdio_count = 2;
121*0e552da7Schristos options.stdio[0].flags = UV_IGNORE;
122*0e552da7Schristos options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
123*0e552da7Schristos options.stdio[1].data.stream = (uv_stream_t*)&out;
124*0e552da7Schristos
125*0e552da7Schristos r = uv_spawn(loop, &process, &options);
126*0e552da7Schristos ASSERT(r == 0);
127*0e552da7Schristos
128*0e552da7Schristos process_open = 1;
129*0e552da7Schristos pipe_open = 1;
130*0e552da7Schristos output_used = 0;
131*0e552da7Schristos
132*0e552da7Schristos r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
133*0e552da7Schristos ASSERT(r == 0);
134*0e552da7Schristos }
135*0e552da7Schristos
136*0e552da7Schristos
BENCHMARK_IMPL(spawn)137*0e552da7Schristos BENCHMARK_IMPL(spawn) {
138*0e552da7Schristos int r;
139*0e552da7Schristos static int64_t start_time, end_time;
140*0e552da7Schristos
141*0e552da7Schristos loop = uv_default_loop();
142*0e552da7Schristos
143*0e552da7Schristos r = uv_exepath(exepath, &exepath_size);
144*0e552da7Schristos ASSERT(r == 0);
145*0e552da7Schristos exepath[exepath_size] = '\0';
146*0e552da7Schristos
147*0e552da7Schristos uv_update_time(loop);
148*0e552da7Schristos start_time = uv_now(loop);
149*0e552da7Schristos
150*0e552da7Schristos spawn();
151*0e552da7Schristos
152*0e552da7Schristos r = uv_run(loop, UV_RUN_DEFAULT);
153*0e552da7Schristos ASSERT(r == 0);
154*0e552da7Schristos
155*0e552da7Schristos uv_update_time(loop);
156*0e552da7Schristos end_time = uv_now(loop);
157*0e552da7Schristos
158*0e552da7Schristos fprintf(stderr, "spawn: %.0f spawns/s\n",
159*0e552da7Schristos (double) N / (double) (end_time - start_time) * 1000.0);
160*0e552da7Schristos fflush(stderr);
161*0e552da7Schristos
162*0e552da7Schristos MAKE_VALGRIND_HAPPY();
163*0e552da7Schristos return 0;
164*0e552da7Schristos }
165