Skip to content

Commit 126407d

Browse files
joyeecheungtargos
authored andcommitted
test: deflake test-perf-hooks.js
Previously when checking the initial timing we did a lot of checks after accessing and copying timing.duration and before we check that timing.duration is roughly the same as performance.now(), which can lead to flakes if the overhead of the checking is big enough. Update the test to check timing.duration against performance.now() as soon as possible when it's copied instead of computed. :# PR-URL: #49892 Refs: nodejs/reliability#676 Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent f564ed4 commit 126407d

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

test/sequential/test-perf-hooks.js

+32-30
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,40 @@ const epsilon = 50;
4141
assert.strictEqual(performance.nodeTiming.name, 'node');
4242
assert.strictEqual(performance.nodeTiming.entryType, 'node');
4343

44+
// If timing.duration gets copied into the argument instead of being computed
45+
// via the getter, this should be called right after timing is created.
46+
function checkNodeTiming(timing) {
47+
// Calculate the difference between now() and duration as soon as possible.
48+
const now = performance.now();
49+
const delta = Math.abs(now - timing.duration);
50+
51+
log(JSON.stringify(timing, null, 2));
52+
// Check that the properties are still reasonable.
53+
assert.strictEqual(timing.name, 'node');
54+
assert.strictEqual(timing.entryType, 'node');
55+
56+
// Check that duration is positive and practically the same as
57+
// performance.now() i.e. measures Node.js instance up time.
58+
assert.strictEqual(typeof timing.duration, 'number');
59+
assert(timing.duration > 0, `timing.duration ${timing.duration} <= 0`);
60+
assert(delta < 10,
61+
`now (${now}) - timing.duration (${timing.duration}) = ${delta} >= ${10}`);
62+
63+
// Check that the following fields do not change.
64+
assert.strictEqual(timing.startTime, initialTiming.startTime);
65+
assert.strictEqual(timing.nodeStart, initialTiming.nodeStart);
66+
assert.strictEqual(timing.v8Start, initialTiming.v8Start);
67+
assert.strictEqual(timing.environment, initialTiming.environment);
68+
assert.strictEqual(timing.bootstrapComplete, initialTiming.bootstrapComplete);
69+
70+
assert.strictEqual(typeof timing.loopStart, 'number');
71+
assert.strictEqual(typeof timing.loopExit, 'number');
72+
}
73+
74+
log('check initial nodeTiming');
4475
// Copy all the values from the getters.
4576
const initialTiming = { ...performance.nodeTiming };
77+
checkNodeTiming(initialTiming);
4678

4779
{
4880
const {
@@ -87,36 +119,6 @@ const initialTiming = { ...performance.nodeTiming };
87119
`bootstrapComplete ${bootstrapComplete} >= ${testStartTime}`);
88120
}
89121

90-
function checkNodeTiming(timing) {
91-
// Calculate the difference between now() and duration as soon as possible.
92-
const now = performance.now();
93-
const delta = Math.abs(now - timing.duration);
94-
95-
log(JSON.stringify(timing, null, 2));
96-
// Check that the properties are still reasonable.
97-
assert.strictEqual(timing.name, 'node');
98-
assert.strictEqual(timing.entryType, 'node');
99-
100-
// Check that duration is positive and practically the same as
101-
// performance.now() i.e. measures Node.js instance up time.
102-
assert.strictEqual(typeof timing.duration, 'number');
103-
assert(timing.duration > 0, `timing.duration ${timing.duration} <= 0`);
104-
assert(delta < 10,
105-
`now (${now}) - timing.duration (${timing.duration}) = ${delta} >= 10`);
106-
107-
// Check that the following fields do not change.
108-
assert.strictEqual(timing.startTime, initialTiming.startTime);
109-
assert.strictEqual(timing.nodeStart, initialTiming.nodeStart);
110-
assert.strictEqual(timing.v8Start, initialTiming.v8Start);
111-
assert.strictEqual(timing.environment, initialTiming.environment);
112-
assert.strictEqual(timing.bootstrapComplete, initialTiming.bootstrapComplete);
113-
114-
assert.strictEqual(typeof timing.loopStart, 'number');
115-
assert.strictEqual(typeof timing.loopExit, 'number');
116-
}
117-
118-
log('check initial nodeTiming');
119-
checkNodeTiming(initialTiming);
120122
assert.strictEqual(initialTiming.loopExit, -1);
121123

122124
function checkValue(timing, name, min, max) {

0 commit comments

Comments
 (0)