benchmark: add --track to benchmark

This option will measure how long the benchmark
takes to run. Example:

```
$ ./node benchmark/run.js --track assert

assert/assertion-error.js
assert/assertion-error.js size=2 n=200: 22,987.24012781365
assert/assertion-error.js size=75 n=200: 296.2362823364434
[740ms] assert/assertion-error.js

...
```

Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com>
PR-URL: https://github.com/nodejs/node/pull/59174
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
This commit is contained in:
Rafael Gonzaga 2025-07-25 10:22:26 -03:00 committed by GitHub
parent d5b815c9ea
commit 5f3aa57210
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,6 +3,7 @@
const path = require('path'); const path = require('path');
const { spawn, fork } = require('node:child_process'); const { spawn, fork } = require('node:child_process');
const CLI = require('./_cli.js'); const CLI = require('./_cli.js');
const { styleText } = require('node:util');
const cli = new CLI(`usage: ./node run.js [options] [--] <category> ... const cli = new CLI(`usage: ./node run.js [options] [--] <category> ...
Run each benchmark in the <category> directory a single time, more than one Run each benchmark in the <category> directory a single time, more than one
@ -16,6 +17,7 @@ const cli = new CLI(`usage: ./node run.js [options] [--] <category> ...
Default: 1 Default: 1
--set variable=value set benchmark variable (can be repeated) --set variable=value set benchmark variable (can be repeated)
--format [simple|csv] optional value that specifies the output format --format [simple|csv] optional value that specifies the output format
--track Display the time elapsed to run each benchmark file.
test only run a single configuration from the options test only run a single configuration from the options
matrix matrix
all each benchmark category is run one after the other all each benchmark category is run one after the other
@ -25,7 +27,7 @@ const cli = new CLI(`usage: ./node run.js [options] [--] <category> ...
--set CPUSET=0-2 Specifies that benchmarks should run on CPU cores 0 to 2. --set CPUSET=0-2 Specifies that benchmarks should run on CPU cores 0 to 2.
Note: The CPUSET format should match the specifications of the 'taskset' command on your system. Note: The CPUSET format should match the specifications of the 'taskset' command on your system.
`, { arrayArgs: ['set', 'filter', 'exclude'] }); `, { arrayArgs: ['set', 'filter', 'exclude'], boolArgs: ['track'] });
const benchmarks = cli.benchmarks(); const benchmarks = cli.benchmarks();
@ -107,7 +109,12 @@ async function run() {
} }
while (runs-- > 0) { while (runs-- > 0) {
const start = performance.now();
await runBenchmark(filename); await runBenchmark(filename);
if (format !== 'csv' && cli.optional.track) {
const ms = styleText(['bold', 'yellow'], `${Math.round(performance.now() - start)}ms`);
console.log(`[${ms}] ${filename}`);
}
} }
} }
} }