1'use strict'; 2 3const common = require('../common'); 4const assert = require('assert'); 5 6const { performance } = require('perf_hooks'); 7 8function createTimingInfo(startTime) { 9 const timingInfo = { 10 startTime: startTime, 11 endTime: startTime, 12 finalServiceWorkerStartTime: 0, 13 redirectStartTime: 0, 14 redirectEndTime: 0, 15 postRedirectStartTime: 0, 16 finalConnectionTimingInfo: { 17 domainLookupStartTime: 0, 18 domainLookupEndTime: 0, 19 connectionStartTime: 0, 20 connectionEndTime: 0, 21 secureConnectionStartTime: 0, 22 ALPNNegotiatedProtocol: 0, 23 }, 24 finalNetworkRequestStartTime: 0, 25 finalNetworkResponseStartTime: 0, 26 encodedBodySize: 0, 27 decodedBodySize: 0, 28 }; 29 return timingInfo; 30} 31const requestedUrl = 'https://nodejs.org'; 32const initiatorType = ''; 33const cacheMode = ''; 34 35async function main() { 36 performance.setResourceTimingBufferSize(1); 37 performance.markResourceTiming(createTimingInfo(1), requestedUrl, initiatorType, globalThis, cacheMode); 38 // Trigger a resourcetimingbufferfull event. 39 performance.markResourceTiming(createTimingInfo(2), requestedUrl, initiatorType, globalThis, cacheMode); 40 performance.markResourceTiming(createTimingInfo(3), requestedUrl, initiatorType, globalThis, cacheMode); 41 assert.strictEqual(performance.getEntriesByType('resource').length, 1); 42 43 // Clear resource timings on resourcetimingbufferfull event. 44 await new Promise((resolve) => { 45 const listener = common.mustCall((event) => { 46 assert.strictEqual(event.type, 'resourcetimingbufferfull'); 47 performance.removeEventListener('resourcetimingbufferfull', listener); 48 49 performance.clearResourceTimings(); 50 assert.strictEqual(performance.getEntriesByType('resource').length, 0); 51 52 resolve(); 53 }); 54 performance.addEventListener('resourcetimingbufferfull', listener); 55 }); 56 57 // Secondary buffer has been added to the global buffer. 58 { 59 const entries = performance.getEntriesByType('resource'); 60 assert.strictEqual(entries.length, 1); 61 assert.strictEqual(entries[0].startTime, 2); 62 // The last item is discarded. 63 } 64 65 66 performance.clearResourceTimings(); 67 performance.setResourceTimingBufferSize(1); 68 performance.markResourceTiming(createTimingInfo(4), requestedUrl, initiatorType, globalThis, cacheMode); 69 // Trigger a resourcetimingbufferfull event. 70 performance.markResourceTiming(createTimingInfo(5), requestedUrl, initiatorType, globalThis, cacheMode); 71 performance.markResourceTiming(createTimingInfo(6), requestedUrl, initiatorType, globalThis, cacheMode); 72 73 // Increase the buffer size on resourcetimingbufferfull event. 74 await new Promise((resolve) => { 75 const listener = common.mustCall((event) => { 76 assert.strictEqual(event.type, 'resourcetimingbufferfull'); 77 performance.removeEventListener('resourcetimingbufferfull', listener); 78 79 performance.setResourceTimingBufferSize(2); 80 assert.strictEqual(performance.getEntriesByType('resource').length, 1); 81 82 resolve(); 83 }); 84 performance.addEventListener('resourcetimingbufferfull', listener); 85 }); 86 87 // Secondary buffer has been added to the global buffer. 88 { 89 const entries = performance.getEntriesByType('resource'); 90 assert.strictEqual(entries.length, 2); 91 assert.strictEqual(entries[0].startTime, 4); 92 assert.strictEqual(entries[1].startTime, 5); 93 // The last item is discarded. 94 } 95 96 97 performance.clearResourceTimings(); 98 performance.setResourceTimingBufferSize(2); 99 performance.markResourceTiming(createTimingInfo(7), requestedUrl, initiatorType, globalThis, cacheMode); 100 performance.markResourceTiming(createTimingInfo(8), requestedUrl, initiatorType, globalThis, cacheMode); 101 // Trigger a resourcetimingbufferfull event. 102 performance.markResourceTiming(createTimingInfo(9), requestedUrl, initiatorType, globalThis, cacheMode); 103 104 // Decrease the buffer size on resourcetimingbufferfull event. 105 await new Promise((resolve) => { 106 const listener = common.mustCall((event) => { 107 assert.strictEqual(event.type, 'resourcetimingbufferfull'); 108 performance.removeEventListener('resourcetimingbufferfull', listener); 109 110 performance.setResourceTimingBufferSize(1); 111 assert.strictEqual(performance.getEntriesByType('resource').length, 2); 112 113 resolve(); 114 }); 115 performance.addEventListener('resourcetimingbufferfull', listener); 116 }); 117 118 // Secondary buffer has been added to the global buffer. 119 { 120 const entries = performance.getEntriesByType('resource'); 121 assert.strictEqual(entries.length, 2); 122 assert.strictEqual(entries[0].startTime, 7); 123 assert.strictEqual(entries[1].startTime, 8); 124 // The last item is discarded. 125 } 126} 127 128main(); 129