-
Notifications
You must be signed in to change notification settings - Fork 29.8k
/
test-performance.js
147 lines (126 loc) Β· 3.6 KB
/
test-performance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict';
const common = require('../common');
const assert = require('assert');
const { performance } = require('perf_hooks');
if (!common.isMainThread)
common.skip('bootstrapping workers works differently');
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 15 seconds.
assert(Math.abs(performance.timeOrigin - Date.now()) < 15000);
const inited = performance.now();
assert(inited < 15000);
{
// Should work without throwing any errors
performance.mark('A');
performance.clearMarks('A');
performance.mark('B');
performance.clearMarks();
}
{
performance.mark('A');
[undefined, null, 'foo', 'initialize', 1].forEach((i) => {
performance.measure('test', i, 'A'); // Should not throw.
});
[undefined, null, 'foo', 1].forEach((i) => {
common.expectsError(
() => performance.measure('test', 'A', i),
{
code: 'ERR_INVALID_PERFORMANCE_MARK',
type: Error,
message: `The "${i}" performance mark has not been set`
});
});
performance.clearMarks();
}
{
performance.mark('A');
setImmediate(() => {
performance.mark('B');
performance.measure('foo', 'A', 'B');
});
}
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;
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}: ` +
`${performance.nodeTiming[prop]} vs ${props[prop]}`);
}
}
}
checkNodeTiming({
name: 'node',
entryType: 'node',
startTime: 0,
duration: { around: performance.now() },
nodeStart: { around: 0 },
v8Start: { around: 0 },
bootstrapComplete: { around: inited },
environment: { around: 0 },
loopStart: -1,
loopExit: -1
});
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({
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: { around: performance.now() }
});
});