Skip to content

Commit c3d2725

Browse files
committed
lib: default node:bench warmup to 10 invocations
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com>
1 parent bbd566d commit c3d2725

21 files changed

Lines changed: 86 additions & 26 deletions

‎doc/api/bench.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ once to provide an externally measured sample. Setup before `start()` and
7474
cleanup after `end()` are outside the measured region. Promise-returning
7575
functions are awaited.
7676

77+
By default, ten unreported warmup invocations run before the measured samples
78+
so that early samples are less likely to be taken before the measured code has
79+
been optimized. A fixed warmup count does not guarantee a stable optimization
80+
state; inspect raw `samples` and increase `warmup` when early samples are
81+
consistently slower. Set `warmup` to `0` to measure from the first invocation.
82+
7783
By default, an event loop turn occurs between sample invocations. An embedded
7884
runner can disable this using `yieldBetweenSamples`. The runner executes
7985
benchmarks serially, but it does not provide process isolation. Other work in
@@ -336,7 +342,7 @@ added: v26.9.0
336342
* `timeout` {number} The number of milliseconds after which the benchmark
337343
fails. **Default:** `Infinity`.
338344
* `warmup` {number} The number of unreported callback invocations before
339-
measured samples. Must be a 32-bit unsigned integer. **Default:** `0`.
345+
measured samples. Must be a 32-bit unsigned integer. **Default:** `10`.
340346
* `fn` {Function|AsyncFunction} The benchmark function. It receives a
341347
{BenchContext}.
342348
* Returns: {Promise} Fulfilled with the benchmark result after a top-level

‎doc/api/cli.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,8 @@ added: v26.9.0
630630
631631
Overrides the number of unreported warmup callback invocations for every
632632
selected benchmark. `count` must be an integer between `0` and `4294967295`.
633+
Without this option, each benchmark uses its own `warmup` value, which defaults
634+
to `10`.
633635

634636
### `--build-sea=config`
635637

‎doc/node.1‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ selected benchmark. A benchmark may finish earlier by calling
384384
.It Fl -bench-warmup Ns = Ns Ar count
385385
Overrides the number of unreported warmup callback invocations for every
386386
selected benchmark. \fBcount\fR must be an integer between \fB0\fR and \fB4294967295\fR.
387+
Without this option, each benchmark uses its own \fBwarmup\fR value, which defaults
388+
to \fB10\fR.
387389
.
388390
.It Fl -build-sea Ns = Ns Ar config
389391
Generates a single executable application from a JSON

‎lib/internal/bench_runner/benchmark.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const { structuredClone } = require('internal/worker/js_transferable');
4747

4848
const { bigint: hrtime } = process.hrtime;
4949
const kDefaultSamples = 30;
50-
const kDefaultWarmup = 0;
50+
const kDefaultWarmup = 10;
5151
const kEmptyDiagnosticChannels = ObjectFreeze([]);
5252
const kEmptyNamePath = ObjectFreeze([]);
5353
const kEmptyParams = ObjectFreeze({ __proto__: null });

‎test/fixtures/bench-runner/diagnostic.cjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const diagnosticChannel = channel(channelName);
99
bench('diagnostic relay', {
1010
diagnosticChannels: [channelName],
1111
samples: 1,
12+
warmup: 0,
1213
}, (b) => {
1314
const message = { value: 42n };
1415
diagnosticChannel.publish(message);

‎test/fixtures/bench-runner/many-records.cjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { bench } = require('node:bench');
44

5-
bench('many records', { samples: 30 }, (b) => {
5+
bench('many records', { samples: 30, warmup: 0 }, (b) => {
66
process.stdout.write(`${b.index}\n`);
77
b.record({ duration_ns: 1n, operations: 1 });
88
});

‎test/fixtures/bench-runner/recorded-detail.cjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { bench } = require('node:bench');
44

5-
bench('recorded detail', { samples: 3 }, (b) => {
5+
bench('recorded detail', { samples: 3, warmup: 0 }, (b) => {
66
b.record({
77
__proto__: null,
88
detail: {

‎test/fixtures/bench-runner/serial.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = function register(name) {
99
fs.writeFileSync(lock, `${process.pid}`, { flag: 'wx' });
1010
process.on('exit', () => fs.unlinkSync(lock));
1111

12-
bench(name, { samples: 1 }, async (b) => {
12+
bench(name, { samples: 1, warmup: 0 }, async (b) => {
1313
b.start();
1414
await setTimeout(25);
1515
b.end(1);

‎test/fixtures/bench-runner/utf8-output.cjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { bench } = require('node:bench');
66
const output = Buffer.from('split:\u20ac\n');
77
process.stdout.write(output.subarray(0, 7));
88

9-
bench('UTF-8 output', { samples: 1 }, async (b) => {
9+
bench('UTF-8 output', { samples: 1, warmup: 0 }, async (b) => {
1010
b.start();
1111
await setTimeout(20);
1212
process.stdout.write(output.subarray(7));

‎test/parallel/test-bench-auto-run.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ const child = spawnSync(process.execPath, [
1414
]);
1515
assert.strictEqual(child.status, 1);
1616

17-
const completion = bench('automatic execution', common.mustCall((b) => {
17+
const completion = bench('automatic execution', {
18+
warmup: 0,
19+
}, common.mustCall((b) => {
1820
b.record({ duration_ns: 1n, operations: 1 });
1921
}, 30));
2022

0 commit comments

Comments
 (0)