11'use strict' ;
22
33const child_process = require ( 'child_process' ) ;
4-
5- // The port used by servers and wrk
6- exports . PORT = process . env . PORT || 12346 ;
4+ const http_benchmarkers = require ( './_http-benchmarkers.js' ) ;
75
86exports . createBenchmark = function ( fn , options ) {
97 return new Benchmark ( fn , options ) ;
108} ;
119
1210function Benchmark ( fn , options ) {
1311 this . name = require . main . filename . slice ( __dirname . length + 1 ) ;
14- this . options = this . _parseArgs ( process . argv . slice ( 2 ) , options ) ;
12+ const parsed_args = this . _parseArgs ( process . argv . slice ( 2 ) , options ) ;
13+ this . options = parsed_args . cli ;
14+ this . extra_options = parsed_args . extra ;
1515 this . queue = this . _queue ( this . options ) ;
1616 this . config = this . queue [ 0 ] ;
1717
@@ -29,7 +29,7 @@ function Benchmark(fn, options) {
2929
3030Benchmark . prototype . _parseArgs = function ( argv , options ) {
3131 const cliOptions = Object . assign ( { } , options ) ;
32-
32+ const extraOptions = { } ;
3333 // Parse configuration arguments
3434 for ( const arg of argv ) {
3535 const match = arg . match ( / ^ ( .+ ?) = ( [ \s \S ] * ) $ / ) ;
@@ -38,14 +38,16 @@ Benchmark.prototype._parseArgs = function(argv, options) {
3838 process . exit ( 1 ) ;
3939 }
4040
41- // Infer the type from the options object and parse accordingly
42- const isNumber = typeof options [ match [ 1 ] ] [ 0 ] === 'number' ;
43- const value = isNumber ? + match [ 2 ] : match [ 2 ] ;
44-
45- cliOptions [ match [ 1 ] ] = [ value ] ;
41+ if ( options [ match [ 1 ] ] ) {
42+ // Infer the type from the options object and parse accordingly
43+ const isNumber = typeof options [ match [ 1 ] ] [ 0 ] === 'number' ;
44+ const value = isNumber ? + match [ 2 ] : match [ 2 ] ;
45+ cliOptions [ match [ 1 ] ] = [ value ] ;
46+ } else {
47+ extraOptions [ match [ 1 ] ] = match [ 2 ] ;
48+ }
4649 }
47-
48- return cliOptions ;
50+ return { cli : cliOptions , extra : extraOptions } ;
4951} ;
5052
5153Benchmark . prototype . _queue = function ( options ) {
@@ -88,51 +90,29 @@ Benchmark.prototype._queue = function(options) {
8890 return queue ;
8991} ;
9092
91- function hasWrk ( ) {
92- const result = child_process . spawnSync ( 'wrk' , [ '-h' ] ) ;
93- if ( result . error && result . error . code === 'ENOENT' ) {
94- console . error ( 'Couldn\'t locate `wrk` which is needed for running ' +
95- 'benchmarks. Check benchmark/README.md for further instructions.' ) ;
96- process . exit ( 1 ) ;
97- }
98- }
93+ // Benchmark an http server.
94+ exports . default_http_benchmarker =
95+ http_benchmarkers . default_http_benchmarker ;
96+ exports . PORT = http_benchmarkers . PORT ;
9997
100- // benchmark an http server.
101- const WRK_REGEXP = / R e q u e s t s \/ s e c : [ \t ] + ( [ 0 - 9 \. ] + ) / ;
102- Benchmark . prototype . http = function ( urlPath , args , cb ) {
103- hasWrk ( ) ;
98+ Benchmark . prototype . http = function ( options , cb ) {
10499 const self = this ;
105-
106- const urlFull = 'http://127.0.0.1:' + exports . PORT + urlPath ;
107- args = args . concat ( urlFull ) ;
108-
109- const childStart = process . hrtime ( ) ;
110- const child = child_process . spawn ( 'wrk' , args ) ;
111- child . stderr . pipe ( process . stderr ) ;
112-
113- // Collect stdout
114- let stdout = '' ;
115- child . stdout . on ( 'data' , ( chunk ) => stdout += chunk . toString ( ) ) ;
116-
117- child . once ( 'close' , function ( code ) {
118- const elapsed = process . hrtime ( childStart ) ;
119- if ( cb ) cb ( code ) ;
120-
121- if ( code ) {
122- console . error ( 'wrk failed with ' + code ) ;
123- process . exit ( code ) ;
100+ const http_options = Object . assign ( { } , options ) ;
101+ http_options . benchmarker = http_options . benchmarker ||
102+ self . config . benchmarker ||
103+ self . extra_options . benchmarker ||
104+ exports . default_http_benchmarker ;
105+ http_benchmarkers . run ( http_options , function ( error , code , used_benchmarker ,
106+ result , elapsed ) {
107+ if ( cb ) {
108+ cb ( code ) ;
124109 }
125-
126- // Extract requests pr second and check for odd results
127- const match = stdout . match ( WRK_REGEXP ) ;
128- if ( ! match || match . length <= 1 ) {
129- console . error ( 'wrk produced strange output:' ) ;
130- console . error ( stdout ) ;
131- process . exit ( 1 ) ;
110+ if ( error ) {
111+ console . error ( error ) ;
112+ process . exit ( code || 1 ) ;
132113 }
133-
134- // Report rate
135- self . report ( + match [ 1 ] , elapsed ) ;
114+ self . config . benchmarker = used_benchmarker ;
115+ self . report ( result , elapsed ) ;
136116 } ) ;
137117} ;
138118
@@ -152,6 +132,9 @@ Benchmark.prototype._run = function() {
152132 for ( const key of Object . keys ( config ) ) {
153133 childArgs . push ( `${ key } =${ config [ key ] } ` ) ;
154134 }
135+ for ( const key of Object . keys ( self . extra_options ) ) {
136+ childArgs . push ( `${ key } =${ self . extra_options [ key ] } ` ) ;
137+ }
155138
156139 const child = child_process . fork ( require . main . filename , childArgs , {
157140 env : childEnv
0 commit comments