From 37077de83dbb97f1b51839fb0ebff608aa9ea3c8 Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 11 Feb 2013 23:21:48 -0800 Subject: [PATCH] bench: add runner --- benchmark/common.js | 59 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/benchmark/common.js b/benchmark/common.js index 04cf382a0c9..289f17d1280 100644 --- a/benchmark/common.js +++ b/benchmark/common.js @@ -3,6 +3,45 @@ var path = require('path'); exports.PORT = process.env.PORT || 12346; +// If this is the main module, then run the benchmarks +if (module === require.main) { + var type = process.argv[2]; + if (!type) { + console.error('usage:\n ./node benchmark/common.js '); + process.exit(1); + } + + var path = require('path'); + var fs = require('fs'); + var dir = path.join(__dirname, type); + var tests = fs.readdirSync(dir); + var spawn = require('child_process').spawn; + + runBenchmarks(); + + function runBenchmarks() { + var test = tests.shift(); + if (!test) + return; + + if (test.match(/^[\._]/)) + return process.nextTick(runBenchmarks); + + console.error(type + '/' + test); + test = path.resolve(dir, test); + + var child = spawn(process.execPath, [ test ], { stdio: 'inherit' }); + child.on('close', function(code) { + if (code) + process.exit(code); + else { + console.log(''); + runBenchmarks(); + } + }); + } +} + exports.createBenchmark = function(fn, options) { return new Benchmark(fn, options); }; @@ -11,7 +50,7 @@ function Benchmark(fn, options) { this.fn = fn; this.options = options; this.config = parseOpts(options); - this._name = path.basename(require.main.filename, '.js'); + this._name = require.main.filename.split(/benchmark[\/\\]/).pop(); this._start = [0,0]; this._started = false; var self = this; @@ -76,7 +115,7 @@ Benchmark.prototype._run = function() { var j = 0; set.forEach(function(s) { vals.forEach(function(val) { - newSet[j++] = s.concat('--' + key + '=' + val); + newSet[j++] = s.concat(key + '=' + val); }); }); return newSet; @@ -101,13 +140,13 @@ Benchmark.prototype._run = function() { }; function parseOpts(options) { - // verify that there's an --option provided for each of the options + // verify that there's an option provided for each of the options // if they're not *all* specified, then we return null. var keys = Object.keys(options); var num = keys.length; var conf = {}; for (var i = 2; i < process.argv.length; i++) { - var m = process.argv[i].match(/^--(.+)=(.+)$/); + var m = process.argv[i].match(/^(.+)=(.+)$/); if (!m || !m[1] || !m[2] || !options[m[1]]) return null; else { @@ -115,6 +154,12 @@ function parseOpts(options) { num--; } } + // still go ahead and set whatever WAS set, if it was. + if (num !== 0) { + Object.keys(conf).forEach(function(k) { + options[k] = [conf[k]]; + }); + } return num === 0 ? conf : null; }; @@ -144,7 +189,7 @@ Benchmark.prototype.report = function(value) { Benchmark.prototype.getHeading = function() { var conf = this.config; - return this._name + '_' + Object.keys(conf).map(function(key) { - return key + '_' + conf[key]; - }).join('_'); + return this._name + ' ' + Object.keys(conf).map(function(key) { + return key + '=' + conf[key]; + }).join(' '); }