-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-performance-eventloopdelay.js
More file actions
135 lines (123 loc) Β· 3.79 KB
/
test-performance-eventloopdelay.js
File metadata and controls
135 lines (123 loc) Β· 3.79 KB
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
// Flags: --expose-gc --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const {
monitorEventLoopDelay,
} = require('perf_hooks');
const { sleep } = require('internal/util');
{
const histogram = monitorEventLoopDelay();
assert(histogram);
assert(histogram.enable());
assert(!histogram.enable());
histogram.reset();
assert(histogram.disable());
assert(!histogram.disable());
}
{
[null, 'a', 1, false, Infinity].forEach((i) => {
assert.throws(
() => monitorEventLoopDelay(i),
{
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
}
);
});
[null, 'a', false, {}, []].forEach((i) => {
assert.throws(
() => monitorEventLoopDelay({ resolution: i }),
{
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
}
);
});
[-1, 0, 2 ** 53, Infinity].forEach((i) => {
assert.throws(
() => monitorEventLoopDelay({ resolution: i }),
{
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
}
);
});
}
{
const histogram = monitorEventLoopDelay({ resolution: 1 });
histogram.enable();
// Check if histogram has recorded valid samples (min > 0 indicates real delays measured)
function hasValidSamples() {
return histogram.count > 0 && histogram.min > 0 && histogram.max > 0;
}
// Final assertions - call this when ready to verify histogram values
function runAssertions() {
setImmediate(common.mustCall(() => {
histogram.disable();
// The values are non-deterministic, so we just check that a value is
// present, as opposed to a specific value.
assert(histogram.count > 0, `Expected samples to be recorded, got count=${histogram.count}`);
assert(histogram.min > 0, `Expected min > 0, got ${histogram.min}`);
assert(histogram.max > 0);
assert(histogram.stddev > 0);
assert(histogram.mean > 0);
assert(histogram.percentiles.size > 0);
for (let n = 1; n < 100; n = n + 0.1) {
assert(histogram.percentile(n) >= 0);
}
histogram.reset();
assert.strictEqual(histogram.min, 9223372036854776000);
assert.strictEqual(histogram.max, 0);
assert(Number.isNaN(histogram.stddev));
assert(Number.isNaN(histogram.mean));
assert.strictEqual(histogram.percentiles.size, 1);
['a', false, {}, []].forEach((i) => {
assert.throws(
() => histogram.percentile(i),
{
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
}
);
});
[-1, 0, 101, NaN].forEach((i) => {
assert.throws(
() => histogram.percentile(i),
{
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
}
);
});
}));
}
// Spin the event loop with blocking work to generate measurable delays.
// Some configurations (s390x, sharedlibs) need more iterations.
let spinsRemaining = 5;
const maxRetries = 3;
let retries = 0;
function spinAWhile() {
sleep(1000);
if (--spinsRemaining > 0) {
setTimeout(spinAWhile, common.platformTimeout(500));
} else {
// Give the histogram a chance to record final samples before checking.
setImmediate(() => {
// If we have valid samples or exhausted retries, run assertions.
// Otherwise retry with more spinning for slower configurations.
if (hasValidSamples() || retries >= maxRetries) {
runAssertions();
} else {
retries++;
spinsRemaining = 5;
setTimeout(spinAWhile, common.platformTimeout(500));
}
});
}
}
spinAWhile();
}
// Make sure that the histogram instances can be garbage-collected without
// and not just implicitly destroyed when the Environment is torn down.
process.on('exit', global.gc);