benchmark: add output format option [csv]
authorBrendan Ashworth <brendan.ashworth@me.com>
Tue, 10 Feb 2015 03:36:52 +0000 (19:36 -0800)
committerBrendan Ashworth <brendan.ashworth@me.com>
Tue, 17 Mar 2015 04:27:17 +0000 (21:27 -0700)
This commit adds an `OUTPUT_FORMAT` environment variable option for
all benchmark tests that allow either 'csv' or 'default' output. Default
output has been left unchanged, and csv output prints out the csv
headers along with the csv formatted per-test output, each test also
seperated by a newline.

It can be used like the following:
$ OUTPUT_FORMAT=csv iojs benchmark/common.js http

Not specifying the OUTPUT_FORMAT env var will default to 'default'.
Specifying a bad value will throw an error.

PR-URL: https://github.com/iojs/io.js/pull/777
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
benchmark/common.js

index 2a51874..199d9a5 100644 (file)
@@ -3,7 +3,14 @@ var fs = require('fs');
 var path = require('path');
 var child_process = require('child_process');
 
-var silent = +process.env.NODE_BENCH_SILENT;
+var outputFormat = process.env.OUTPUT_FORMAT ||
+                   (+process.env.NODE_BENCH_SILENT ? 'silent' : false) ||
+                   'default';
+
+// verify outputFormat
+if (['default', 'csv', 'silent'].indexOf(outputFormat) == -1) {
+  throw new Error('OUTPUT_FORMAT set to invalid value');
+}
 
 exports.PORT = process.env.PORT || 12346;
 
@@ -54,7 +61,9 @@ function runBenchmarks() {
   if (test.match(/^[\._]/))
     return process.nextTick(runBenchmarks);
 
-  console.error(type + '/' + test);
+  if (outputFormat == 'default')
+    console.error(type + '/' + test);
+
   test = path.resolve(dir, test);
 
   var a = (process.execArgv || []).concat(test);
@@ -151,6 +160,10 @@ Benchmark.prototype._run = function() {
     return newSet;
   }, [[main]]);
 
+  // output csv heading
+  if (outputFormat == 'csv')
+    console.log('filename,' + Object.keys(options).join(',') + ',result');
+
   var node = process.execPath;
   var i = 0;
   function run() {
@@ -216,15 +229,25 @@ Benchmark.prototype.end = function(operations) {
 
 Benchmark.prototype.report = function(value) {
   var heading = this.getHeading();
-  if (!silent)
+
+  if (outputFormat == 'default')
     console.log('%s: %s', heading, value.toFixed(5));
+  else if (outputFormat == 'csv')
+    console.log('%s,%s', heading, value.toFixed(5));
 
   process.exit(0);
 };
 
 Benchmark.prototype.getHeading = function() {
   var conf = this.config;
-  return this._name + ' ' + Object.keys(conf).map(function(key) {
-    return key + '=' + conf[key];
-  }).join(' ');
+
+  if (outputFormat == 'default') {
+    return this._name + ' ' + Object.keys(conf).map(function(key) {
+      return key + '=' + conf[key];
+    }).join(' ');
+  } else if (outputFormat == 'csv') {
+    return this._name + ',' + Object.keys(conf).map(function(key) {
+      return conf[key];
+    }).join(',');
+  }
 };