• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5
6const { performance } = require('perf_hooks');
7
8const timingInfo = {
9  startTime: 0,
10  endTime: 0,
11  finalServiceWorkerStartTime: 0,
12  redirectStartTime: 0,
13  redirectEndTime: 0,
14  postRedirectStartTime: 0,
15  finalConnectionTimingInfo: {
16    domainLookupStartTime: 0,
17    domainLookupEndTime: 0,
18    connectionStartTime: 0,
19    connectionEndTime: 0,
20    secureConnectionStartTime: 0,
21    ALPNNegotiatedProtocol: 0,
22  },
23  finalNetworkRequestStartTime: 0,
24  finalNetworkResponseStartTime: 0,
25  encodedBodySize: 0,
26  decodedBodySize: 0,
27};
28const requestedUrl = 'https://nodejs.org';
29const initiatorType = '';
30const cacheMode = '';
31
32async function main() {
33  // Invalid buffer size values are converted to 0.
34  const invalidValues = [ null, undefined, true, false, -1, 0.5, Infinity, NaN, '', 'foo', {}, [], () => {} ];
35  for (const value of invalidValues) {
36    performance.setResourceTimingBufferSize(value);
37    performance.markResourceTiming(timingInfo, requestedUrl, initiatorType, globalThis, cacheMode);
38    assert.strictEqual(performance.getEntriesByType('resource').length, 0);
39    performance.clearResourceTimings();
40  }
41  // Wait for the buffer full event to be cleared.
42  await waitBufferFullEvent();
43
44  performance.setResourceTimingBufferSize(1);
45  performance.markResourceTiming(timingInfo, requestedUrl, initiatorType, globalThis, cacheMode);
46  // Trigger a resourcetimingbufferfull event.
47  performance.markResourceTiming(timingInfo, requestedUrl, initiatorType, globalThis, cacheMode);
48  assert.strictEqual(performance.getEntriesByType('resource').length, 1);
49  await waitBufferFullEvent();
50
51  // Apply a new buffer size limit
52  performance.setResourceTimingBufferSize(0);
53  // Buffer is not cleared on `performance.setResourceTimingBufferSize`.
54  assert.strictEqual(performance.getEntriesByType('resource').length, 1);
55
56  performance.clearResourceTimings();
57  assert.strictEqual(performance.getEntriesByType('resource').length, 0);
58  // Trigger a resourcetimingbufferfull event.
59  performance.markResourceTiming(timingInfo, requestedUrl, initiatorType, globalThis, cacheMode);
60  // New entry is not added to the global buffer.
61  assert.strictEqual(performance.getEntriesByType('resource').length, 0);
62  await waitBufferFullEvent();
63
64  // Apply a new buffer size limit
65  performance.setResourceTimingBufferSize(1);
66  performance.markResourceTiming(timingInfo, requestedUrl, initiatorType, globalThis, cacheMode);
67  assert.strictEqual(performance.getEntriesByType('resource').length, 1);
68}
69
70function waitBufferFullEvent() {
71  return new Promise((resolve) => {
72    const listener = common.mustCall((event) => {
73      assert.strictEqual(event.type, 'resourcetimingbufferfull');
74      performance.removeEventListener('resourcetimingbufferfull', listener);
75      resolve();
76    });
77    performance.addEventListener('resourcetimingbufferfull', listener);
78  });
79}
80
81main();
82