Skip to content

Commit

Permalink
benchmark: add --format csv option
Browse files Browse the repository at this point in the history
Added the option of using --format csv when outputting data.

Fixes: nodejs#7890
PR-URL: nodejs#7961
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Andreas Madsen <[email protected]>
  • Loading branch information
adrian-nitu-92 authored and AndreasMadsen committed Aug 12, 2016
1 parent 4b527a4 commit 474e629
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions benchmark/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,54 @@ const cli = CLI(`usage: ./node run.js [options] [--] <category> ...
--filter pattern string to filter benchmark scripts
--set variable=value set benchmark variable (can be repeated)
--format [simple|csv] optional value that specifies the output format
`, {
arrayArgs: ['set']
});
const benchmarks = cli.benchmarks();

if (benchmarks.length === 0) {
console.error('no benchmarks found');
process.exit(1);
console.error('No benchmarks found');
process.exitCode = 1;
return;
}

const validFormats = ['csv', 'simple'];
const format = cli.optional.format || 'simple';
if (!validFormats.includes(format)) {
console.error('Invalid format detected');
process.exitCode = 1;
return;
}

if (format === 'csv') {
console.log('"filename", "configuration", "rate", "time"');
}

(function recursive(i) {
const filename = benchmarks[i];
const child = fork(path.resolve(__dirname, filename), cli.optional.set);

console.log();
console.log(filename);
if (format !== 'csv') {
console.log();
console.log(filename);
}

child.on('message', function(data) {
// Construct configuration string, " A=a, B=b, ..."
let conf = '';
for (const key of Object.keys(data.conf)) {
conf += ' ' + key + '=' + JSON.stringify(data.conf[key]);
}

console.log(`${data.name}${conf}: ${data.rate}`);
// delete first space of the configuration
conf = conf.slice(1);
if (format === 'csv') {
// Escape quotes (") for correct csv formatting
conf = conf.replace(/"/g, '""');
console.log(`"${data.name}", "${conf}", ${data.rate}, ${data.time}`);
} else {
console.log(`${data.name} ${conf}: ${data.rate}`);
}
});

child.once('close', function(code) {
Expand Down

0 comments on commit 474e629

Please sign in to comment.