Skip to content

Commit

Permalink
test: harden sequential/test-performance
Browse files Browse the repository at this point in the history
1) This adds a better error logging so we are able to address further
   failures easier.
2) It adds a extra epsilon so the test runs into less issues in case
   the machine is under heavy load.
3) The epsilon in increased if the CPU is under heavy load.
4) The total startup epsilon was reduced due to recent startup time
   improvements.

PR-URL: #22404
Fixes: #19197
Refs: nodejs/reliability#14
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
  • Loading branch information
BridgeAR authored and targos committed Sep 3, 2018
1 parent 38b0c1f commit acfb29c
Showing 1 changed file with 51 additions and 18 deletions.
69 changes: 51 additions & 18 deletions test/sequential/test-performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ assert(performance);
assert(performance.nodeTiming);
assert.strictEqual(typeof performance.timeOrigin, 'number');
// Use a fairly large epsilon value, since we can only guarantee that the node
// process started up in 20 seconds.
assert(Math.abs(performance.timeOrigin - Date.now()) < 20000);
// process started up in 15 seconds.
assert(Math.abs(performance.timeOrigin - Date.now()) < 15000);

const inited = performance.now();
assert(inited < 20000);
assert(inited < 15000);

{
// Should work without throwing any errors
Expand Down Expand Up @@ -56,12 +56,43 @@ assert(inited < 20000);
assert.strictEqual(performance.nodeTiming.name, 'node');
assert.strictEqual(performance.nodeTiming.entryType, 'node');

let timeoutDelay = 111; // An extra of 111 ms for the first call.

function checkDelay(cb) {
const defaultTimeout = 1;
const timer = setInterval(checkDelay, defaultTimeout);
const timeouts = 10;

const now = getTime();
let resolved = 0;

function checkDelay() {
resolved++;
if (resolved === timeouts) {
clearInterval(timer);
timeoutDelay = getTime() - now;
cb();
}
}
}

function getTime() {
const ts = process.hrtime();
return Math.ceil((ts[0] * 1e3) + (ts[1] / 1e6));
}

function checkNodeTiming(props) {
console.log(props);

for (const prop of Object.keys(props)) {
if (props[prop].around !== undefined) {
assert.strictEqual(typeof performance.nodeTiming[prop], 'number');
const delta = performance.nodeTiming[prop] - props[prop].around;
assert(Math.abs(delta) < 1000);
const delay = 1000 + timeoutDelay;
assert(
Math.abs(delta) < delay,
`${prop}: ${Math.abs(delta)} >= ${delay}`
);
} else {
assert.strictEqual(performance.nodeTiming[prop], props[prop],
`mismatch for performance property ${prop}: ` +
Expand All @@ -83,20 +114,22 @@ checkNodeTiming({
loopExit: -1
});

setTimeout(() => {
checkNodeTiming({
name: 'node',
entryType: 'node',
startTime: 0,
duration: { around: performance.now() },
nodeStart: { around: 0 },
v8Start: { around: 0 },
bootstrapComplete: { around: inited },
environment: { around: 0 },
loopStart: { around: inited },
loopExit: -1
});
}, 2000);
checkDelay(() => {
setTimeout(() => {
checkNodeTiming({
name: 'node',
entryType: 'node',
startTime: 0,
duration: { around: performance.now() },
nodeStart: { around: 0 },
v8Start: { around: 0 },
bootstrapComplete: { around: inited },
environment: { around: 0 },
loopStart: { around: inited },
loopExit: -1
});
}, 1000);
});

process.on('exit', () => {
checkNodeTiming({
Expand Down

0 comments on commit acfb29c

Please sign in to comment.