mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
lib,benchmark: address multiple review issues
Refs: https://github.com/nodejs/node/pull/65606#discussion_r3887305271 Refs: https://github.com/nodejs/node/pull/65606#discussion_r3887305272 Refs: https://github.com/nodejs/node/pull/65606#discussion_r3887305275 PR-URL: https://github.com/nodejs/node/pull/65631 Reviewed-By: Filip Skokan <panva.ip@gmail.com>
This commit is contained in:
1 parent
aae83d87a4
commit
c07b2ab8a9
8 files changed
+164
-30
No files matched your search
+3
-3
@@ -80,9 +80,9 @@ Rscript benchmark/compare.R < compare-node-bench.csv
|
||||
|
||||
Pass `--analyze` to run the same Welch analysis inline. `--max-regression N`
|
||||
implies `--analyze` and makes the command fail only when the Holm-Bonferroni
|
||||
adjusted p-value is below 0.05 and the full 95% confidence interval is worse
|
||||
than `-N%`. Requiring both conditions prevents a noisy point estimate from
|
||||
failing a regression gate.
|
||||
adjusted one-sided p-value against the `N%` threshold is below 0.05 and the full
|
||||
95% confidence interval is worse than `-N%`. Requiring both conditions prevents
|
||||
a noisy point estimate from failing a regression gate.
|
||||
|
||||
```console
|
||||
./node benchmark/compare-node-bench.js \
|
||||
|
||||
@@ -31,8 +31,19 @@ function holmAdjust(pValues) {
|
||||
return adjusted;
|
||||
}
|
||||
|
||||
function thresholdPValue(oldRates, newHistogram, scale, maxRegression) {
|
||||
const factor = 1 - maxRegression / 100;
|
||||
if (factor <= 0) return 1;
|
||||
const thresholdHistogram = createRateHistogram(
|
||||
oldRates.map((rate) => rate * factor), scale, 3);
|
||||
const result = thresholdHistogram.welchTest(newHistogram);
|
||||
if (Number.isNaN(result.pValue)) return 1;
|
||||
return result.tStatistic > 0 ?
|
||||
result.pValue / 2 : 1 - result.pValue / 2;
|
||||
}
|
||||
|
||||
function isRegressionFailure(row, maxRegression) {
|
||||
return row.pAdjusted < 0.05 &&
|
||||
return row.pThresholdAdjusted < 0.05 &&
|
||||
row.improvement + row.ci95 < -maxRegression;
|
||||
}
|
||||
|
||||
@@ -80,7 +91,7 @@ function analyzeCompare(samples, scale, maxRegression) {
|
||||
result.confidenceInterval.lower) / 2;
|
||||
return (half / (oldMean * scale)) * 100;
|
||||
};
|
||||
rows.push({
|
||||
const row = {
|
||||
ci95: ciPercent(w95),
|
||||
ci99: ciPercent(w99),
|
||||
ci999: ciPercent(w999),
|
||||
@@ -88,14 +99,24 @@ function analyzeCompare(samples, scale, maxRegression) {
|
||||
name,
|
||||
pValue: Number.isNaN(w95.pValue) ? 1 : w95.pValue,
|
||||
stars,
|
||||
});
|
||||
};
|
||||
if (maxRegression !== undefined) {
|
||||
row.pThreshold = thresholdPValue(
|
||||
oldRates, newHistogram, scale, maxRegression);
|
||||
}
|
||||
rows.push(row);
|
||||
}
|
||||
|
||||
const adjusted = holmAdjust(rows.map(({ pValue }) => pValue));
|
||||
const thresholdAdjusted = maxRegression === undefined ? null :
|
||||
holmAdjust(rows.map(({ pThreshold }) => pThreshold));
|
||||
let underpowered = 0;
|
||||
for (let index = 0; index < rows.length; index++) {
|
||||
const row = rows[index];
|
||||
row.pAdjusted = adjusted[index];
|
||||
if (thresholdAdjusted !== null) {
|
||||
row.pThresholdAdjusted = thresholdAdjusted[index];
|
||||
}
|
||||
row.inconclusive = maxRegression > 0 &&
|
||||
row.stars.trim() === '' &&
|
||||
row.ci95 > maxRegression;
|
||||
@@ -146,8 +167,13 @@ function analyzeCompare(samples, scale, maxRegression) {
|
||||
`After Holm-Bonferroni correction across ${rows.length} comparison` +
|
||||
`${rows.length === 1 ? '' : 's'}, ${significant} remain` +
|
||||
`${significant === 1 ? 's' : ''} significant at 5%.`,
|
||||
'--max-regression uses the corrected values.',
|
||||
);
|
||||
if (maxRegression !== undefined) {
|
||||
output.push(
|
||||
`For --max-regression, one-sided p-values against the ` +
|
||||
`${maxRegression}% threshold were corrected separately.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (maxRegression > 0 && underpowered > 0) {
|
||||
output.push('');
|
||||
@@ -159,21 +185,22 @@ function analyzeCompare(samples, scale, maxRegression) {
|
||||
);
|
||||
}
|
||||
|
||||
const failures = maxRegression > 0 ?
|
||||
const failures = maxRegression !== undefined ?
|
||||
rows.filter((row) => isRegressionFailure(row, maxRegression)) : [];
|
||||
if (failures.length > 0) {
|
||||
output.push('');
|
||||
output.push(
|
||||
`FAIL: ${failures.length} benchmark${failures.length === 1 ? '' : 's'}` +
|
||||
` regressed by more than ${maxRegression}% (the 95% interval excludes ` +
|
||||
`the threshold and significance is family-wise corrected across ` +
|
||||
`the threshold and its one-sided test is family-wise corrected across ` +
|
||||
`${rows.length} comparisons):`,
|
||||
);
|
||||
for (const failure of failures) {
|
||||
output.push(
|
||||
` ${failure.name} ${failure.improvement.toFixed(2)}% ` +
|
||||
`(95% CI up to ${(failure.improvement + failure.ci95).toFixed(2)}%, ` +
|
||||
`adjusted p=${failure.pAdjusted.toExponential(2)})`,
|
||||
`adjusted threshold p=` +
|
||||
`${failure.pThresholdAdjusted.toExponential(2)})`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,9 +36,10 @@ async function main() {
|
||||
const runs = parseInteger(cli.optional.runs, 30, '--runs', 1);
|
||||
const warmup = parseInteger(cli.optional.warmup, 0, '--warmup', 0);
|
||||
const scale = parseInteger(cli.optional.scale, 1000, '--scale', 1);
|
||||
const hasMaxRegression = cli.optional['max-regression'] !== undefined;
|
||||
const maxRegression = parseNumber(
|
||||
cli.optional['max-regression'], 0, '--max-regression', 0);
|
||||
const analyze = !!cli.optional.analyze || maxRegression > 0;
|
||||
const analyze = !!cli.optional.analyze || hasMaxRegression;
|
||||
const options = {
|
||||
namePattern: cli.optional['name-pattern'],
|
||||
nodeArgs: cli.optional['node-arg'],
|
||||
@@ -96,7 +97,8 @@ async function main() {
|
||||
}
|
||||
|
||||
if (analyze) {
|
||||
const result = analyzeCompare(rows, scale, maxRegression);
|
||||
const result = analyzeCompare(
|
||||
rows, scale, hasMaxRegression ? maxRegression : undefined);
|
||||
process.stdout.write(result.output);
|
||||
if (result.failed) process.exitCode = 1;
|
||||
return;
|
||||
|
||||
@@ -349,6 +349,11 @@ samples, but their samples are discarded. An exception, rejection, timeout,
|
||||
abort, missing timing call, or duplicate timing call stops the current
|
||||
benchmark. Later benchmarks continue to run.
|
||||
|
||||
After a timeout or abort, the runner briefly waits for asynchronous benchmark
|
||||
work to settle before continuing. If it remains pending, all later benchmarks
|
||||
that were selected to run fail without running so that their measurements
|
||||
cannot overlap with that work.
|
||||
|
||||
For each warmup and measured callback, the runner subscribes to the configured
|
||||
diagnostics channels. Each publication queues a context diagnostic whose
|
||||
`message` is `{ name, message }`, containing the string channel name and the
|
||||
|
||||
@@ -721,8 +721,9 @@ Both parallel tools support inline analysis. `scatter-node-bench.js --analyze`
|
||||
uses the same `--xaxis`, `--category`, and `--no-chart` interface described for
|
||||
`scatter.js`. `compare-node-bench.js --analyze` performs Welch's t-test, while
|
||||
`--max-regression N` adds a corrected regression gate. The gate requires both a
|
||||
Holm-Bonferroni-adjusted p-value below 0.05 and a 95% confidence interval lying
|
||||
entirely beyond `-N%`; the point estimate alone cannot fail the command.
|
||||
Holm-Bonferroni-adjusted one-sided p-value against the `N%` threshold below 0.05
|
||||
and a 95% confidence interval lying entirely beyond `-N%`; the point estimate
|
||||
alone cannot fail the command.
|
||||
Scatter analysis reduces aggregated configurations to one value per outer
|
||||
process and uses disjoint process sets for consecutive Mann-Whitney comparisons
|
||||
so configurations sharing a process are not treated as independent samples.
|
||||
|
||||
@@ -64,6 +64,7 @@ const {
|
||||
} = require('internal/bench_runner/benchmarks_stream');
|
||||
|
||||
const { bigint: hrtime } = process.hrtime;
|
||||
const kCancellationGracePeriod = 100;
|
||||
const kHookNames = ['after', 'afterEach', 'before', 'beforeEach'];
|
||||
const kIsCliRunner = getOptionValue('--bench');
|
||||
let nextRunId = 0;
|
||||
@@ -76,6 +77,20 @@ function eventLoopTurn() {
|
||||
return new Promise((resolve) => setImmediate(resolve));
|
||||
}
|
||||
|
||||
async function settlesPromptly(work) {
|
||||
const timeout = PromiseWithResolvers();
|
||||
const timer = setTimeout(
|
||||
() => timeout.resolve(false), kCancellationGracePeriod);
|
||||
try {
|
||||
return await SafePromiseRace([
|
||||
PromisePrototypeThen(work, () => true, () => true),
|
||||
timeout.promise,
|
||||
]);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
function createAbortError(signal) {
|
||||
return new AbortError(undefined, { __proto__: null, cause: signal.reason });
|
||||
}
|
||||
@@ -86,6 +101,7 @@ function createTimeoutError(benchmark) {
|
||||
}
|
||||
|
||||
class Harness {
|
||||
#abortReason = null;
|
||||
#autoRun;
|
||||
#buildPromises = [];
|
||||
#duplicateErrors = new SafeMap();
|
||||
@@ -581,6 +597,14 @@ class Harness {
|
||||
}
|
||||
|
||||
async #executeSuite(suite) {
|
||||
if (this.#abortReason !== null) {
|
||||
await this.#completeSubtree(suite, this.#abortReason);
|
||||
suite.finished = true;
|
||||
suite.completion.resolve();
|
||||
if (!suite.isRoot) suite.emitDestroy();
|
||||
return;
|
||||
}
|
||||
|
||||
if (suite.buildError !== null) {
|
||||
await this.#diagnostic(suite.buildError, suite.loc, 'error', suite);
|
||||
await this.#completeSubtree(suite, suite.buildError);
|
||||
@@ -614,7 +638,7 @@ class Harness {
|
||||
}
|
||||
}
|
||||
|
||||
if (active) {
|
||||
if (active && this.#abortReason === null) {
|
||||
const failure = await this.#runSuiteHooks(suite, 'after');
|
||||
if (failure !== null) {
|
||||
await this.#diagnostic(
|
||||
@@ -711,6 +735,13 @@ class Harness {
|
||||
} catch {
|
||||
// Preserve the error that stopped benchmark execution.
|
||||
}
|
||||
if (!await settlesPromptly(work) && this.#abortReason === null) {
|
||||
this.#abortReason = new AbortError(
|
||||
'The benchmark run was aborted because asynchronous work did not ' +
|
||||
'settle after cancellation',
|
||||
{ __proto__: null, cause: error },
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
if (timer !== undefined) clearTimeout(timer);
|
||||
@@ -833,6 +864,7 @@ class Harness {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.#abortReason !== null) forcedError = this.#abortReason;
|
||||
if (forcedError !== undefined) {
|
||||
this.success = false;
|
||||
this.counts.failed++;
|
||||
|
||||
@@ -27,14 +27,21 @@ bench('invalid operations', options, (b) => {
|
||||
bench('throws', options, () => {
|
||||
throw new Error('benchmark failure');
|
||||
});
|
||||
bench('timeout', { samples: 1, timeout: 10 }, async () => {
|
||||
await new Promise(() => {});
|
||||
});
|
||||
let lateTimeoutActive = false;
|
||||
bench('late timeout', { samples: 1, timeout: 5 }, async (b) => {
|
||||
b.start();
|
||||
await setTimeout(30);
|
||||
b.end(1);
|
||||
lateTimeoutActive = true;
|
||||
try {
|
||||
b.start();
|
||||
await setTimeout(30);
|
||||
b.end(1);
|
||||
} finally {
|
||||
lateTimeoutActive = false;
|
||||
}
|
||||
});
|
||||
bench('after late timeout', options, common.mustCall((b) => {
|
||||
assert.strictEqual(lateTimeoutActive, false);
|
||||
complete(b);
|
||||
}));
|
||||
|
||||
const signal = AbortSignal.abort(new Error('stop'));
|
||||
bench('aborted', { samples: 1, signal }, () => {});
|
||||
@@ -48,6 +55,11 @@ function complete(b) {
|
||||
bench('duplicate', { samples: 1, params: { value: 1 } }, complete);
|
||||
bench('duplicate', { samples: 1, params: { value: 1 } }, complete);
|
||||
bench('continues', options, complete);
|
||||
bench('timeout', { samples: 1, timeout: 10 }, async () => {
|
||||
await new Promise(() => {});
|
||||
});
|
||||
bench('after unsettled timeout', options, common.mustNotCall());
|
||||
bench.skip('skipped after unsettled timeout', options, common.mustNotCall());
|
||||
|
||||
const completions = [];
|
||||
const sampleNames = [];
|
||||
@@ -57,13 +69,13 @@ stream.on('bench:complete', (result) => completions.push(result));
|
||||
stream.on('bench:sample', (sample) => sampleNames.push(sample.name));
|
||||
stream.on('bench:summary', (result) => { summary = result; });
|
||||
stream.on('end', common.mustCall(() => {
|
||||
assert.strictEqual(completions.length, 13);
|
||||
assert.strictEqual(completions.length, 16);
|
||||
assert.deepStrictEqual(summary.counts, {
|
||||
__proto__: null,
|
||||
completed: 2,
|
||||
failed: 11,
|
||||
skipped: 0,
|
||||
total: 13,
|
||||
completed: 3,
|
||||
failed: 12,
|
||||
skipped: 1,
|
||||
total: 16,
|
||||
});
|
||||
assert.strictEqual(summary.success, false);
|
||||
|
||||
@@ -92,12 +104,18 @@ stream.on('end', common.mustCall(() => {
|
||||
'ERR_OPERATION_FAILED');
|
||||
assert.strictEqual(byName.get('late timeout')[0].error.code,
|
||||
'ERR_OPERATION_FAILED');
|
||||
assert.strictEqual(byName.get('after late timeout')[0].error, undefined);
|
||||
assert.strictEqual(byName.get('aborted')[0].error.code, 'ABORT_ERR');
|
||||
|
||||
const duplicates = byName.get('duplicate');
|
||||
assert.strictEqual(duplicates[0].error, undefined);
|
||||
assert.match(duplicates[1].error.message, /duplicate benchmark identity/);
|
||||
assert.strictEqual(byName.get('continues')[0].error, undefined);
|
||||
const unsettled = byName.get('after unsettled timeout')[0].error;
|
||||
assert.strictEqual(unsettled.code, 'ABORT_ERR');
|
||||
assert.strictEqual(unsettled.cause.code, 'ERR_OPERATION_FAILED');
|
||||
assert.strictEqual(
|
||||
byName.get('skipped after unsettled timeout')[0].skip, true);
|
||||
setTimeout(40).then(common.mustCall(() => {
|
||||
assert.strictEqual(sampleNames.includes('late timeout'), false);
|
||||
}));
|
||||
|
||||
@@ -9,6 +9,7 @@ const path = require('path');
|
||||
const fixtures = require('../common/fixtures');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
const {
|
||||
analyzeCompare,
|
||||
analyzeScatter,
|
||||
holmAdjust,
|
||||
isRegressionFailure,
|
||||
@@ -27,18 +28,51 @@ assert.deepStrictEqual(holmAdjust([0.01, 0.03, 0.04]), [0.03, 0.06, 0.06]);
|
||||
assert.strictEqual(isRegressionFailure({
|
||||
ci95: 3,
|
||||
improvement: -12,
|
||||
pAdjusted: 0.01,
|
||||
pThresholdAdjusted: 0.01,
|
||||
}, 10), false);
|
||||
assert.strictEqual(isRegressionFailure({
|
||||
ci95: 1,
|
||||
improvement: -12,
|
||||
pAdjusted: 0.06,
|
||||
pThresholdAdjusted: 0.06,
|
||||
}, 10), false);
|
||||
assert.strictEqual(isRegressionFailure({
|
||||
ci95: 1,
|
||||
improvement: -12,
|
||||
pAdjusted: 0.01,
|
||||
pThresholdAdjusted: 0.01,
|
||||
}, 10), true);
|
||||
|
||||
function comparisonSamples(oldRates, newRates) {
|
||||
const create = (binary, rate) => ({
|
||||
binary,
|
||||
configuration: '',
|
||||
identity: 'comparison',
|
||||
name: 'comparison',
|
||||
rate,
|
||||
});
|
||||
return [
|
||||
...oldRates.map((rate) => create('old', rate)),
|
||||
...newRates.map((rate) => create('new', rate)),
|
||||
];
|
||||
}
|
||||
|
||||
{
|
||||
const result = analyzeCompare(comparisonSamples(
|
||||
[98, 99, 100, 100, 101, 102],
|
||||
[48, 49, 50, 50, 51, 52],
|
||||
), 1000, 0);
|
||||
assert.strictEqual(result.failed, true);
|
||||
assert.strictEqual(result.rows[0].pThresholdAdjusted < 0.05, true);
|
||||
}
|
||||
|
||||
{
|
||||
const result = analyzeCompare(comparisonSamples(
|
||||
[98, 99, 100, 100, 101, 102],
|
||||
[93, 94, 95, 95, 96, 97],
|
||||
), 1000, 10);
|
||||
assert.strictEqual(result.rows[0].pAdjusted < 0.05, true);
|
||||
assert.strictEqual(result.rows[0].pThresholdAdjusted > 0.05, true);
|
||||
assert.strictEqual(result.failed, false);
|
||||
}
|
||||
assert.throws(
|
||||
() => analyzeScatter([{
|
||||
observation: 0,
|
||||
@@ -221,7 +255,22 @@ function run(script, args, options = undefined) {
|
||||
assert.strictEqual(result.stderr, '');
|
||||
assert.match(result.stdout, /confidence\s+improvement\s+accuracy/);
|
||||
assert.match(result.stdout, /Holm-Bonferroni correction/);
|
||||
assert.match(result.stdout, /--max-regression uses the corrected values/);
|
||||
assert.match(result.stdout, /one-sided p-values against the 100% threshold/);
|
||||
assert.doesNotMatch(result.stdout, /"binary","filename"/);
|
||||
}
|
||||
|
||||
{
|
||||
const result = run(compare, [
|
||||
'--old', process.execPath,
|
||||
'--new', process.execPath,
|
||||
'--runs', '1',
|
||||
'--max-regression', '0',
|
||||
'--', fixtures.path('bench-runner/tools-no-params.cjs'),
|
||||
]);
|
||||
assert.strictEqual(result.status, 0, result.stderr);
|
||||
assert.strictEqual(result.stderr, '');
|
||||
assert.match(result.stdout, /confidence\s+improvement\s+accuracy/);
|
||||
assert.match(result.stdout, /one-sided p-values against the 0% threshold/);
|
||||
assert.doesNotMatch(result.stdout, /"binary","filename"/);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user