Skip to content

Commit c71d7eb

Browse files
jasnelladuh95
authored andcommitted
lib: add runFile api to node:bench
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode PR-URL: #65631 Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent 6252e4c commit c71d7eb

10 files changed

Lines changed: 920 additions & 89 deletions

File tree

doc/api/bench.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ added: REPLACEME
1111
<!-- source_link=lib/bench.js -->
1212

1313
The `node:bench` module supports defining and running JavaScript benchmarks in
14-
the current process. To access it:
14+
the current process, and running one benchmark file in a fresh child process.
15+
To access it:
1516

1617
```mjs
1718
import { bench, suite } from 'node:bench';
@@ -497,6 +498,51 @@ for await (const { type, data } of run()) {
497498
}
498499
```
499500

501+
## `runFile(path[, options])`
502+
503+
<!-- YAML
504+
added: REPLACEME
505+
-->
506+
507+
* `path` {string} The absolute path of one benchmark module.
508+
* `options` {Object}
509+
* `env` {Object} The child process environment. Property values must be
510+
strings or `undefined`. This replaces, rather than extends, the parent
511+
environment. **Default:** A snapshot of `process.env`.
512+
* `execArgv` {string\[]} Node.js command-line options for the child process.
513+
This replaces, rather than extends, inherited options. Benchmark runner
514+
options, positional arguments, and options that select another execution
515+
mode are not allowed. **Default:** Compatible options inherited from the
516+
current process.
517+
* `signal` {AbortSignal} Terminates the child process when aborted.
518+
* Returns: {BenchmarksStream}
519+
520+
Runs exactly one benchmark module in a fresh child process and returns its
521+
object-mode event stream. `path` is not interpreted as a glob. Unless the signal
522+
is aborted or the stream is destroyed before startup, every call uses a new
523+
child. Input discovery, ordering, concurrency, retries, and multi-file
524+
scheduling remain the caller's responsibility.
525+
526+
Records use advanced child process serialization, preserving supported
527+
structured values such as `bigint` and errors. Child writes to stdout and stderr
528+
become `'bench:diagnostic'` records. A module loading error, abnormal child exit,
529+
or cancellation also emits an error diagnostic and produces a terminal
530+
`'bench:summary'` whose `success` property is `false`; these execution failures
531+
do not error the stream. If module evaluation fails after declaring benchmarks,
532+
those declarations still run before the unsuccessful summary.
533+
534+
`env`, effective inherited options, and an explicitly provided `execArgv` are
535+
copied when `runFile()` is called. The runner removes `NODE_OPTIONS`, replaces
536+
IPC-related environment variables, and sets its private child-context, run
537+
identity, and file identity variables, overriding properties with those names
538+
in `env`. Pass child Node.js options through `execArgv`, not `NODE_OPTIONS`.
539+
Standard `child_process` environment propagation still applies, including
540+
`NODE_V8_COVERAGE`, permission-model options, and required z/OS variables.
541+
Aborting `signal` before the child starts produces an `AbortError` diagnostic
542+
without spawning it. Aborting during execution sends `SIGTERM` to the child and
543+
escalates to `SIGKILL` if it does not exit. Destroying the returned stream
544+
follows the same termination procedure.
545+
500546
## Class: `BenchContext`
501547

502548
An instance of `BenchContext` is passed to every benchmark invocation. A new

lib/bench.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616
const {
1717
createRunner,
1818
run,
19+
runFile,
1920
} = require('internal/bench_runner/runner');
2021

2122
if (process.env.NODE_BENCH_CONTEXT !== 'child' ||
@@ -33,5 +34,6 @@ ObjectAssign(module.exports, {
3334
createRunner,
3435
describe: suite,
3536
run,
37+
runFile,
3638
suite,
3739
});

0 commit comments

Comments
 (0)