11var assert = require ( 'assert' ) ;
22var fs = require ( 'fs' ) ;
33var path = require ( 'path' ) ;
4- var spawn = require ( 'child_process' ) . spawn ;
4+ var child_process = require ( 'child_process' ) ;
55
66var silent = + process . env . NODE_BENCH_SILENT ;
77
88exports . PORT = process . env . PORT || 12346 ;
99
1010// If this is the main module, then run the benchmarks
1111if ( module === require . main ) {
12+ hasWrk ( ) ;
1213 var type = process . argv [ 2 ] ;
1314 var testFilter = process . argv [ 3 ] ;
1415 if ( ! type ) {
@@ -36,6 +37,15 @@ if (module === require.main) {
3637 runBenchmarks ( ) ;
3738}
3839
40+ function hasWrk ( ) {
41+ var result = child_process . spawnSync ( 'wrk' , [ '-h' ] ) ;
42+ if ( result . error . code === 'ENOENT' ) {
43+ console . error ( 'Couldn\'t locate `wrk` which is needed for running ' +
44+ 'benchmarks. Check benchmark/README.md for further instructions.' ) ;
45+ process . exit ( - 1 ) ;
46+ }
47+ }
48+
3949function runBenchmarks ( ) {
4050 var test = tests . shift ( ) ;
4151 if ( ! test )
@@ -48,7 +58,7 @@ function runBenchmarks() {
4858 test = path . resolve ( dir , test ) ;
4959
5060 var a = ( process . execArgv || [ ] ) . concat ( test ) ;
51- var child = spawn ( process . execPath , a , { stdio : 'inherit' } ) ;
61+ var child = child_process . spawn ( process . execPath , a , { stdio : 'inherit' } ) ;
5262 child . on ( 'close' , function ( code ) {
5363 if ( code ) {
5464 process . exit ( code ) ;
@@ -70,7 +80,10 @@ function Benchmark(fn, options) {
7080 this . _name = require . main . filename . split ( / b e n c h m a r k [ \/ \\ ] / ) . pop ( ) ;
7181 this . _start = [ 0 , 0 ] ;
7282 this . _started = false ;
83+
7384 var self = this ;
85+
86+ hasWrk ( ) ;
7487 process . nextTick ( function ( ) {
7588 self . _run ( ) ;
7689 } ) ;
@@ -85,7 +98,7 @@ Benchmark.prototype.http = function(p, args, cb) {
8598 args = args . concat ( url ) ;
8699
87100 var out = '' ;
88- var child = spawn ( 'wrk' , args ) ;
101+ var child = child_process . spawn ( 'wrk' , args ) ;
89102
90103 child . stdout . setEncoding ( 'utf8' ) ;
91104
@@ -145,7 +158,7 @@ Benchmark.prototype._run = function() {
145158 if ( ! argv )
146159 return ;
147160 argv = process . execArgv . concat ( argv ) ;
148- var child = spawn ( node , argv , { stdio : 'inherit' } ) ;
161+ var child = child_process . spawn ( node , argv , { stdio : 'inherit' } ) ;
149162 child . on ( 'close' , function ( code , signal ) {
150163 if ( code )
151164 console . error ( 'child process exited with code ' + code ) ;
0 commit comments