Skip to content

Commit 2f57673

Browse files
joyeecheungaduh95
authored andcommitted
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: #60639 Fixes: #60638 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 1daece1 commit 2f57673

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

test/common/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,12 @@ function expectRequiredTLAError(err) {
910910
}
911911
}
912912

913+
function sleepSync(ms) {
914+
const sab = new SharedArrayBuffer(4);
915+
const i32 = new Int32Array(sab);
916+
Atomics.wait(i32, 0, 0, ms);
917+
}
918+
913919
const common = {
914920
allowGlobals,
915921
buildType,
@@ -959,6 +965,7 @@ const common = {
959965
skipIfInspectorDisabled,
960966
skipIfSQLiteMissing,
961967
spawnPromisified,
968+
sleepSync,
962969

963970
get enoughTestMem() {
964971
return require('os').totalmem() > 0x70000000; /* 1.75 Gb */

test/common/index.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const {
4747
skipIfInspectorDisabled,
4848
skipIfSQLiteMissing,
4949
spawnPromisified,
50+
sleepSync,
5051
} = common;
5152

5253
const getPort = () => common.PORT;
@@ -97,4 +98,5 @@ export {
9798
skipIfInspectorDisabled,
9899
skipIfSQLiteMissing,
99100
spawnPromisified,
101+
sleepSync,
100102
};
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
// Test that timerify works with histogram option for synchronous functions.
22

3-
import '../common/index.mjs';
3+
import { sleepSync } from '../common/index.mjs';
44
import assert from 'assert';
55
import { createHistogram, timerify } from 'perf_hooks';
6-
import { setTimeout as sleep } from 'timers/promises';
7-
8-
let _deadCode;
96

107
const histogram = createHistogram();
11-
const m = (a, b = 1) => {
12-
for (let i = 0; i < 1e3; i++)
13-
_deadCode = i;
8+
9+
const m = () => {
10+
// Deterministic blocking delay (~1 millisecond). The histogram operates on
11+
// nanosecond precision, so this should be sufficient to prevent zero timings.
12+
sleepSync(1);
1413
};
1514
const n = timerify(m, { histogram });
1615
assert.strictEqual(histogram.max, 0);
1716
for (let i = 0; i < 10; i++) {
1817
n();
19-
await sleep(10);
2018
}
21-
assert(_deadCode >= 0);
2219
assert.notStrictEqual(histogram.max, 0);

0 commit comments

Comments
 (0)