Skip to content

Commit dbbc9d1

Browse files
committed
src: add pretty print
This commit add supports to console output and JSON output. It checks if the script was run without echoing to a file: ``` $ node index.js ``` Then caling pretty console ouput, or when pipe is found ``` $ node index.js > results.json ``` Resulting in a JSON output.
1 parent e93f2d9 commit dbbc9d1

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

console-output.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const opsSecFormatter = Intl.NumberFormat('en-US', { notation: 'compact', maximumSignificantDigits: 4})
2+
3+
module.exports = {
4+
log (str) {
5+
console.log(str);
6+
},
7+
printResults (results) {
8+
const halfScreen = process.stdout.columns / 2;
9+
console.log('-'.repeat(halfScreen));
10+
for (const result of results) {
11+
console.log(`${result.name}`);
12+
// TODO(rafaelgss): support non-operation method
13+
for (const operation of result.operations) {
14+
const opName = ` ${operation.name}:`;
15+
const spaces = ' '.repeat(halfScreen - opName.length);
16+
console.log(opName +
17+
`${spaces}${opsSecFormatter.format(operation.opsSec)} (${operation.samples} samples)`);
18+
}
19+
}
20+
console.log('-'.repeat(process.stdout.columns / 2));
21+
}
22+
}

index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,32 @@ const piscina = new Piscina({
1212
maxQueue: 1,
1313
});
1414

15+
let output;
16+
// Considering this script won't be called as a
17+
// child_process, stdout.isTTY should be reliable enough.
18+
if (process.stdout.isTTY) {
19+
output = require('./console-output');
20+
} else {
21+
output = {
22+
log: () => {},
23+
printResults: (results) => {
24+
console.log(JSON.stringify(results, null, 2));
25+
}
26+
}
27+
}
28+
1529
async function main() {
1630
const files = await fs.readdir(path.join(__dirname, './src'));
31+
output.log('Running Node.js Package Benchmark...');
32+
const results = [];
1733
for (const file of files) {
1834
if (file.match(/.*-benchmark\.js$/)) {
1935
const benchFile = path.join(__dirname, './src/', file);
2036
const result = await piscina.run(benchFile);
21-
console.log('results', JSON.stringify(result, null, 2));
37+
results.push(result);
2238
}
2339
}
40+
output.printResults(results);
2441
}
2542

2643
main();

0 commit comments

Comments
 (0)