|
1 | 1 | 'use strict'; |
2 | 2 |
|
| 3 | +const common = require('../common'); |
3 | 4 | const assert = require('assert'); |
4 | 5 | const { |
5 | 6 | PerformanceObserver, |
6 | 7 | PerformanceEntry, |
7 | 8 | PerformanceResourceTiming, |
8 | 9 | performance: { |
9 | | - clearResourceTimings |
| 10 | + clearResourceTimings, |
| 11 | + markResourceTiming, |
10 | 12 | }, |
11 | 13 | } = require('perf_hooks'); |
12 | 14 |
|
13 | 15 | assert(PerformanceObserver); |
14 | 16 | assert(PerformanceEntry); |
15 | 17 | assert(PerformanceResourceTiming); |
16 | 18 | assert(clearResourceTimings); |
| 19 | +assert(markResourceTiming); |
17 | 20 |
|
18 | | -// TODO:continue once `markResourceTiming` is done |
| 21 | +function createTimingInfo({ |
| 22 | + startTime = 0, |
| 23 | + redirectStartTime = 0, |
| 24 | + redirectEndTime = 0, |
| 25 | + postRedirectStartTime = 0, |
| 26 | + finalServiceWorkerStartTime = 0, |
| 27 | + finalNetworkRequestStartTime = 0, |
| 28 | + finalNetworkResponseStartTime = 0, |
| 29 | + endTime = 0, |
| 30 | + encodedBodySize = 0, |
| 31 | + decodedBodySize = 0, |
| 32 | + finalConnectionTimingInfo = null |
| 33 | +}) { |
| 34 | + if (finalConnectionTimingInfo !== null) { |
| 35 | + finalConnectionTimingInfo.domainLookupStartTime = |
| 36 | + finalConnectionTimingInfo.domainLookupStartTime || 0 |
| 37 | + finalConnectionTimingInfo.domainLookupEndTime = |
| 38 | + finalConnectionTimingInfo.domainLookupEndTime || 0 |
| 39 | + finalConnectionTimingInfo.connectionStartTime = |
| 40 | + finalConnectionTimingInfo.connectionStartTime || 0 |
| 41 | + finalConnectionTimingInfo.connectionEndTime = |
| 42 | + finalConnectionTimingInfo.connectionEndTime || 0 |
| 43 | + finalConnectionTimingInfo.secureConnectionStartTime = |
| 44 | + finalConnectionTimingInfo.secureConnectionStartTime || 0 |
| 45 | + finalConnectionTimingInfo.ALPNNegotiatedProtocol = |
| 46 | + finalConnectionTimingInfo.ALPNNegotiatedProtocol || [] |
| 47 | + } |
| 48 | + return { |
| 49 | + startTime, |
| 50 | + redirectStartTime, |
| 51 | + redirectEndTime, |
| 52 | + postRedirectStartTime, |
| 53 | + finalServiceWorkerStartTime, |
| 54 | + finalNetworkRequestStartTime, |
| 55 | + finalNetworkResponseStartTime, |
| 56 | + endTime, |
| 57 | + encodedBodySize, |
| 58 | + decodedBodySize, |
| 59 | + finalConnectionTimingInfo, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +{ |
| 64 | + const obs = new PerformanceObserver(common.mustCall((list) => { |
| 65 | + { |
| 66 | + const entries = list.getEntries(); |
| 67 | + assert.strictEqual(entries.length, 1); |
| 68 | + } |
| 69 | + { |
| 70 | + const entries = list.getEntriesByType('resource'); |
| 71 | + assert.strictEqual(entries.length, 1); |
| 72 | + } |
| 73 | + { |
| 74 | + const entries = list.getEntriesByName('http://localhost:8080'); |
| 75 | + assert.strictEqual(entries.length, 1); |
| 76 | + } |
| 77 | + obs.disconnect(); |
| 78 | + })); |
| 79 | + obs.observe({ entryTypes: ['resource'] }); |
| 80 | + |
| 81 | + const timingInfo = createTimingInfo({ finalConnectionTimingInfo: {} }); |
| 82 | + const customGlobal = {}; |
| 83 | + const requestedUrl = 'http://localhost:8080'; |
| 84 | + const cacheMode = 'local'; |
| 85 | + const initiatorType = 'fetch'; |
| 86 | + const resource = markResourceTiming( |
| 87 | + timingInfo, |
| 88 | + requestedUrl, |
| 89 | + initiatorType, |
| 90 | + customGlobal, |
| 91 | + cacheMode, |
| 92 | + ); |
| 93 | + |
| 94 | + assert(resource instanceof PerformanceEntry); |
| 95 | + assert(resource instanceof PerformanceResourceTiming); |
| 96 | + |
| 97 | + assert.strictEqual(resource.entryType, 'resource'); |
| 98 | + assert.strictEqual(resource.name, requestedUrl); |
| 99 | + assert.ok(typeof resource.cacheMode === 'undefined', 'cacheMode does not have a getter'); |
| 100 | + assert.strictEqual(resource.startTime, timingInfo.startTime); |
| 101 | + assert.strictEqual(resource.duration, 0); |
| 102 | + assert.strictEqual(resource.workerStart, 0); |
| 103 | + assert.strictEqual(resource.redirectStart, 0); |
| 104 | + assert.strictEqual(resource.redirectEnd, 0); |
| 105 | + assert.strictEqual(resource.fetchStart, 0); |
| 106 | + assert.strictEqual(resource.domainLookupStart, 0); |
| 107 | + assert.strictEqual(resource.domainLookupEnd, 0); |
| 108 | + assert.strictEqual(resource.connectStart, 0); |
| 109 | + assert.strictEqual(resource.connectEnd, 0); |
| 110 | + assert.strictEqual(resource.secureConnectionStart, 0); |
| 111 | + assert.deepEqual(resource.nextHopProtocol, []); |
| 112 | + assert.strictEqual(resource.requestStart, 0); |
| 113 | + assert.strictEqual(resource.responseStart, 0); |
| 114 | + assert.strictEqual(resource.responseEnd, 0); |
| 115 | + assert.strictEqual(resource.encodedBodySize, 0); |
| 116 | + assert.strictEqual(resource.decodedBodySize, 0); |
| 117 | + assert.strictEqual(resource.transferSize, 0); |
| 118 | + assert.deepEqual(resource.toJSON(), { |
| 119 | + initiatorType, |
| 120 | + requestedUrl, |
| 121 | + cacheMode, |
| 122 | + name: requestedUrl, |
| 123 | + entryType: 'resource', |
| 124 | + startTime: timingInfo.startTime, |
| 125 | + duration: 0 |
| 126 | + }); |
| 127 | +} |
0 commit comments