test: deflake test-perf-hooks-timerify-histogram-sync

The previous busy loop wasn't robust enough in making sure
that the function runs for more than 1 nanosecond - and when it
runs faster than that on a fast machine, it measures to 0 for
nanosecond precision and throws a RangeErorr as histogram.record()
only takes positive values. Update it to use Atomics.wait() to
make sure that the function being measured runs for at least
1 millisecond so that the histogram always records a positive value.

PR-URL: https://github.com/nodejs/node/pull/60639
Fixes: https://github.com/nodejs/node/issues/60638
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Joyee Cheung authored and GitHub committed 2025-11-10 17:46:46 +00:00
1 parent 5e1ab9fffb
commit 33f982e7ed
3 files changed
+15 -9

No files matched your search

+7
View File
@@ -931,6 +931,12 @@ function expectRequiredTLAError(err) {
}
}
function sleepSync(ms) {
const sab = new SharedArrayBuffer(4);
const i32 = new Int32Array(sab);
Atomics.wait(i32, 0, 0, ms);
}
const common = {
allowGlobals,
buildType,
@@ -982,6 +988,7 @@ const common = {
skipIfInspectorDisabled,
skipIfSQLiteMissing,
spawnPromisified,
sleepSync,
get enoughTestMem() {
return require('os').totalmem() > 0x70000000; /* 1.75 Gb */
+2
View File
@@ -50,6 +50,7 @@ const {
skipIfInspectorDisabled,
skipIfSQLiteMissing,
spawnPromisified,
sleepSync,
} = common;
const getPort = () => common.PORT;
@@ -103,4 +104,5 @@ export {
skipIfInspectorDisabled,
skipIfSQLiteMissing,
spawnPromisified,
sleepSync,
};
@@ -1,22 +1,19 @@
// Test that timerify works with histogram option for synchronous functions.
import '../common/index.mjs';
import { sleepSync } from '../common/index.mjs';
import assert from 'assert';
import { createHistogram, timerify } from 'perf_hooks';
import { setTimeout as sleep } from 'timers/promises';
let _deadCode;
const histogram = createHistogram();
const m = (a, b = 1) => {
for (let i = 0; i < 1e3; i++)
_deadCode = i;
const m = () => {
// Deterministic blocking delay (~1 millisecond). The histogram operates on
// nanosecond precision, so this should be sufficient to prevent zero timings.
sleepSync(1);
};
const n = timerify(m, { histogram });
assert.strictEqual(histogram.max, 0);
for (let i = 0; i < 10; i++) {
n();
await sleep(10);
}
assert(_deadCode >= 0);
assert.notStrictEqual(histogram.max, 0);