diff --git a/test/common/index.js b/test/common/index.js index db5b81d4957d44..3f612afbff5ef9 100755 --- a/test/common/index.js +++ b/test/common/index.js @@ -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 */ diff --git a/test/common/index.mjs b/test/common/index.mjs index 29030aeb591ba2..7516eb68a53b7a 100644 --- a/test/common/index.mjs +++ b/test/common/index.mjs @@ -50,6 +50,7 @@ const { skipIfInspectorDisabled, skipIfSQLiteMissing, spawnPromisified, + sleepSync, } = common; const getPort = () => common.PORT; @@ -103,4 +104,5 @@ export { skipIfInspectorDisabled, skipIfSQLiteMissing, spawnPromisified, + sleepSync, }; diff --git a/test/parallel/test-perf-hooks-timerify-histogram-sync.mjs b/test/parallel/test-perf-hooks-timerify-histogram-sync.mjs index 4944aad3b64259..b26a40ee3cda65 100644 --- a/test/parallel/test-perf-hooks-timerify-histogram-sync.mjs +++ b/test/parallel/test-perf-hooks-timerify-histogram-sync.mjs @@ -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);