lib: complete the implementation of node:bench and cli · nodejs/node@d9f1817 · GitHub
Skip to content

Commit d9f1817

Browse files
jasnelladuh95
authored andcommitted
lib: complete the implementation of node:bench and cli
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode PR-URL: #65606 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent c39e20e commit d9f1817

35 files changed

Lines changed: 1653 additions & 28 deletions

‎doc/api/bench.md‎

Lines changed: 43 additions & 1 deletion

‎doc/api/cli.md‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,109 @@ Error: Access to this API has been restricted
450450
}
451451
```
452452

453+
### `--bench`
454+
455+
<!-- YAML
456+
added: REPLACEME
457+
-->
458+
459+
> Stability: 1 - Experimental
460+
461+
Starts the Node.js command-line benchmark runner. At least one explicit file or
462+
glob pattern is required:
463+
464+
```console
465+
node --bench benchmark.mjs
466+
node --bench 'benchmarks/**/*.js'
467+
```
468+
469+
Quote glob patterns to prevent expansion by the shell. Matching files are
470+
sorted and executed serially. By default, each file runs in a separate child
471+
process. Benchmark files declare benchmarks using `node:bench`; they must not
472+
call `run()` themselves. See the [benchmark runner][] documentation for more
473+
details.
474+
475+
This flag cannot be combined with `--test`, `--watch`, `--watch-path`,
476+
`--check`, `--eval`, or `--interactive`.
477+
478+
### `--bench-isolation=mode`
479+
480+
<!-- YAML
481+
added: REPLACEME
482+
-->
483+
484+
> Stability: 1 - Experimental
485+
486+
Configures benchmark file isolation. When `mode` is `'process'`, each matching
487+
file runs in a separate child process. This is the default. Files are still run
488+
serially so their measured work does not overlap.
489+
490+
When `mode` is `'none'`, all matching files and benchmarks run serially in the
491+
benchmark runner process. This reduces startup overhead but allows module,
492+
heap, and process state to carry between files. User writes to stdout or stderr
493+
also share destinations with benchmark reporters in this mode.
494+
495+
### `--bench-name-pattern=pattern`
496+
497+
<!-- YAML
498+
added: REPLACEME
499+
-->
500+
501+
> Stability: 1 - Experimental
502+
503+
Only runs benchmarks whose full hierarchical name matches the JavaScript
504+
regular expression `pattern`. Non-matching benchmarks are reported as skipped.
505+
506+
### `--bench-reporter-destination=destination`
507+
508+
<!-- YAML
509+
added: REPLACEME
510+
-->
511+
512+
> Stability: 1 - Experimental
513+
514+
Specifies the destination for the corresponding benchmark reporter. The value
515+
can be `stdout`, `stderr`, or a file path. A single reporter defaults to
516+
`stdout` when no destination is specified.
517+
518+
### `--bench-reporter=reporter`
519+
520+
<!-- YAML
521+
added: REPLACEME
522+
-->
523+
524+
> Stability: 1 - Experimental
525+
526+
Specifies a benchmark reporter. The built-in reporters are `spec` and `json`.
527+
The `json` reporter emits newline-delimited JSON. A custom reporter can be
528+
specified using a module specifier resolved from the current working directory.
529+
530+
This option can be repeated. When multiple reporters are specified, each must
531+
have a corresponding `--bench-reporter-destination`. The default reporter is
532+
`spec`.
533+
534+
### `--bench-samples=count`
535+
536+
<!-- YAML
537+
added: REPLACEME
538+
-->
539+
540+
> Stability: 1 - Experimental
541+
542+
Overrides the number of measured callback invocations for every selected
543+
benchmark. `count` must be an integer between `1` and `4294967295`.
544+
545+
### `--bench-warmup=count`
546+
547+
<!-- YAML
548+
added: REPLACEME
549+
-->
550+
551+
> Stability: 1 - Experimental
552+
553+
Overrides the number of unreported warmup callback invocations for every
554+
selected benchmark. `count` must be an integer between `0` and `4294967295`.
555+
453556
### `--build-sea=config`
454557

455558
<!-- YAML
@@ -3900,6 +4003,12 @@ one is included in the list below.
39004003
* `--allow-openssl-store`
39014004
* `--allow-wasi`
39024005
* `--allow-worker`
4006+
* `--bench-isolation`
4007+
* `--bench-name-pattern`
4008+
* `--bench-reporter-destination`
4009+
* `--bench-reporter`
4010+
* `--bench-samples`
4011+
* `--bench-warmup`
39034012
* `--conditions`, `-C`
39044013
* `--cpu-prof-dir`
39054014
* `--cpu-prof-interval`
@@ -4608,6 +4717,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
46084717
[`v8.startupSnapshot.setDeserializeMainFunction()`]: v8.md#v8startupsnapshotsetdeserializemainfunctioncallback-data
46094718
[`v8.startupSnapshot` API]: v8.md#startup-snapshot-api
46104719
[asynchronous module customization hooks]: module.md#asynchronous-customization-hooks
4720+
[benchmark runner]: bench.md#command-line-runner
46114721
[captured by the built-in snapshot of Node.js]: https://github.com/nodejs/node/blob/b19525a33cc84033af4addd0f80acd4dc33ce0cf/test/parallel/test-bootstrap-modules.js#L24
46124722
[collecting code coverage from tests]: test.md#collecting-code-coverage
46134723
[conditional exports]: packages.md#conditional-exports

‎doc/node.1‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,55 @@ Error: Access to this API has been restricted
281281
}
282282
.Ed
283283
.
284+
.It Fl -bench
285+
Starts the Node.js command-line benchmark runner. At least one explicit file or
286+
glob pattern is required:
287+
.Bd -literal
288+
node --bench benchmark.mjs
289+
node --bench 'benchmarks/**/*.js'
290+
.Ed
291+
Quote glob patterns to prevent expansion by the shell. Matching files are
292+
sorted and executed serially. By default, each file runs in a separate child
293+
process. Benchmark files declare benchmarks using \fBnode:bench\fR; they must not
294+
call \fBrun()\fR themselves. See the benchmark runner documentation for more
295+
details.
296+
This flag cannot be combined with \fB--test\fR, \fB--watch\fR, \fB--watch-path\fR,
297+
\fB--check\fR, \fB--eval\fR, or \fB--interactive\fR.
298+
.
299+
.It Fl -bench-isolation Ns = Ns Ar mode
300+
Configures benchmark file isolation. When \fBmode\fR is \fB'process'\fR, each matching
301+
file runs in a separate child process. This is the default. Files are still run
302+
serially so their measured work does not overlap.
303+
When \fBmode\fR is \fB'none'\fR, all matching files and benchmarks run serially in the
304+
benchmark runner process. This reduces startup overhead but allows module,
305+
heap, and process state to carry between files. User writes to stdout or stderr
306+
also share destinations with benchmark reporters in this mode.
307+
.
308+
.It Fl -bench-name-pattern Ns = Ns Ar pattern
309+
Only runs benchmarks whose full hierarchical name matches the JavaScript
310+
regular expression \fBpattern\fR. Non-matching benchmarks are reported as skipped.
311+
.
312+
.It Fl -bench-reporter-destination Ns = Ns Ar destination
313+
Specifies the destination for the corresponding benchmark reporter. The value
314+
can be \fBstdout\fR, \fBstderr\fR, or a file path. A single reporter defaults to
315+
\fBstdout\fR when no destination is specified.
316+
.
317+
.It Fl -bench-reporter Ns = Ns Ar reporter
318+
Specifies a benchmark reporter. The built-in reporters are \fBspec\fR and \fBjson\fR.
319+
The \fBjson\fR reporter emits newline-delimited JSON. A custom reporter can be
320+
specified using a module specifier resolved from the current working directory.
321+
This option can be repeated. When multiple reporters are specified, each must
322+
have a corresponding \fB--bench-reporter-destination\fR. The default reporter is
323+
\fBspec\fR.
324+
.
325+
.It Fl -bench-samples Ns = Ns Ar count
326+
Overrides the number of measured callback invocations for every selected
327+
benchmark. \fBcount\fR must be an integer between \fB1\fR and \fB4294967295\fR.
328+
.
329+
.It Fl -bench-warmup Ns = Ns Ar count
330+
Overrides the number of unreported warmup callback invocations for every
331+
selected benchmark. \fBcount\fR must be an integer between \fB0\fR and \fB4294967295\fR.
332+
.
284333
.It Fl -build-sea Ns = Ns Ar config
285334
Generates a single executable application from a JSON
286335
configuration file. The argument must be a path to the configuration file. If
@@ -1982,6 +2031,18 @@ one is included in the list below.
19822031
.It
19832032
\fB--allow-worker\fR
19842033
.It
2034+
\fB--bench-isolation\fR
2035+
.It
2036+
\fB--bench-name-pattern\fR
2037+
.It
2038+
\fB--bench-reporter-destination\fR
2039+
.It
2040+
\fB--bench-reporter\fR
2041+
.It
2042+
\fB--bench-samples\fR
2043+
.It
2044+
\fB--bench-warmup\fR
2045+
.It
19852046
\fB--conditions\fR, \fB-C\fR
19862047
.It
19872048
\fB--cpu-prof-dir\fR

‎lib/bench.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ const {
1515
} = require('internal/bench_runner/harness');
1616
const { run } = require('internal/bench_runner/runner');
1717

18-
emitExperimentalWarning('Benchmarks');
18+
if (process.env.NODE_BENCH_CONTEXT !== 'child' ||
19+
typeof process.send !== 'function') {
20+
emitExperimentalWarning('Benchmarks');
21+
}
1922

2023
module.exports = bench;
2124
ObjectAssign(module.exports, {

‎lib/internal/bench_runner/benchmarks_stream.js‎

Lines changed: 11 additions & 9 deletions

0 commit comments

Comments
 (0)