From 1c844a10db0102d313a86e7565c15d028100919d Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Mon, 4 Mar 2024 17:00:41 -0300 Subject: [PATCH 1/3] 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. --- console-output.js | 22 ++++++++++++++++++++++ index.js | 19 ++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 console-output.js diff --git a/console-output.js b/console-output.js new file mode 100644 index 0000000..7649964 --- /dev/null +++ b/console-output.js @@ -0,0 +1,22 @@ +const opsSecFormatter = Intl.NumberFormat('en-US', { notation: 'compact', maximumSignificantDigits: 4}) + +module.exports = { + log (str) { + console.log(str); + }, + printResults (results) { + const halfScreen = process.stdout.columns / 2; + console.log('-'.repeat(halfScreen)); + for (const result of results) { + console.log(`${result.name}`); + // TODO(rafaelgss): support non-operation method + for (const operation of result.operations) { + const opName = ` ${operation.name}:`; + const spaces = ' '.repeat(halfScreen - opName.length); + console.log(opName + + `${spaces}${opsSecFormatter.format(operation.opsSec)} (${operation.samples} samples)`); + } + } + console.log('-'.repeat(process.stdout.columns / 2)); + } +} diff --git a/index.js b/index.js index 65b7d6f..9e5bdac 100644 --- a/index.js +++ b/index.js @@ -12,15 +12,32 @@ const piscina = new Piscina({ maxQueue: 1, }); +let output; +// Considering this script won't be called as a +// child_process, stdout.isTTY should be reliable enough. +if (process.stdout.isTTY) { + output = require('./console-output'); +} else { + output = { + log: () => {}, + printResults: (results) => { + console.log(JSON.stringify(results, null, 2)); + } + } +} + async function main() { const files = await fs.readdir(path.join(__dirname, './src')); + output.log('Running Node.js Package Benchmark...'); + const results = []; for (const file of files) { if (file.match(/.*-benchmark\.js$/)) { const benchFile = path.join(__dirname, './src/', file); const result = await piscina.run(benchFile); - console.log('results', JSON.stringify(result, null, 2)); + results.push(result); } } + output.printResults(results); } main(); From 6567798607ea9d5a8c5c9b168646b35de54ddff8 Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Mon, 4 Mar 2024 17:45:22 -0300 Subject: [PATCH 2/3] doc: add pretty output doc --- README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/README.md b/README.md index 62dab12..899682f 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,60 @@ packages operations. - [x] pinojs - [x] winston - [x] underscore + +## Running + +To a pretty terminal output, run `index.js` + +```console +$ node index.js +Running Node.js Package Benchmark... +---------------------------------------------------------- +babel + transform (code=true ast=true): 70.09 (3 samples) + transform (code=false): 78.57 (2 samples) +dotenv + config: 31.09K (5 samples) +lodash + .chunk: 24.47M (5 samples) + .groupBy: 3.343M (7 samples) + .includes: 10.35M (6 samples) + .orderBy: 921.3K (8 samples) +moment + format (full): 504.7K (7 samples) + format: 441K (4 samples) + fromNow (YYYYMMDD): 73K (9 samples) + subtract (10): 134K (5 samples) +pinojs + info (10x): 109.1K (4 samples) +prettier + format (semi=true): 773.5K (3 samples) + format (singleQuote=true semi=true tabs=true): 593.7K (3 samples) + format (singleQuote=false semi=false tabs=false): 473.4K (3 samples) +underscore + .chunk: 3.15M (3 samples) + .groupBy: 1.003M (3 samples) + .includes: 6.588M (4 samples) + .orderBy: 488.7K (5 samples) +winston + info (10x): 24.48K (4 samples) +``` + +To store it as JSON, just pipe output to a file: + +```console +$ node index.js > results.json +$ cat result.json +[ + { + "name": "babel", + "method": "benchmarkjs", + "operations": [ + { + "name": "transform (code=true ast=true)", + "opsSec": 67.80076532539411, + "samples": 3 + }, + { +... +``` From 257439db84a7d822724f7ad85d7cfb4230e43460 Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Wed, 6 Mar 2024 17:31:28 -0300 Subject: [PATCH 3/3] src: add support to TTY=1 --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 9e5bdac..5ce3637 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,7 @@ const piscina = new Piscina({ let output; // Considering this script won't be called as a // child_process, stdout.isTTY should be reliable enough. -if (process.stdout.isTTY) { +if (process.env.TTY || process.stdout.isTTY) { output = require('./console-output'); } else { output = {