命令行运行器
🌐 Command-line runner
--bench 标志用于运行一个或多个指定的基准测试文件或通配符模式:
🌐 The --bench flag runs one or more explicit benchmark files or glob patterns:
node --experimental-bench --bench benchmark.mjs
node --experimental-bench --bench --bench-reporter=json 'benchmarks/**/*.js' 文件会被排序并按顺序执行。默认的 --bench-isolation=process 模式会在每个子进程中运行每个文件,并生成一个汇总摘要。结构化事件会直接传递给父进程,不会进行 JSON 转换,可以保留 BigInt 类型的持续时间、错误和参数值。子进程写入的 stdout 和 stderr 会作为诊断记录输出,因此不会破坏报告器的输出。
🌐 Files are sorted and executed serially. The default
--bench-isolation=process mode runs each file in a separate child process and
emits one aggregate summary. Structured events are transferred to the parent
without JSON conversion, preserving BigInt durations, errors, and parameter
values. Child writes to stdout and stderr are emitted as diagnostic records so
they do not corrupt reporter output.
--bench-isolation=none 会把所有文件导入到运行器进程中。这种模式启动开销较低,但模块、堆和进程状态会在文件间共享,而且用户的输出和错误会与报告器共用 stdout 和 stderr。
工作线程隔离不是命令行模式。每个新创建的 <Worker> 都有一个独立的 V8 隔离区、JavaScript 堆和事件循环,通常启动开销比子进程低。重用工作线程可以保留它的模块和堆状态。工作线程还共享 libuv 的全进程线程池,并且可以共享进程全局的本地或附加状态,所以它们并不能提供像进程隔离那样的边界。
🌐 Worker-thread isolation is not a CLI mode. Each newly constructed <Worker> has a separate V8 isolate, JavaScript heap, and event loop, typically with lower startup cost than a child process. Reusing a worker preserves its module and heap state. Workers also share libuv's process-wide thread pool and can share process-global native or addon state, so they do not provide the same boundary as process isolation.
更高级的工具可以通过在 worker 内加载基准代码来实验 worker 隔离,在那里进行测量,传输结构化的样本数据,并将其传给 context.record()。报告的 duration_ns 可以在 worker 捕获两个时间戳时排除消息传输。工具应该明确识别 worker 模块和工作负载。它们不应该将任意函数或闭包转换成字符串来在隔离环境间移动,因为闭包无法与其原始词法环境重建。
🌐 Higher-level tools can experiment with worker isolation by loading benchmark
code inside a worker, measuring there, transferring structured sample data, and
passing it to context.record(). The reported duration_ns can exclude
message transport when the worker captures both timestamps. Tools should
identify worker modules and workloads explicitly. They should not stringify
arbitrary functions or closures to move them between isolates, because closures
cannot be reconstructed with their original lexical environment.
传递给 --bench 的基准文件应该声明基准,但不得调用 run()。CLI 支持 --bench-name-pattern、--bench-samples、--bench-warmup、--bench-reporter 和 --bench-reporter-destination。详情请参见 命令行选项文档。
🌐 Benchmark files passed to --bench should declare benchmarks but must not call
run(). The CLI supports --bench-name-pattern, --bench-samples,
--bench-warmup, --bench-reporter, and --bench-reporter-destination. See
the command-line options documentation for details.
通过 --require 或 --import 传递的预加载模块不应该声明基准。这样的声明不会与入口文件关联,而且它们的 entryFile 值是 null。它们的 fileRunId 用来标识发生这些操作的运行器或子执行进程。在进程隔离的情况下,预加载模块会被评估,并且它的声明会在每个基准子进程中运行一次。
🌐 Preload modules passed through --require or --import should not declare
benchmarks. Such declarations are not associated with an entry file and have
an entryFile value of null. Their fileRunId identifies the runner or child
execution in which they occurred. With process isolation, a preload is evaluated
and its declarations run once for every benchmark child process.