基准报告器
🌐 Benchmark reporters
内置报告器可以从仅限 Scheme 的 node:bench/reporters 模块中获取:
🌐 The built-in reporters are available from the scheme-only
node:bench/reporters module:
import { json, spec } from 'node:bench/reporters';const { json, spec } = require('node:bench/reporters');Reporter 的值可以直接传递给 stream.compose():
🌐 Reporter values can be passed directly to stream.compose():
import { bench, run } from 'node:bench';
import { spec } from 'node:bench/reporters';
import process from 'node:process';
bench('example', (b) => {
b.start();
doWork();
b.end(1);
});
run().compose(spec).pipe(process.stdout); spec 报告器会缓冲结果,并输出一个简洁的表格,包含样本数量、平均速率、平均值的 95% 置信区间、中位速率以及警告。修改系数超过 5% 会显示为 noisy,绝对偏度超过 1 会显示为 skewed。具体的可读格式可能会有所变化。
🌐 The spec reporter buffers results and outputs a concise table containing the
sample count, mean rate, 95% confidence interval for the mean, median rate, and
warnings. A coefficient of variation above 5% is reported as noisy, and an
absolute skewness above 1 is reported as skewed. The exact human-readable
format is subject to change.
json 报告器会将每个生命周期记录作为换行分隔的 JSON 输出。BigInt 值,包括 duration_ns,会被编码为十进制字符串。错误会使用它们的 name、message、stack、code、cause 和 errors 属性表示。根据 JSON 的要求,非有限数字会被编码为 null。
🌐 The json reporter emits every lifecycle record as newline-delimited JSON.
BigInt values, including duration_ns, are encoded as decimal strings. Errors
are represented using their name, message, stack, code, cause, and
errors properties. As required by JSON, non-finite numbers are encoded as
null.
自定义报告器使用相同的组合契约。它们可以是 stream.compose() 接受的转换或函数。组合后的可读流可以传输到任意可写的目标:
🌐 Custom reporters use the same composition contract. They can be transforms or
functions accepted by stream.compose(). The composed readable can be piped to
any writable destination:
import { run } from 'node:bench';
import process from 'node:process';
async function* names(source) {
for await (const { type, data } of source) {
if (type === 'bench:complete') {
yield `${data.name}\n`;
}
}
}
run().compose(names).pipe(process.stdout);