lighthouse 11.6.0-dev.20240304 → 11.6.0-dev.20240305
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/audits/prioritize-lcp-image.d.ts +1 -1
- package/core/computed/load-simulator.d.ts +3 -1
- package/core/computed/load-simulator.js +3 -1
- package/core/computed/metrics/lantern-metric.d.ts +1 -1
- package/core/computed/page-dependency-graph.d.ts +1 -54
- package/core/computed/page-dependency-graph.js +3 -450
- package/core/lib/lantern/lantern.d.ts +0 -1
- package/core/lib/lantern/lantern.js +0 -3
- package/core/lib/lantern/network-node.d.ts +5 -4
- package/core/lib/lantern/network-node.js +7 -5
- package/core/lib/lantern/page-dependency-graph.d.ts +58 -0
- package/core/lib/lantern/page-dependency-graph.js +463 -0
- package/core/lib/lantern/simulator/connection-pool.d.ts +15 -14
- package/core/lib/lantern/simulator/connection-pool.js +9 -8
- package/core/lib/lantern/simulator/dns-cache.d.ts +11 -4
- package/core/lib/lantern/simulator/dns-cache.js +3 -3
- package/core/lib/lantern/simulator/network-analyzer.d.ts +30 -29
- package/core/lib/lantern/simulator/network-analyzer.js +15 -14
- package/core/lib/lantern/simulator/simulator.d.ts +20 -14
- package/core/lib/lantern/simulator/simulator.js +19 -11
- package/core/lib/network-recorder.js +1 -1
- package/core/lib/network-request.d.ts +4 -2
- package/core/lib/network-request.js +3 -2
- package/package.json +1 -1
- package/tsconfig.json +1 -0
- package/types/gatherer.d.ts +8 -35
- package/types/internal/lantern.d.ts +115 -73
|
@@ -39,7 +39,7 @@ declare class PrioritizeLcpImage extends Audit {
|
|
|
39
39
|
* @return {{lcpNodeToPreload?: LH.Gatherer.Simulation.GraphNetworkNode, initiatorPath?: InitiatorPath}}
|
|
40
40
|
*/
|
|
41
41
|
static getLCPNodeToPreload(mainResource: LH.Artifacts.NetworkRequest, graph: LH.Gatherer.Simulation.GraphNode, lcpRecord: NetworkRequest | undefined): {
|
|
42
|
-
lcpNodeToPreload?: import("
|
|
42
|
+
lcpNodeToPreload?: import("../lib/lantern/network-node.js").NetworkNode<NetworkRequest> | undefined;
|
|
43
43
|
initiatorPath?: InitiatorPath | undefined;
|
|
44
44
|
};
|
|
45
45
|
/**
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
export { LoadSimulatorComputed as LoadSimulator };
|
|
2
|
+
export type SimulationOptions = import('../../types/internal/lantern.js').Lantern.Simulation.Options;
|
|
2
3
|
declare const LoadSimulatorComputed: typeof LoadSimulator & {
|
|
3
4
|
request: (dependencies: {
|
|
4
5
|
devtoolsLog: import("../index.js").DevtoolsLog;
|
|
5
6
|
settings: LH.Audit.Context['settings'];
|
|
6
7
|
}, context: import("../../types/utility-types.js").default.ImmutableObject<{
|
|
7
8
|
computedCache: Map<string, import("../lib/arbitrary-equality-map.js").ArbitraryEqualityMap>;
|
|
8
|
-
}>) => Promise<Simulator
|
|
9
|
+
}>) => Promise<Simulator<any>>;
|
|
9
10
|
};
|
|
11
|
+
/** @typedef {import('../../types/internal/lantern.js').Lantern.Simulation.Options} SimulationOptions */
|
|
10
12
|
declare class LoadSimulator {
|
|
11
13
|
/**
|
|
12
14
|
* @param {{devtoolsLog: LH.DevtoolsLog, settings: LH.Audit.Context['settings']}} data
|
|
@@ -9,6 +9,8 @@ import * as constants from '../config/constants.js';
|
|
|
9
9
|
import {Simulator} from '../lib/lantern/simulator/simulator.js';
|
|
10
10
|
import {NetworkAnalysis} from './network-analysis.js';
|
|
11
11
|
|
|
12
|
+
/** @typedef {import('../../types/internal/lantern.js').Lantern.Simulation.Options} SimulationOptions */
|
|
13
|
+
|
|
12
14
|
class LoadSimulator {
|
|
13
15
|
/**
|
|
14
16
|
* @param {{devtoolsLog: LH.DevtoolsLog, settings: LH.Audit.Context['settings']}} data
|
|
@@ -19,7 +21,7 @@ class LoadSimulator {
|
|
|
19
21
|
const {throttlingMethod, throttling, precomputedLanternData} = data.settings;
|
|
20
22
|
const networkAnalysis = await NetworkAnalysis.request(data.devtoolsLog, context);
|
|
21
23
|
|
|
22
|
-
/** @type {
|
|
24
|
+
/** @type {SimulationOptions} */
|
|
23
25
|
const options = {
|
|
24
26
|
additionalRttByOrigin: networkAnalysis.additionalRttByOrigin,
|
|
25
27
|
serverResponseTimeByOrigin: networkAnalysis.serverResponseTimeByOrigin,
|
|
@@ -31,7 +31,7 @@ export class LanternMetric {
|
|
|
31
31
|
/**
|
|
32
32
|
* @return {LH.Gatherer.Simulation.MetricCoefficients}
|
|
33
33
|
*/
|
|
34
|
-
static get COEFFICIENTS(): import("../../../types/
|
|
34
|
+
static get COEFFICIENTS(): import("../../../types/internal/lantern.js").Lantern.Simulation.MetricCoefficients;
|
|
35
35
|
/**
|
|
36
36
|
* Returns the coefficients, scaled by the throttling settings if needed by the metric.
|
|
37
37
|
* Some lantern metrics (speed-index) use components in their estimate that are not
|
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
export { PageDependencyGraphComputed as PageDependencyGraph };
|
|
2
2
|
export type Node = import('../lib/lantern/base-node.js').Node<LH.Artifacts.NetworkRequest>;
|
|
3
|
-
export type URLArtifact = Omit<LH.Artifacts['URL'], 'finalDisplayedUrl'>;
|
|
4
|
-
export type NetworkNodeOutput = {
|
|
5
|
-
nodes: Array<NetworkNode<LH.Artifacts.NetworkRequest>>;
|
|
6
|
-
idToNodeMap: Map<string, NetworkNode<LH.Artifacts.NetworkRequest>>;
|
|
7
|
-
urlToNodeMap: Map<string, Array<NetworkNode<LH.Artifacts.NetworkRequest>>>;
|
|
8
|
-
frameIdToNodeMap: Map<string, NetworkNode<LH.Artifacts.NetworkRequest> | null>;
|
|
9
|
-
};
|
|
10
3
|
declare const PageDependencyGraphComputed: typeof PageDependencyGraph & {
|
|
11
4
|
request: (dependencies: {
|
|
12
5
|
trace: LH.Trace;
|
|
@@ -17,52 +10,8 @@ declare const PageDependencyGraphComputed: typeof PageDependencyGraph & {
|
|
|
17
10
|
}>) => Promise<Node>;
|
|
18
11
|
};
|
|
19
12
|
import { NetworkRequest } from '../lib/network-request.js';
|
|
20
|
-
|
|
13
|
+
/** @typedef {import('../lib/lantern/base-node.js').Node<LH.Artifacts.NetworkRequest>} Node */
|
|
21
14
|
declare class PageDependencyGraph {
|
|
22
|
-
/**
|
|
23
|
-
* @param {LH.Artifacts.NetworkRequest} record
|
|
24
|
-
* @return {Array<string>}
|
|
25
|
-
*/
|
|
26
|
-
static getNetworkInitiators(record: LH.Artifacts.NetworkRequest): Array<string>;
|
|
27
|
-
/**
|
|
28
|
-
* @param {Array<LH.Artifacts.NetworkRequest>} networkRecords
|
|
29
|
-
* @return {NetworkNodeOutput}
|
|
30
|
-
*/
|
|
31
|
-
static getNetworkNodeOutput(networkRecords: Array<LH.Artifacts.NetworkRequest>): NetworkNodeOutput;
|
|
32
|
-
/**
|
|
33
|
-
* @param {LH.Artifacts.ProcessedTrace} processedTrace
|
|
34
|
-
* @return {Array<CPUNode>}
|
|
35
|
-
*/
|
|
36
|
-
static getCPUNodes({ mainThreadEvents }: LH.Artifacts.ProcessedTrace): Array<CPUNode>;
|
|
37
|
-
/**
|
|
38
|
-
* @param {NetworkNode} rootNode
|
|
39
|
-
* @param {NetworkNodeOutput} networkNodeOutput
|
|
40
|
-
*/
|
|
41
|
-
static linkNetworkNodes(rootNode: NetworkNode, networkNodeOutput: NetworkNodeOutput): void;
|
|
42
|
-
/**
|
|
43
|
-
* @param {Node} rootNode
|
|
44
|
-
* @param {NetworkNodeOutput} networkNodeOutput
|
|
45
|
-
* @param {Array<CPUNode>} cpuNodes
|
|
46
|
-
*/
|
|
47
|
-
static linkCPUNodes(rootNode: Node, networkNodeOutput: NetworkNodeOutput, cpuNodes: Array<CPUNode>): void;
|
|
48
|
-
/**
|
|
49
|
-
* Removes the given node from the graph, but retains all paths between its dependencies and
|
|
50
|
-
* dependents.
|
|
51
|
-
* @param {Node} node
|
|
52
|
-
*/
|
|
53
|
-
static _pruneNode(node: Node): void;
|
|
54
|
-
/**
|
|
55
|
-
* @param {LH.Artifacts.ProcessedTrace} processedTrace
|
|
56
|
-
* @param {Array<LH.Artifacts.NetworkRequest>} networkRecords
|
|
57
|
-
* @param {URLArtifact} URL
|
|
58
|
-
* @return {Node}
|
|
59
|
-
*/
|
|
60
|
-
static createGraph(processedTrace: LH.Artifacts.ProcessedTrace, networkRecords: Array<LH.Artifacts.NetworkRequest>, URL: URLArtifact): Node;
|
|
61
|
-
/**
|
|
62
|
-
*
|
|
63
|
-
* @param {Node} rootNode
|
|
64
|
-
*/
|
|
65
|
-
static printGraph(rootNode: Node, widthInCharacters?: number): void;
|
|
66
15
|
/**
|
|
67
16
|
* @param {{trace: LH.Trace, devtoolsLog: LH.DevtoolsLog, URL: LH.Artifacts['URL']}} data
|
|
68
17
|
* @param {LH.Artifacts.ComputedContext} context
|
|
@@ -74,6 +23,4 @@ declare class PageDependencyGraph {
|
|
|
74
23
|
URL: LH.Artifacts['URL'];
|
|
75
24
|
}, context: LH.Artifacts.ComputedContext): Promise<Node>;
|
|
76
25
|
}
|
|
77
|
-
import { ProcessedTrace } from './processed-trace.js';
|
|
78
|
-
import { CPUNode } from '../lib/lantern/cpu-node.js';
|
|
79
26
|
//# sourceMappingURL=page-dependency-graph.d.ts.map
|
|
@@ -5,463 +5,15 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import {makeComputedArtifact} from './computed-artifact.js';
|
|
8
|
-
import {
|
|
9
|
-
import {CPUNode} from '../lib/lantern/cpu-node.js';
|
|
10
|
-
import {TraceProcessor} from '../lib/tracehouse/trace-processor.js';
|
|
8
|
+
import {PageDependencyGraph as LanternPageDependencyGraph} from '../lib/lantern/page-dependency-graph.js';
|
|
11
9
|
import {NetworkRequest} from '../lib/network-request.js';
|
|
12
10
|
import {ProcessedTrace} from './processed-trace.js';
|
|
13
11
|
import {NetworkRecords} from './network-records.js';
|
|
14
|
-
import {NetworkAnalyzer} from '../lib/lantern/simulator/network-analyzer.js';
|
|
15
12
|
import {DocumentUrls} from './document-urls.js';
|
|
16
13
|
|
|
17
14
|
/** @typedef {import('../lib/lantern/base-node.js').Node<LH.Artifacts.NetworkRequest>} Node */
|
|
18
|
-
/** @typedef {Omit<LH.Artifacts['URL'], 'finalDisplayedUrl'>} URLArtifact */
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @typedef {Object} NetworkNodeOutput
|
|
22
|
-
* @property {Array<NetworkNode<LH.Artifacts.NetworkRequest>>} nodes
|
|
23
|
-
* @property {Map<string, NetworkNode<LH.Artifacts.NetworkRequest>>} idToNodeMap
|
|
24
|
-
* @property {Map<string, Array<NetworkNode<LH.Artifacts.NetworkRequest>>>} urlToNodeMap
|
|
25
|
-
* @property {Map<string, NetworkNode<LH.Artifacts.NetworkRequest>|null>} frameIdToNodeMap
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
// Shorter tasks have negligible impact on simulation results.
|
|
29
|
-
const SIGNIFICANT_DUR_THRESHOLD_MS = 10;
|
|
30
|
-
|
|
31
|
-
// TODO: video files tend to be enormous and throw off all graph traversals, move this ignore
|
|
32
|
-
// into estimation logic when we use the dependency graph for other purposes.
|
|
33
|
-
const IGNORED_MIME_TYPES_REGEX = /^video/;
|
|
34
15
|
|
|
35
16
|
class PageDependencyGraph {
|
|
36
|
-
/**
|
|
37
|
-
* @param {LH.Artifacts.NetworkRequest} record
|
|
38
|
-
* @return {Array<string>}
|
|
39
|
-
*/
|
|
40
|
-
static getNetworkInitiators(record) {
|
|
41
|
-
if (!record.initiator) return [];
|
|
42
|
-
if (record.initiator.url) return [record.initiator.url];
|
|
43
|
-
if (record.initiator.type === 'script') {
|
|
44
|
-
// Script initiators have the stack of callFrames from all functions that led to this request.
|
|
45
|
-
// If async stacks are enabled, then the stack will also have the parent functions that asynchronously
|
|
46
|
-
// led to this request chained in the `parent` property.
|
|
47
|
-
/** @type {Set<string>} */
|
|
48
|
-
const scriptURLs = new Set();
|
|
49
|
-
let stack = record.initiator.stack;
|
|
50
|
-
while (stack) {
|
|
51
|
-
const callFrames = stack.callFrames || [];
|
|
52
|
-
for (const frame of callFrames) {
|
|
53
|
-
if (frame.url) scriptURLs.add(frame.url);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
stack = stack.parent;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return Array.from(scriptURLs);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return [];
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* @param {Array<LH.Artifacts.NetworkRequest>} networkRecords
|
|
67
|
-
* @return {NetworkNodeOutput}
|
|
68
|
-
*/
|
|
69
|
-
static getNetworkNodeOutput(networkRecords) {
|
|
70
|
-
/** @type {Array<NetworkNode>} */
|
|
71
|
-
const nodes = [];
|
|
72
|
-
/** @type {Map<string, NetworkNode>} */
|
|
73
|
-
const idToNodeMap = new Map();
|
|
74
|
-
/** @type {Map<string, Array<NetworkNode>>} */
|
|
75
|
-
const urlToNodeMap = new Map();
|
|
76
|
-
/** @type {Map<string, NetworkNode|null>} */
|
|
77
|
-
const frameIdToNodeMap = new Map();
|
|
78
|
-
|
|
79
|
-
networkRecords.forEach(record => {
|
|
80
|
-
if (IGNORED_MIME_TYPES_REGEX.test(record.mimeType)) return;
|
|
81
|
-
if (record.sessionTargetType === 'worker') return;
|
|
82
|
-
|
|
83
|
-
// Network record requestIds can be duplicated for an unknown reason
|
|
84
|
-
// Suffix all subsequent records with `:duplicate` until it's unique
|
|
85
|
-
// NOTE: This should never happen with modern NetworkRequest library, but old fixtures
|
|
86
|
-
// might still have this issue.
|
|
87
|
-
while (idToNodeMap.has(record.requestId)) {
|
|
88
|
-
record.requestId += ':duplicate';
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const node = new NetworkNode(NetworkRequest.asLanternNetworkRequest(record));
|
|
92
|
-
nodes.push(node);
|
|
93
|
-
|
|
94
|
-
const urlList = urlToNodeMap.get(record.url) || [];
|
|
95
|
-
urlList.push(node);
|
|
96
|
-
|
|
97
|
-
idToNodeMap.set(record.requestId, node);
|
|
98
|
-
urlToNodeMap.set(record.url, urlList);
|
|
99
|
-
|
|
100
|
-
// If the request was for the root document of an iframe, save an entry in our
|
|
101
|
-
// map so we can link up the task `args.data.frame` dependencies later in graph creation.
|
|
102
|
-
if (record.frameId &&
|
|
103
|
-
record.resourceType === NetworkRequest.TYPES.Document &&
|
|
104
|
-
record.documentURL === record.url) {
|
|
105
|
-
// If there's ever any ambiguity, permanently set the value to `false` to avoid loops in the graph.
|
|
106
|
-
const value = frameIdToNodeMap.has(record.frameId) ? null : node;
|
|
107
|
-
frameIdToNodeMap.set(record.frameId, value);
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
return {nodes, idToNodeMap, urlToNodeMap, frameIdToNodeMap};
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* @param {LH.Artifacts.ProcessedTrace} processedTrace
|
|
116
|
-
* @return {Array<CPUNode>}
|
|
117
|
-
*/
|
|
118
|
-
static getCPUNodes({mainThreadEvents}) {
|
|
119
|
-
/** @type {Array<CPUNode>} */
|
|
120
|
-
const nodes = [];
|
|
121
|
-
let i = 0;
|
|
122
|
-
|
|
123
|
-
TraceProcessor.assertHasToplevelEvents(mainThreadEvents);
|
|
124
|
-
|
|
125
|
-
while (i < mainThreadEvents.length) {
|
|
126
|
-
const evt = mainThreadEvents[i];
|
|
127
|
-
i++;
|
|
128
|
-
|
|
129
|
-
// Skip all trace events that aren't schedulable tasks with sizable duration
|
|
130
|
-
if (!TraceProcessor.isScheduleableTask(evt) || !evt.dur) {
|
|
131
|
-
continue;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Capture all events that occurred within the task
|
|
135
|
-
/** @type {Array<LH.TraceEvent>} */
|
|
136
|
-
const children = [];
|
|
137
|
-
for (
|
|
138
|
-
const endTime = evt.ts + evt.dur;
|
|
139
|
-
i < mainThreadEvents.length && mainThreadEvents[i].ts < endTime;
|
|
140
|
-
i++
|
|
141
|
-
) {
|
|
142
|
-
children.push(mainThreadEvents[i]);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
nodes.push(new CPUNode(evt, children));
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return nodes;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* @param {NetworkNode} rootNode
|
|
153
|
-
* @param {NetworkNodeOutput} networkNodeOutput
|
|
154
|
-
*/
|
|
155
|
-
static linkNetworkNodes(rootNode, networkNodeOutput) {
|
|
156
|
-
networkNodeOutput.nodes.forEach(node => {
|
|
157
|
-
const directInitiatorRequest = node.record.initiatorRequest || rootNode.record;
|
|
158
|
-
const directInitiatorNode =
|
|
159
|
-
networkNodeOutput.idToNodeMap.get(directInitiatorRequest.requestId) || rootNode;
|
|
160
|
-
const canDependOnInitiator =
|
|
161
|
-
!directInitiatorNode.isDependentOn(node) &&
|
|
162
|
-
node.canDependOn(directInitiatorNode);
|
|
163
|
-
const initiators = PageDependencyGraph.getNetworkInitiators(node.record);
|
|
164
|
-
if (initiators.length) {
|
|
165
|
-
initiators.forEach(initiator => {
|
|
166
|
-
const parentCandidates = networkNodeOutput.urlToNodeMap.get(initiator) || [];
|
|
167
|
-
// Only add the edge if the parent is unambiguous with valid timing and isn't circular.
|
|
168
|
-
if (parentCandidates.length === 1 &&
|
|
169
|
-
parentCandidates[0].startTime <= node.startTime &&
|
|
170
|
-
!parentCandidates[0].isDependentOn(node)) {
|
|
171
|
-
node.addDependency(parentCandidates[0]);
|
|
172
|
-
} else if (canDependOnInitiator) {
|
|
173
|
-
directInitiatorNode.addDependent(node);
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
} else if (canDependOnInitiator) {
|
|
177
|
-
directInitiatorNode.addDependent(node);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Make sure the nodes are attached to the graph if the initiator information was invalid.
|
|
181
|
-
if (node !== rootNode && node.getDependencies().length === 0 && node.canDependOn(rootNode)) {
|
|
182
|
-
node.addDependency(rootNode);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if (!node.record.redirects) return;
|
|
186
|
-
|
|
187
|
-
const redirects = [...node.record.redirects, node.record];
|
|
188
|
-
for (let i = 1; i < redirects.length; i++) {
|
|
189
|
-
const redirectNode = networkNodeOutput.idToNodeMap.get(redirects[i - 1].requestId);
|
|
190
|
-
const actualNode = networkNodeOutput.idToNodeMap.get(redirects[i].requestId);
|
|
191
|
-
if (actualNode && redirectNode) {
|
|
192
|
-
actualNode.addDependency(redirectNode);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* @param {Node} rootNode
|
|
200
|
-
* @param {NetworkNodeOutput} networkNodeOutput
|
|
201
|
-
* @param {Array<CPUNode>} cpuNodes
|
|
202
|
-
*/
|
|
203
|
-
static linkCPUNodes(rootNode, networkNodeOutput, cpuNodes) {
|
|
204
|
-
/** @type {Set<LH.Crdp.Network.ResourceType|undefined>} */
|
|
205
|
-
const linkableResourceTypes = new Set([
|
|
206
|
-
NetworkRequest.TYPES.XHR, NetworkRequest.TYPES.Fetch, NetworkRequest.TYPES.Script,
|
|
207
|
-
]);
|
|
208
|
-
|
|
209
|
-
/** @param {CPUNode} cpuNode @param {string} reqId */
|
|
210
|
-
function addDependentNetworkRequest(cpuNode, reqId) {
|
|
211
|
-
const networkNode = networkNodeOutput.idToNodeMap.get(reqId);
|
|
212
|
-
if (!networkNode ||
|
|
213
|
-
// Ignore all network nodes that started before this CPU task started
|
|
214
|
-
// A network request that started earlier could not possibly have been started by this task
|
|
215
|
-
networkNode.startTime <= cpuNode.startTime) return;
|
|
216
|
-
const {record} = networkNode;
|
|
217
|
-
const resourceType = record.resourceType ||
|
|
218
|
-
record.redirectDestination?.resourceType;
|
|
219
|
-
if (!linkableResourceTypes.has(resourceType)) {
|
|
220
|
-
// We only link some resources to CPU nodes because we observe LCP simulation
|
|
221
|
-
// regressions when including images, etc.
|
|
222
|
-
return;
|
|
223
|
-
}
|
|
224
|
-
cpuNode.addDependent(networkNode);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* If the node has an associated frameId, then create a dependency on the root document request
|
|
229
|
-
* for the frame. The task obviously couldn't have started before the frame was even downloaded.
|
|
230
|
-
*
|
|
231
|
-
* @param {CPUNode} cpuNode
|
|
232
|
-
* @param {string|undefined} frameId
|
|
233
|
-
*/
|
|
234
|
-
function addDependencyOnFrame(cpuNode, frameId) {
|
|
235
|
-
if (!frameId) return;
|
|
236
|
-
const networkNode = networkNodeOutput.frameIdToNodeMap.get(frameId);
|
|
237
|
-
if (!networkNode) return;
|
|
238
|
-
// Ignore all network nodes that started after this CPU task started
|
|
239
|
-
// A network request that started after could not possibly be required this task
|
|
240
|
-
if (networkNode.startTime >= cpuNode.startTime) return;
|
|
241
|
-
cpuNode.addDependency(networkNode);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/** @param {CPUNode} cpuNode @param {string} url */
|
|
245
|
-
function addDependencyOnUrl(cpuNode, url) {
|
|
246
|
-
if (!url) return;
|
|
247
|
-
// Allow network requests that end up to 100ms before the task started
|
|
248
|
-
// Some script evaluations can start before the script finishes downloading
|
|
249
|
-
const minimumAllowableTimeSinceNetworkNodeEnd = -100 * 1000;
|
|
250
|
-
const candidates = networkNodeOutput.urlToNodeMap.get(url) || [];
|
|
251
|
-
|
|
252
|
-
let minCandidate = null;
|
|
253
|
-
let minDistance = Infinity;
|
|
254
|
-
// Find the closest request that finished before this CPU task started
|
|
255
|
-
for (const candidate of candidates) {
|
|
256
|
-
// Explicitly ignore all requests that started after this CPU node
|
|
257
|
-
// A network request that started after this task started cannot possibly be a dependency
|
|
258
|
-
if (cpuNode.startTime <= candidate.startTime) return;
|
|
259
|
-
|
|
260
|
-
const distance = cpuNode.startTime - candidate.endTime;
|
|
261
|
-
if (distance >= minimumAllowableTimeSinceNetworkNodeEnd && distance < minDistance) {
|
|
262
|
-
minCandidate = candidate;
|
|
263
|
-
minDistance = distance;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
if (!minCandidate) return;
|
|
268
|
-
cpuNode.addDependency(minCandidate);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/** @type {Map<string, CPUNode>} */
|
|
272
|
-
const timers = new Map();
|
|
273
|
-
for (const node of cpuNodes) {
|
|
274
|
-
for (const evt of node.childEvents) {
|
|
275
|
-
if (!evt.args.data) continue;
|
|
276
|
-
|
|
277
|
-
const argsUrl = evt.args.data.url;
|
|
278
|
-
const stackTraceUrls = (evt.args.data.stackTrace || []).map(l => l.url).filter(Boolean);
|
|
279
|
-
|
|
280
|
-
switch (evt.name) {
|
|
281
|
-
case 'TimerInstall':
|
|
282
|
-
// @ts-expect-error - 'TimerInstall' event means timerId exists.
|
|
283
|
-
timers.set(evt.args.data.timerId, node);
|
|
284
|
-
stackTraceUrls.forEach(url => addDependencyOnUrl(node, url));
|
|
285
|
-
break;
|
|
286
|
-
case 'TimerFire': {
|
|
287
|
-
// @ts-expect-error - 'TimerFire' event means timerId exists.
|
|
288
|
-
const installer = timers.get(evt.args.data.timerId);
|
|
289
|
-
if (!installer || installer.endTime > node.startTime) break;
|
|
290
|
-
installer.addDependent(node);
|
|
291
|
-
break;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
case 'InvalidateLayout':
|
|
295
|
-
case 'ScheduleStyleRecalculation':
|
|
296
|
-
addDependencyOnFrame(node, evt.args.data.frame);
|
|
297
|
-
stackTraceUrls.forEach(url => addDependencyOnUrl(node, url));
|
|
298
|
-
break;
|
|
299
|
-
|
|
300
|
-
case 'EvaluateScript':
|
|
301
|
-
addDependencyOnFrame(node, evt.args.data.frame);
|
|
302
|
-
// @ts-expect-error - 'EvaluateScript' event means argsUrl is defined.
|
|
303
|
-
addDependencyOnUrl(node, argsUrl);
|
|
304
|
-
stackTraceUrls.forEach(url => addDependencyOnUrl(node, url));
|
|
305
|
-
break;
|
|
306
|
-
|
|
307
|
-
case 'XHRReadyStateChange':
|
|
308
|
-
// Only create the dependency if the request was completed
|
|
309
|
-
// 'XHRReadyStateChange' event means readyState is defined.
|
|
310
|
-
if (evt.args.data.readyState !== 4) break;
|
|
311
|
-
|
|
312
|
-
// @ts-expect-error - 'XHRReadyStateChange' event means argsUrl is defined.
|
|
313
|
-
addDependencyOnUrl(node, argsUrl);
|
|
314
|
-
stackTraceUrls.forEach(url => addDependencyOnUrl(node, url));
|
|
315
|
-
break;
|
|
316
|
-
|
|
317
|
-
case 'FunctionCall':
|
|
318
|
-
case 'v8.compile':
|
|
319
|
-
addDependencyOnFrame(node, evt.args.data.frame);
|
|
320
|
-
// @ts-expect-error - events mean argsUrl is defined.
|
|
321
|
-
addDependencyOnUrl(node, argsUrl);
|
|
322
|
-
break;
|
|
323
|
-
|
|
324
|
-
case 'ParseAuthorStyleSheet':
|
|
325
|
-
addDependencyOnFrame(node, evt.args.data.frame);
|
|
326
|
-
// @ts-expect-error - 'ParseAuthorStyleSheet' event means styleSheetUrl is defined.
|
|
327
|
-
addDependencyOnUrl(node, evt.args.data.styleSheetUrl);
|
|
328
|
-
break;
|
|
329
|
-
|
|
330
|
-
case 'ResourceSendRequest':
|
|
331
|
-
addDependencyOnFrame(node, evt.args.data.frame);
|
|
332
|
-
// @ts-expect-error - 'ResourceSendRequest' event means requestId is defined.
|
|
333
|
-
addDependentNetworkRequest(node, evt.args.data.requestId);
|
|
334
|
-
stackTraceUrls.forEach(url => addDependencyOnUrl(node, url));
|
|
335
|
-
break;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
// Nodes starting before the root node cannot depend on it.
|
|
340
|
-
if (node.getNumberOfDependencies() === 0 && node.canDependOn(rootNode)) {
|
|
341
|
-
node.addDependency(rootNode);
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
// Second pass to prune the graph of short tasks.
|
|
346
|
-
const minimumEvtDur = SIGNIFICANT_DUR_THRESHOLD_MS * 1000;
|
|
347
|
-
let foundFirstLayout = false;
|
|
348
|
-
let foundFirstPaint = false;
|
|
349
|
-
let foundFirstParse = false;
|
|
350
|
-
|
|
351
|
-
for (const node of cpuNodes) {
|
|
352
|
-
// Don't prune if event is the first ParseHTML/Layout/Paint.
|
|
353
|
-
// See https://github.com/GoogleChrome/lighthouse/issues/9627#issuecomment-526699524 for more.
|
|
354
|
-
let isFirst = false;
|
|
355
|
-
if (!foundFirstLayout && node.childEvents.some(evt => evt.name === 'Layout')) {
|
|
356
|
-
isFirst = foundFirstLayout = true;
|
|
357
|
-
}
|
|
358
|
-
if (!foundFirstPaint && node.childEvents.some(evt => evt.name === 'Paint')) {
|
|
359
|
-
isFirst = foundFirstPaint = true;
|
|
360
|
-
}
|
|
361
|
-
if (!foundFirstParse && node.childEvents.some(evt => evt.name === 'ParseHTML')) {
|
|
362
|
-
isFirst = foundFirstParse = true;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
if (isFirst || node.event.dur >= minimumEvtDur) {
|
|
366
|
-
// Don't prune this node. The task is long / important so it will impact simulation.
|
|
367
|
-
continue;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
// Prune the node if it isn't highly connected to minimize graph size. Rewiring the graph
|
|
371
|
-
// here replaces O(M + N) edges with (M * N) edges, which is fine if either M or N is at
|
|
372
|
-
// most 1.
|
|
373
|
-
if (node.getNumberOfDependencies() === 1 || node.getNumberOfDependents() <= 1) {
|
|
374
|
-
PageDependencyGraph._pruneNode(node);
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
/**
|
|
380
|
-
* Removes the given node from the graph, but retains all paths between its dependencies and
|
|
381
|
-
* dependents.
|
|
382
|
-
* @param {Node} node
|
|
383
|
-
*/
|
|
384
|
-
static _pruneNode(node) {
|
|
385
|
-
const dependencies = node.getDependencies();
|
|
386
|
-
const dependents = node.getDependents();
|
|
387
|
-
for (const dependency of dependencies) {
|
|
388
|
-
node.removeDependency(dependency);
|
|
389
|
-
for (const dependent of dependents) {
|
|
390
|
-
dependency.addDependent(dependent);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
for (const dependent of dependents) {
|
|
394
|
-
node.removeDependent(dependent);
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
/**
|
|
399
|
-
* @param {LH.Artifacts.ProcessedTrace} processedTrace
|
|
400
|
-
* @param {Array<LH.Artifacts.NetworkRequest>} networkRecords
|
|
401
|
-
* @param {URLArtifact} URL
|
|
402
|
-
* @return {Node}
|
|
403
|
-
*/
|
|
404
|
-
static createGraph(processedTrace, networkRecords, URL) {
|
|
405
|
-
const networkNodeOutput = PageDependencyGraph.getNetworkNodeOutput(networkRecords);
|
|
406
|
-
const cpuNodes = PageDependencyGraph.getCPUNodes(processedTrace);
|
|
407
|
-
const {requestedUrl, mainDocumentUrl} = URL;
|
|
408
|
-
if (!requestedUrl) throw new Error('requestedUrl is required to get the root request');
|
|
409
|
-
if (!mainDocumentUrl) throw new Error('mainDocumentUrl is required to get the main resource');
|
|
410
|
-
|
|
411
|
-
const rootRequest = NetworkAnalyzer.findResourceForUrl(networkRecords, requestedUrl);
|
|
412
|
-
if (!rootRequest) throw new Error('rootRequest not found');
|
|
413
|
-
const rootNode = networkNodeOutput.idToNodeMap.get(rootRequest.requestId);
|
|
414
|
-
if (!rootNode) throw new Error('rootNode not found');
|
|
415
|
-
|
|
416
|
-
const mainDocumentRequest =
|
|
417
|
-
NetworkAnalyzer.findLastDocumentForUrl(networkRecords, mainDocumentUrl);
|
|
418
|
-
if (!mainDocumentRequest) throw new Error('mainDocumentRequest not found');
|
|
419
|
-
const mainDocumentNode = networkNodeOutput.idToNodeMap.get(mainDocumentRequest.requestId);
|
|
420
|
-
if (!mainDocumentNode) throw new Error('mainDocumentNode not found');
|
|
421
|
-
|
|
422
|
-
PageDependencyGraph.linkNetworkNodes(rootNode, networkNodeOutput);
|
|
423
|
-
PageDependencyGraph.linkCPUNodes(rootNode, networkNodeOutput, cpuNodes);
|
|
424
|
-
mainDocumentNode.setIsMainDocument(true);
|
|
425
|
-
|
|
426
|
-
if (NetworkNode.hasCycle(rootNode)) {
|
|
427
|
-
throw new Error('Invalid dependency graph created, cycle detected');
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
return rootNode;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
/**
|
|
434
|
-
*
|
|
435
|
-
* @param {Node} rootNode
|
|
436
|
-
*/
|
|
437
|
-
static printGraph(rootNode, widthInCharacters = 100) {
|
|
438
|
-
/** @param {string} str @param {number} target */
|
|
439
|
-
function padRight(str, target, padChar = ' ') {
|
|
440
|
-
return str + padChar.repeat(Math.max(target - str.length, 0));
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
/** @type {Array<Node>} */
|
|
444
|
-
const nodes = [];
|
|
445
|
-
rootNode.traverse(node => nodes.push(node));
|
|
446
|
-
nodes.sort((a, b) => a.startTime - b.startTime);
|
|
447
|
-
|
|
448
|
-
const min = nodes[0].startTime;
|
|
449
|
-
const max = nodes.reduce((max, node) => Math.max(max, node.endTime), 0);
|
|
450
|
-
|
|
451
|
-
const totalTime = max - min;
|
|
452
|
-
const timePerCharacter = totalTime / widthInCharacters;
|
|
453
|
-
nodes.forEach(node => {
|
|
454
|
-
const offset = Math.round((node.startTime - min) / timePerCharacter);
|
|
455
|
-
const length = Math.ceil((node.endTime - node.startTime) / timePerCharacter);
|
|
456
|
-
const bar = padRight('', offset) + padRight('', length, '=');
|
|
457
|
-
|
|
458
|
-
// @ts-expect-error -- disambiguate displayName from across possible Node types.
|
|
459
|
-
const displayName = node.record ? node.record.url : node.type;
|
|
460
|
-
// eslint-disable-next-line
|
|
461
|
-
console.log(padRight(bar, widthInCharacters), `| ${displayName.slice(0, 30)}`);
|
|
462
|
-
});
|
|
463
|
-
}
|
|
464
|
-
|
|
465
17
|
/**
|
|
466
18
|
* @param {{trace: LH.Trace, devtoolsLog: LH.DevtoolsLog, URL: LH.Artifacts['URL']}} data
|
|
467
19
|
* @param {LH.Artifacts.ComputedContext} context
|
|
@@ -478,7 +30,8 @@ class PageDependencyGraph {
|
|
|
478
30
|
// Calculates the URL artifact from the processed trace and DT log.
|
|
479
31
|
const URL = data.URL || await DocumentUrls.request(data, context);
|
|
480
32
|
|
|
481
|
-
|
|
33
|
+
const lanternRequests = networkRecords.map(NetworkRequest.asLanternNetworkRequest);
|
|
34
|
+
return LanternPageDependencyGraph.createGraph(processedTrace, lanternRequests, URL);
|
|
482
35
|
}
|
|
483
36
|
}
|
|
484
37
|
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
* Copyright 2024 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
export const NetworkRequest: import("../../../types/internal/lantern").NetworkRequest<any>;
|
|
7
6
|
/** @type {LH.Util.SelfMap<LH.Crdp.Network.ResourceType>} */
|
|
8
7
|
export const NetworkRequestTypes: LH.Util.SelfMap<LH.Crdp.Network.ResourceType>;
|
|
9
8
|
//# sourceMappingURL=lantern.d.ts.map
|
|
@@ -4,8 +4,6 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const NetworkRequest = /** @type {import('../../../types/internal/lantern').NetworkRequest} */({});
|
|
8
|
-
|
|
9
7
|
/** @type {LH.Util.SelfMap<LH.Crdp.Network.ResourceType>} */
|
|
10
8
|
const NetworkRequestTypes = {
|
|
11
9
|
XHR: 'XHR',
|
|
@@ -29,6 +27,5 @@ const NetworkRequestTypes = {
|
|
|
29
27
|
};
|
|
30
28
|
|
|
31
29
|
export {
|
|
32
|
-
NetworkRequest,
|
|
33
30
|
NetworkRequestTypes,
|
|
34
31
|
};
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
export type NetworkRequest<T> = import('../../../types/internal/lantern.js').Lantern.NetworkRequest<T>;
|
|
1
2
|
/**
|
|
2
3
|
* @template [T=any]
|
|
3
4
|
* @extends {BaseNode<T>}
|
|
4
5
|
*/
|
|
5
6
|
export class NetworkNode<T = any> extends BaseNode<T> {
|
|
6
7
|
/**
|
|
7
|
-
* @param {
|
|
8
|
+
* @param {NetworkRequest<T>} networkRequest
|
|
8
9
|
*/
|
|
9
|
-
constructor(networkRequest:
|
|
10
|
+
constructor(networkRequest: NetworkRequest<T>);
|
|
10
11
|
/** @private */
|
|
11
12
|
private _request;
|
|
12
13
|
get type(): "network";
|
|
@@ -15,9 +16,9 @@ export class NetworkNode<T = any> extends BaseNode<T> {
|
|
|
15
16
|
*/
|
|
16
17
|
get record(): Readonly<T>;
|
|
17
18
|
/**
|
|
18
|
-
* @return {
|
|
19
|
+
* @return {NetworkRequest<T>}
|
|
19
20
|
*/
|
|
20
|
-
get request(): import("../../../types/internal/lantern.js").NetworkRequest<
|
|
21
|
+
get request(): import("../../../types/internal/lantern.js").Lantern.NetworkRequest<T>;
|
|
21
22
|
/**
|
|
22
23
|
* @return {string}
|
|
23
24
|
*/
|