1
1
var assert = require ( 'assert' ) ;
2
+ var fs = require ( 'fs' ) ;
2
3
var path = require ( 'path' ) ;
4
+ var spawn = require ( 'child_process' ) . spawn ;
5
+
3
6
var silent = + process . env . NODE_BENCH_SILENT ;
4
7
5
8
exports . PORT = process . env . PORT || 12346 ;
@@ -13,17 +16,16 @@ if (module === require.main) {
13
16
process . exit ( 1 ) ;
14
17
}
15
18
16
- var fs = require ( 'fs' ) ;
17
19
var dir = path . join ( __dirname , type ) ;
18
20
var tests = fs . readdirSync ( dir ) ;
19
- var spawn = require ( 'child_process' ) . spawn ;
20
21
21
22
if ( testFilter ) {
22
23
var filteredTests = tests . filter ( function ( item ) {
23
24
if ( item . lastIndexOf ( testFilter ) >= 0 ) {
24
25
return item ;
25
26
}
26
27
} ) ;
28
+
27
29
if ( filteredTests . length === 0 ) {
28
30
console . error ( `${ testFilter } is not found in \n ${ tests . join ( ' \n' ) } ` ) ;
29
31
return ;
@@ -48,9 +50,9 @@ function runBenchmarks() {
48
50
var a = ( process . execArgv || [ ] ) . concat ( test ) ;
49
51
var child = spawn ( process . execPath , a , { stdio : 'inherit' } ) ;
50
52
child . on ( 'close' , function ( code ) {
51
- if ( code )
53
+ if ( code ) {
52
54
process . exit ( code ) ;
53
- else {
55
+ } else {
54
56
console . log ( '' ) ;
55
57
runBenchmarks ( ) ;
56
58
}
@@ -79,7 +81,6 @@ Benchmark.prototype.http = function(p, args, cb) {
79
81
var self = this ;
80
82
var wrk = path . resolve ( __dirname , '..' , 'tools' , 'wrk' , 'wrk' ) ;
81
83
var regexp = / R e q u e s t s \/ s e c : [ \t ] + ( [ 0 - 9 \. ] + ) / ;
82
- var spawn = require ( 'child_process' ) . spawn ;
83
84
var url = 'http://127.0.0.1:' + exports . PORT + p ;
84
85
85
86
args = args . concat ( url ) ;
@@ -101,8 +102,8 @@ Benchmark.prototype.http = function(p, args, cb) {
101
102
console . error ( 'wrk failed with ' + code ) ;
102
103
process . exit ( code )
103
104
}
104
- var m = out . match ( regexp ) ;
105
- var qps = m && + m [ 1 ] ;
105
+ var match = out . match ( regexp ) ;
106
+ var qps = match && + match [ 1 ] ;
106
107
if ( ! qps ) {
107
108
console . error ( '%j' , out ) ;
108
109
console . error ( 'wrk produced strange output' ) ;
@@ -138,7 +139,6 @@ Benchmark.prototype._run = function() {
138
139
return newSet ;
139
140
} , [ [ main ] ] ) ;
140
141
141
- var spawn = require ( 'child_process' ) . spawn ;
142
142
var node = process . execPath ;
143
143
var i = 0 ;
144
144
function run ( ) {
@@ -163,11 +163,11 @@ function parseOpts(options) {
163
163
var num = keys . length ;
164
164
var conf = { } ;
165
165
for ( var i = 2 ; i < process . argv . length ; i ++ ) {
166
- var m = process . argv [ i ] . match ( / ^ ( .+ ) = ( .+ ) $ / ) ;
167
- if ( ! m || ! m [ 1 ] || ! m [ 2 ] || ! options [ m [ 1 ] ] )
166
+ var match = process . argv [ i ] . match ( / ^ ( .+ ) = ( .+ ) $ / ) ;
167
+ if ( ! match || ! match [ 1 ] || ! match [ 2 ] || ! options [ match [ 1 ] ] ) {
168
168
return null ;
169
- else {
170
- conf [ m [ 1 ] ] = isFinite ( m [ 2 ] ) ? + m [ 2 ] : m [ 2 ]
169
+ } else {
170
+ conf [ match [ 1 ] ] = isFinite ( match [ 2 ] ) ? + match [ 2 ] : match [ 2 ]
171
171
num -- ;
172
172
}
173
173
}
@@ -183,16 +183,19 @@ function parseOpts(options) {
183
183
Benchmark . prototype . start = function ( ) {
184
184
if ( this . _started )
185
185
throw new Error ( 'Called start more than once in a single benchmark' ) ;
186
+
186
187
this . _started = true ;
187
188
this . _start = process . hrtime ( ) ;
188
189
} ;
189
190
190
191
Benchmark . prototype . end = function ( operations ) {
191
192
var elapsed = process . hrtime ( this . _start ) ;
193
+
192
194
if ( ! this . _started )
193
195
throw new Error ( 'called end without start' ) ;
194
196
if ( typeof operations !== 'number' )
195
197
throw new Error ( 'called end() without specifying operation count' ) ;
198
+
196
199
var time = elapsed [ 0 ] + elapsed [ 1 ] / 1e9 ;
197
200
var rate = operations / time ;
198
201
this . report ( rate ) ;
@@ -202,6 +205,7 @@ Benchmark.prototype.report = function(value) {
202
205
var heading = this . getHeading ( ) ;
203
206
if ( ! silent )
204
207
console . log ( '%s: %s' , heading , value . toFixed ( 5 ) ) ;
208
+
205
209
process . exit ( 0 ) ;
206
210
} ;
207
211
@@ -210,4 +214,4 @@ Benchmark.prototype.getHeading = function() {
210
214
return this . _name + ' ' + Object . keys ( conf ) . map ( function ( key ) {
211
215
return key + '=' + conf [ key ] ;
212
216
} ) . join ( ' ' ) ;
213
- }
217
+ } ;
0 commit comments