mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
test: make node:bench test samples survive a coarse clock
Most node:bench tests record a sample with `b.start();
process.hrtime.bigint(); b.end(1)`, counting on the extra clock read to
make end() see a later timestamp than start(). On a debian12-x64 CI
host the monotonic clock is coarse enough that all three reads returned
the same value, so end() threw ERR_INVALID_STATE ("insufficient clock
precision for benchmark sample") and test-bench-harness-errors failed;
every test using the idiom can fail the same way there.
Add test/common/bench.js with completeSample(), which spins until
process.hrtime.bigint() moves between start() and end(), and use it at
all of those sites, bench-runner fixtures included.
Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65780
Fixes: https://github.com/nodejs/node/issues/64226
Refs: https://github.com/nodejs/node/issues/59146
Refs: https://github.com/nodejs/node/issues/54534
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
This commit is contained in:
1 parent
e0fa1893c3
commit
31f4bd73b2
21 files changed
+82
-102
No files matched your search
@@ -28,6 +28,7 @@ several other tasks:
|
||||
## Table of contents
|
||||
|
||||
* [ArrayStream module](#arraystream-module)
|
||||
* [Bench module](#bench-module)
|
||||
* [Benchmark module](#benchmark-module)
|
||||
* [Child process module](#child-process-module)
|
||||
* [Common module API](#common-module-api)
|
||||
@@ -49,6 +50,21 @@ several other tasks:
|
||||
* [UDP pair helper](#udp-pair-helper)
|
||||
* [WPT module](#wpt-module)
|
||||
|
||||
## Bench module
|
||||
|
||||
The `bench` module has helpers for tests of `node:bench`.
|
||||
|
||||
### `completeSample(b[, operations[, options]])`
|
||||
|
||||
* `b` The `BenchContext` passed to a `node:bench` benchmark function.
|
||||
* `operations` [\<number>][<number>] Passed to `b.end()`. **Default:** `1`.
|
||||
* `options` [\<Object>][<Object>] Passed to `b.end()`.
|
||||
* return the sample returned by `b.end()`.
|
||||
|
||||
Calls `b.start()` and `b.end()` with at least one `process.hrtime.bigint()`
|
||||
tick in between, so the sample has a non-zero duration on hosts whose
|
||||
monotonic clock is coarse.
|
||||
|
||||
## Benchmark module
|
||||
|
||||
The `benchmark` module is used by tests to run benchmarks.
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
// Records a sample with start()/end() and at least one hrtime tick in
|
||||
// between, so its duration is non-zero even on hosts with a coarse clock.
|
||||
function completeSample(b, operations = 1, options = undefined) {
|
||||
b.start();
|
||||
const started = process.hrtime.bigint();
|
||||
let now = started;
|
||||
while (now === started) now = process.hrtime.bigint();
|
||||
return b.end(operations, options);
|
||||
}
|
||||
|
||||
module.exports = { completeSample };
|
||||
+2
-3
@@ -1,11 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const { completeSample } = require('../../common/bench');
|
||||
const { bench } = require('node:bench');
|
||||
|
||||
module.exports = function declareChildA() {
|
||||
bench('child a', { samples: 1 }, (b) => {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
});
|
||||
};
|
||||
+2
-3
@@ -1,11 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const { completeSample } = require('../../common/bench');
|
||||
const { bench } = require('node:bench');
|
||||
|
||||
module.exports = function declareChildB() {
|
||||
bench('child b', { samples: 1 }, (b) => {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
});
|
||||
};
|
||||
+2
-3
@@ -1,9 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
const { completeSample } = require('../../common/bench');
|
||||
const { bench } = require('node:bench');
|
||||
|
||||
bench('preload identity', { samples: 1 }, (b) => {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
});
|
||||
+2
-3
@@ -1,11 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const { completeSample } = require('../../common/bench');
|
||||
const { bench } = require('node:bench');
|
||||
|
||||
module.exports = function registerSharedIdentity() {
|
||||
bench('shared identity', { samples: 1 }, (b) => {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
});
|
||||
};
|
||||
@@ -1,11 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const { completeSample } = require('../../common/bench');
|
||||
const { bench } = require('node:bench');
|
||||
|
||||
bench('declared before load error', { samples: 1 }, (b) => {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
});
|
||||
|
||||
throw new Error('load failed after declaration');
|
||||
Vendored
+2
-7
@@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const { completeSample } = require('../../common/bench');
|
||||
const { bench } = require('node:bench');
|
||||
|
||||
if (process.env.NODE_BENCH_PID_LOG !== undefined) {
|
||||
@@ -10,11 +11,5 @@ if (process.env.NODE_BENCH_PID_LOG !== undefined) {
|
||||
for (const size of [1, 2]) {
|
||||
bench('tools/simple.js', {
|
||||
params: { method: 'loop', size },
|
||||
}, (b) => {
|
||||
let value = 0;
|
||||
b.start();
|
||||
for (let i = 0; i < 1_000; i++) value += size;
|
||||
b.end(1_000);
|
||||
if (value === 0) throw new Error('unreachable');
|
||||
});
|
||||
}, (b) => completeSample(b, 1_000));
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const { completeSample } = require('../common/bench');
|
||||
const assert = require('assert');
|
||||
const { createRunner } = require('node:bench');
|
||||
|
||||
@@ -16,9 +17,7 @@ const { createRunner } = require('node:bench');
|
||||
}, common.mustCall((b) => {
|
||||
invocations.push(`${b.phase}:${b.index}`);
|
||||
const detail = { index: b.index, phase: b.phase };
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
const sample = b.end(2, { detail });
|
||||
const sample = completeSample(b, 2, { detail });
|
||||
detail.index = -1;
|
||||
|
||||
assert.strictEqual(sample.operations, 2);
|
||||
|
||||
@@ -2,15 +2,14 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const { completeSample } = require('../common/bench');
|
||||
const assert = require('assert');
|
||||
const { createRunner } = require('node:bench');
|
||||
|
||||
const runner = createRunner({ yieldBetweenSamples: false });
|
||||
|
||||
runner.bench('done during warmup', { samples: 1, warmup: 1 }, (b) => {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
b.done();
|
||||
});
|
||||
runner.bench('invalid record', { samples: 1 }, (b) => b.record(null));
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const { completeSample } = require('../common/bench');
|
||||
const assert = require('assert');
|
||||
const { createRunner } = require('node:bench');
|
||||
const { setImmediate } = require('timers/promises');
|
||||
@@ -18,16 +19,12 @@ const { setImmediate } = require('timers/promises');
|
||||
const firstCompletion = first.bench(
|
||||
'same name', { samples: 2 }, common.mustCall((b) => {
|
||||
firstCalls++;
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
}, 2));
|
||||
const secondCompletion = second.bench(
|
||||
'same name', { samples: 1 }, common.mustCall((b) => {
|
||||
secondCalls++;
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
}));
|
||||
|
||||
await setImmediate();
|
||||
@@ -74,9 +71,7 @@ const { setImmediate } = require('timers/promises');
|
||||
|
||||
const retry = createRunner({ yieldBetweenSamples: false });
|
||||
retry.bench('not filtered', { samples: 1 }, (b) => {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
});
|
||||
assert.throws(() => retry.run({
|
||||
namePattern: 'filtered',
|
||||
@@ -90,9 +85,7 @@ const { setImmediate } = require('timers/promises');
|
||||
|
||||
const reentrant = createRunner({ yieldBetweenSamples: false });
|
||||
reentrant.bench('reentrant options', { samples: 1 }, (b) => {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
});
|
||||
const reentrantOptions = {};
|
||||
Object.defineProperty(reentrantOptions, 'samples', {
|
||||
|
||||
@@ -2,15 +2,14 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const { completeSample } = require('../common/bench');
|
||||
const assert = require('assert');
|
||||
const { Writable } = require('stream');
|
||||
const { finished } = require('stream/promises');
|
||||
const { bench, run } = require('node:bench');
|
||||
|
||||
bench('completed', { samples: 1 }, (b) => {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
});
|
||||
bench.skip('skipped', { samples: 1 }, common.mustNotCall());
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const { completeSample } = require('../common/bench');
|
||||
const assert = require('assert');
|
||||
const { bench, run, suite } = require('node:bench');
|
||||
const { setTimeout } = require('timers/promises');
|
||||
@@ -40,21 +41,15 @@ bench('late timeout', { samples: 1, timeout: 5 }, async (b) => {
|
||||
});
|
||||
bench('after late timeout', options, common.mustCall((b) => {
|
||||
assert.strictEqual(lateTimeoutActive, false);
|
||||
complete(b);
|
||||
completeSample(b);
|
||||
}));
|
||||
|
||||
const signal = AbortSignal.abort(new Error('stop'));
|
||||
bench('aborted', { samples: 1, signal }, () => {});
|
||||
|
||||
function complete(b) {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
}
|
||||
|
||||
bench('duplicate', { samples: 1, params: { value: 1 } }, complete);
|
||||
bench('duplicate', { samples: 1, params: { value: 1 } }, complete);
|
||||
bench('continues', options, complete);
|
||||
bench('duplicate', { samples: 1, params: { value: 1 } }, completeSample);
|
||||
bench('duplicate', { samples: 1, params: { value: 1 } }, completeSample);
|
||||
bench('continues', options, completeSample);
|
||||
bench('timeout', { samples: 1, timeout: 10 }, async () => {
|
||||
await new Promise(() => {});
|
||||
});
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const { completeSample } = require('../common/bench');
|
||||
const assert = require('assert');
|
||||
const { bench, run, suite } = require('node:bench');
|
||||
|
||||
@@ -10,9 +11,7 @@ const calls = [];
|
||||
function complete(name) {
|
||||
return (b) => {
|
||||
calls.push(name);
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -2,16 +2,11 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const { completeSample } = require('../common/bench');
|
||||
const assert = require('assert');
|
||||
const { createRunner } = require('node:bench');
|
||||
const { setImmediate } = require('timers/promises');
|
||||
|
||||
function complete(b) {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
}
|
||||
|
||||
async function testSynchronousSuiteFailure() {
|
||||
const runner = createRunner({ yieldBetweenSamples: false });
|
||||
const completion = runner.suite('outer', () => {
|
||||
@@ -43,7 +38,7 @@ async function testRunSignal() {
|
||||
samples: 3,
|
||||
}, (b) => {
|
||||
invocations++;
|
||||
complete(b);
|
||||
completeSample(b);
|
||||
if (b.index === 0) {
|
||||
abortPromise = setImmediate().then(() => {
|
||||
controller.abort(new Error('run aborted'));
|
||||
@@ -65,7 +60,7 @@ async function testRunSignalAfterSample() {
|
||||
const reason = new Error('sample aborted');
|
||||
const completion = runner.bench('aborted after sample', {
|
||||
samples: 2,
|
||||
}, complete);
|
||||
}, completeSample);
|
||||
const stream = runner.run({ signal: controller.signal });
|
||||
stream.once('bench:sample', common.mustCall(() => {
|
||||
controller.abort(reason);
|
||||
@@ -80,7 +75,7 @@ async function testRunSignalAfterSample() {
|
||||
|
||||
async function testStringNamePattern() {
|
||||
const runner = createRunner({ yieldBetweenSamples: false });
|
||||
runner.bench('included', { samples: 1 }, complete);
|
||||
runner.bench('included', { samples: 1 }, completeSample);
|
||||
runner.bench('excluded', { samples: 1 }, common.mustNotCall());
|
||||
const records = await runner.run({ namePattern: 'included' }).toArray();
|
||||
const excluded = records.find(
|
||||
@@ -97,7 +92,7 @@ async function testStringNamePattern() {
|
||||
async function testTopLevelRecovery() {
|
||||
const runner = createRunner({ yieldBetweenSamples: false });
|
||||
const suiteCompletion = runner.suite('nested', () => {
|
||||
runner.bench('listener failure', { samples: 1 }, complete);
|
||||
runner.bench('listener failure', { samples: 1 }, completeSample);
|
||||
});
|
||||
const stream = runner.run();
|
||||
const failure = new Error();
|
||||
@@ -123,7 +118,7 @@ async function testRepeatedReportingFailure() {
|
||||
const summary = new Error('summary listener failed');
|
||||
const completion = runner.bench('reporting failures', {
|
||||
samples: 1,
|
||||
}, complete);
|
||||
}, completeSample);
|
||||
const stream = runner.run();
|
||||
stream.on('bench:start', common.mustCall(() => { throw original; }));
|
||||
stream.on('bench:diagnostic', common.mustCall(() => {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const { completeSample } = require('../common/bench');
|
||||
const assert = require('assert');
|
||||
const { setImmediate } = require('timers/promises');
|
||||
const {
|
||||
@@ -14,12 +15,6 @@ const {
|
||||
suite,
|
||||
} = require('node:bench');
|
||||
|
||||
function complete(b) {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
}
|
||||
|
||||
suite('before failure', () => {
|
||||
before(() => { throw new Error('before failure'); });
|
||||
after(common.mustCall());
|
||||
@@ -34,7 +29,7 @@ suite('beforeEach failure', () => {
|
||||
|
||||
suite('after failure', () => {
|
||||
after(() => { throw new Error('after failure'); });
|
||||
bench('completes before after', { samples: 1 }, complete);
|
||||
bench('completes before after', { samples: 1 }, completeSample);
|
||||
});
|
||||
|
||||
suite('build failure', async () => {
|
||||
@@ -42,7 +37,7 @@ suite('build failure', async () => {
|
||||
throw new Error('build failure');
|
||||
});
|
||||
|
||||
bench('continues after suite failures', { samples: 1 }, complete);
|
||||
bench('continues after suite failures', { samples: 1 }, completeSample);
|
||||
|
||||
const completions = [];
|
||||
const diagnostics = [];
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const { completeSample } = require('../common/bench');
|
||||
const assert = require('assert');
|
||||
const { Readable } = require('stream');
|
||||
const { bench, run } = require('node:bench');
|
||||
@@ -11,9 +12,7 @@ bench('json completed', {
|
||||
params: { size: 'small' },
|
||||
samples: 1,
|
||||
}, (b) => {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
});
|
||||
|
||||
bench('json failed', { samples: 1 }, () => {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const { completeSample } = require('../common/bench');
|
||||
const assert = require('assert');
|
||||
const { bench, run } = require('node:bench');
|
||||
|
||||
@@ -9,9 +10,7 @@ let invocations = 0;
|
||||
const timeout = common.platformTimeout(1000);
|
||||
bench('overridden', { samples: 8, timeout, warmup: 8 }, (b) => {
|
||||
invocations++;
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(invocations);
|
||||
completeSample(b, invocations);
|
||||
});
|
||||
|
||||
const plans = [];
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const { completeSample } = require('../common/bench');
|
||||
const assert = require('assert');
|
||||
const { setImmediate } = require('timers/promises');
|
||||
const {
|
||||
@@ -42,9 +43,7 @@ const suiteCompletion = suite('group', { tags: ['Group'] }, async () => {
|
||||
contexts.add(b);
|
||||
calls.push('sync sample');
|
||||
assert.deepStrictEqual(b.params, { __proto__: null, a: true, z: 2 });
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
active = false;
|
||||
}, 3));
|
||||
|
||||
@@ -54,9 +53,7 @@ const suiteCompletion = suite('group', { tags: ['Group'] }, async () => {
|
||||
contexts.add(b);
|
||||
calls.push('async sample');
|
||||
await setImmediate();
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
await setImmediate();
|
||||
active = false;
|
||||
}, 2));
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const { completeSample } = require('../common/bench');
|
||||
const assert = require('assert');
|
||||
const { bench, createRunner, run } = require('node:bench');
|
||||
|
||||
@@ -11,17 +12,13 @@ let objectOverloadCalls = 0;
|
||||
|
||||
function functionOverload(b) {
|
||||
functionOverloadCalls++;
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
b.done();
|
||||
}
|
||||
|
||||
function objectOverload(b) {
|
||||
objectOverloadCalls++;
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
}
|
||||
|
||||
assert.throws(() => bench('', noop), { code: 'ERR_INVALID_ARG_VALUE' });
|
||||
@@ -69,9 +66,7 @@ bench(functionOverload);
|
||||
bench({ samples: 1 }, objectOverload);
|
||||
|
||||
bench('valid', { samples: 1 }, (b) => {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
});
|
||||
|
||||
const stream = run();
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const { completeSample } = require('../common/bench');
|
||||
const assert = require('assert');
|
||||
const { createRunner } = require('node:bench');
|
||||
const { setImmediate } = require('timers/promises');
|
||||
@@ -16,9 +17,7 @@ async function observe(factoryOptions, runOptions) {
|
||||
|
||||
runner.bench('yielding', { samples: 2 }, (b) => {
|
||||
observed.push(turnOccurred);
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
});
|
||||
|
||||
await runner.run(runOptions).toArray();
|
||||
@@ -55,9 +54,7 @@ async function observeAfterEachTimeout() {
|
||||
samples: 1,
|
||||
timeout: 5,
|
||||
}, (b) => {
|
||||
b.start();
|
||||
process.hrtime.bigint();
|
||||
b.end(1);
|
||||
completeSample(b);
|
||||
});
|
||||
await runner.run().toArray();
|
||||
const result = await completion;
|
||||
|
||||
Reference in new issue
Block a user