forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-performance-resourcetimingbuffersize.js
70 lines (61 loc) · 2.35 KB
/
test-performance-resourcetimingbuffersize.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
'use strict';
const common = require('../common');
const assert = require('assert');
const { performance } = require('perf_hooks');
const timingInfo = {
startTime: 0,
endTime: 0,
finalServiceWorkerStartTime: 0,
redirectStartTime: 0,
redirectEndTime: 0,
postRedirectStartTime: 0,
finalConnectionTimingInfo: {
domainLookupStartTime: 0,
domainLookupEndTime: 0,
connectionStartTime: 0,
connectionEndTime: 0,
secureConnectionStartTime: 0,
ALPNNegotiatedProtocol: 0,
},
finalNetworkRequestStartTime: 0,
finalNetworkResponseStartTime: 0,
encodedBodySize: 0,
decodedBodySize: 0,
};
const requestedUrl = 'https://nodejs.org';
const initiatorType = '';
const cacheMode = '';
async function main() {
performance.setResourceTimingBufferSize(1);
performance.markResourceTiming(timingInfo, requestedUrl, initiatorType, globalThis, cacheMode);
// Trigger a resourcetimingbufferfull event.
performance.markResourceTiming(timingInfo, requestedUrl, initiatorType, globalThis, cacheMode);
assert.strictEqual(performance.getEntriesByType('resource').length, 1);
await waitBufferFullEvent();
// Apply a new buffer size limit
performance.setResourceTimingBufferSize(0);
// Buffer is not cleared on `performance.setResourceTimingBufferSize`.
assert.strictEqual(performance.getEntriesByType('resource').length, 1);
performance.clearResourceTimings();
assert.strictEqual(performance.getEntriesByType('resource').length, 0);
// Trigger a resourcetimingbufferfull event.
performance.markResourceTiming(timingInfo, requestedUrl, initiatorType, globalThis, cacheMode);
// New entry is not added to the global buffer.
assert.strictEqual(performance.getEntriesByType('resource').length, 0);
await waitBufferFullEvent();
// Apply a new buffer size limit
performance.setResourceTimingBufferSize(1);
performance.markResourceTiming(timingInfo, requestedUrl, initiatorType, globalThis, cacheMode);
assert.strictEqual(performance.getEntriesByType('resource').length, 1);
}
function waitBufferFullEvent() {
return new Promise((resolve) => {
const listener = common.mustCall((event) => {
assert.strictEqual(event.type, 'resourcetimingbufferfull');
performance.removeEventListener('resourcetimingbufferfull', listener);
resolve();
});
performance.addEventListener('resourcetimingbufferfull', listener);
});
}
main();