Skip to content

Commit 89dd8e0

Browse files
brendanashworthStephen Belanger
authored and
Stephen Belanger
committed
benchmark: clean up common.js
This commit cleans up `benchmark/common.js` with a few generic changes such as the following: - declare all `require()`'d libraries at the top instead of in the middle - add some empty whitespace where it helps readability - changes ambiguous variable names - standardizes most if / else blocks - missing semicolons PR-URL: #662 Reviewed-By: Nicu Micleușanu <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Stephen Belanger <[email protected]>
1 parent 6561274 commit 89dd8e0

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

benchmark/common.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
var assert = require('assert');
2+
var fs = require('fs');
23
var path = require('path');
4+
var spawn = require('child_process').spawn;
5+
36
var silent = +process.env.NODE_BENCH_SILENT;
47

58
exports.PORT = process.env.PORT || 12346;
@@ -13,17 +16,16 @@ if (module === require.main) {
1316
process.exit(1);
1417
}
1518

16-
var fs = require('fs');
1719
var dir = path.join(__dirname, type);
1820
var tests = fs.readdirSync(dir);
19-
var spawn = require('child_process').spawn;
2021

2122
if (testFilter) {
2223
var filteredTests = tests.filter(function(item){
2324
if (item.lastIndexOf(testFilter) >= 0) {
2425
return item;
2526
}
2627
});
28+
2729
if (filteredTests.length === 0) {
2830
console.error(`${testFilter} is not found in \n ${tests.join(' \n')}`);
2931
return;
@@ -48,9 +50,9 @@ function runBenchmarks() {
4850
var a = (process.execArgv || []).concat(test);
4951
var child = spawn(process.execPath, a, { stdio: 'inherit' });
5052
child.on('close', function(code) {
51-
if (code)
53+
if (code) {
5254
process.exit(code);
53-
else {
55+
} else {
5456
console.log('');
5557
runBenchmarks();
5658
}
@@ -79,7 +81,6 @@ Benchmark.prototype.http = function(p, args, cb) {
7981
var self = this;
8082
var wrk = path.resolve(__dirname, '..', 'tools', 'wrk', 'wrk');
8183
var regexp = /Requests\/sec:[ \t]+([0-9\.]+)/;
82-
var spawn = require('child_process').spawn;
8384
var url = 'http://127.0.0.1:' + exports.PORT + p;
8485

8586
args = args.concat(url);
@@ -101,8 +102,8 @@ Benchmark.prototype.http = function(p, args, cb) {
101102
console.error('wrk failed with ' + code);
102103
process.exit(code)
103104
}
104-
var m = out.match(regexp);
105-
var qps = m && +m[1];
105+
var match = out.match(regexp);
106+
var qps = match && +match[1];
106107
if (!qps) {
107108
console.error('%j', out);
108109
console.error('wrk produced strange output');
@@ -138,7 +139,6 @@ Benchmark.prototype._run = function() {
138139
return newSet;
139140
}, [[main]]);
140141

141-
var spawn = require('child_process').spawn;
142142
var node = process.execPath;
143143
var i = 0;
144144
function run() {
@@ -163,11 +163,11 @@ function parseOpts(options) {
163163
var num = keys.length;
164164
var conf = {};
165165
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]]) {
168168
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]
171171
num--;
172172
}
173173
}
@@ -183,16 +183,19 @@ function parseOpts(options) {
183183
Benchmark.prototype.start = function() {
184184
if (this._started)
185185
throw new Error('Called start more than once in a single benchmark');
186+
186187
this._started = true;
187188
this._start = process.hrtime();
188189
};
189190

190191
Benchmark.prototype.end = function(operations) {
191192
var elapsed = process.hrtime(this._start);
193+
192194
if (!this._started)
193195
throw new Error('called end without start');
194196
if (typeof operations !== 'number')
195197
throw new Error('called end() without specifying operation count');
198+
196199
var time = elapsed[0] + elapsed[1]/1e9;
197200
var rate = operations/time;
198201
this.report(rate);
@@ -202,6 +205,7 @@ Benchmark.prototype.report = function(value) {
202205
var heading = this.getHeading();
203206
if (!silent)
204207
console.log('%s: %s', heading, value.toFixed(5));
208+
205209
process.exit(0);
206210
};
207211

@@ -210,4 +214,4 @@ Benchmark.prototype.getHeading = function() {
210214
return this._name + ' ' + Object.keys(conf).map(function(key) {
211215
return key + '=' + conf[key];
212216
}).join(' ');
213-
}
217+
};

0 commit comments

Comments
 (0)