-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
tbt-impact-tasks.js
244 lines (197 loc) · 8.31 KB
/
tbt-impact-tasks.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import * as Lantern from '../lib/lantern/lantern.js';
import {makeComputedArtifact} from './computed-artifact.js';
import {MainThreadTasks} from './main-thread-tasks.js';
import {FirstContentfulPaint} from './metrics/first-contentful-paint.js';
import {Interactive} from './metrics/interactive.js';
import {TotalBlockingTime} from './metrics/total-blocking-time.js';
import {ProcessedTrace} from './processed-trace.js';
const {calculateTbtImpactForEvent} = Lantern.Metrics.TBTUtils;
class TBTImpactTasks {
/**
* @param {LH.Artifacts.TaskNode} task
* @return {LH.Artifacts.TaskNode}
*/
static getTopLevelTask(task) {
let topLevelTask = task;
while (topLevelTask.parent) {
topLevelTask = topLevelTask.parent;
}
return topLevelTask;
}
/**
* @param {LH.Artifacts.MetricComputationDataInput} metricComputationData
* @param {LH.Artifacts.ComputedContext} context
* @return {Promise<{startTimeMs: number, endTimeMs: number}>}
*/
static async getTbtBounds(metricComputationData, context) {
const processedTrace = await ProcessedTrace.request(metricComputationData.trace, context);
if (metricComputationData.gatherContext.gatherMode !== 'navigation') {
return {
startTimeMs: 0,
endTimeMs: processedTrace.timings.traceEnd,
};
}
const fcpResult = await FirstContentfulPaint.request(metricComputationData, context);
const ttiResult = await Interactive.request(metricComputationData, context);
let startTimeMs = fcpResult.timing;
let endTimeMs = ttiResult.timing;
// When using lantern, we want to get a pessimistic view of the long tasks.
// This means we assume the earliest possible start time and latest possible end time.
if ('optimisticEstimate' in fcpResult) {
startTimeMs = fcpResult.optimisticEstimate.timeInMs;
}
if ('pessimisticEstimate' in ttiResult) {
endTimeMs = ttiResult.pessimisticEstimate.timeInMs;
}
return {startTimeMs, endTimeMs};
}
/**
* @param {LH.Artifacts.TaskNode[]} tasks
* @param {Map<LH.Artifacts.TaskNode, number>} taskToImpact
* @param {Map<LH.Artifacts.TaskNode, number>} taskToBlockingTime
*/
static createImpactTasks(tasks, taskToImpact, taskToBlockingTime) {
/** @type {LH.Artifacts.TBTImpactTask[]} */
const tbtImpactTasks = [];
for (const task of tasks) {
const tbtImpact = taskToImpact.get(task) || 0;
let selfTbtImpact = tbtImpact;
const blockingTime = taskToBlockingTime.get(task) || 0;
let selfBlockingTime = blockingTime;
for (const child of task.children) {
const childTbtImpact = taskToImpact.get(child) || 0;
selfTbtImpact -= childTbtImpact;
const childBlockingTime = taskToBlockingTime.get(child) || 0;
selfBlockingTime -= childBlockingTime;
}
tbtImpactTasks.push({
...task,
// Floating point numbers are not perfectly precise, so the subtraction operations above
// can sometimes output negative numbers close to 0 here. To prevent potentially confusing
// output we should bump those values to 0.
tbtImpact: Math.max(tbtImpact, 0),
selfTbtImpact: Math.max(selfTbtImpact, 0),
selfBlockingTime: Math.max(selfBlockingTime, 0),
});
}
return tbtImpactTasks;
}
/**
* @param {LH.Artifacts.TaskNode[]} tasks
* @param {number} startTimeMs
* @param {number} endTimeMs
* @return {LH.Artifacts.TBTImpactTask[]}
*/
static computeImpactsFromObservedTasks(tasks, startTimeMs, endTimeMs) {
/** @type {Map<LH.Artifacts.TaskNode, number>} */
const taskToImpact = new Map();
/** @type {Map<LH.Artifacts.TaskNode, number>} */
const taskToBlockingTime = new Map();
for (const task of tasks) {
const event = {
start: task.startTime,
end: task.endTime,
duration: task.duration,
};
const topLevelTask = this.getTopLevelTask(task);
const topLevelEvent = {
start: topLevelTask.startTime,
end: topLevelTask.endTime,
duration: topLevelTask.duration,
};
const tbtImpact = calculateTbtImpactForEvent(event, startTimeMs, endTimeMs, topLevelEvent);
const blockingTime = calculateTbtImpactForEvent(event, -Infinity, Infinity, topLevelEvent);
taskToImpact.set(task, tbtImpact);
taskToBlockingTime.set(task, blockingTime);
}
return this.createImpactTasks(tasks, taskToImpact, taskToBlockingTime);
}
/**
* @param {LH.Artifacts.TaskNode[]} tasks
* @param {LH.Gatherer.Simulation.Result['nodeTimings']} tbtNodeTimings
* @param {number} startTimeMs
* @param {number} endTimeMs
* @return {LH.Artifacts.TBTImpactTask[]}
*/
static computeImpactsFromLantern(tasks, tbtNodeTimings, startTimeMs, endTimeMs) {
/** @type {Map<LH.Artifacts.TaskNode, number>} */
const taskToImpact = new Map();
/** @type {Map<LH.Artifacts.TaskNode, number>} */
const taskToBlockingTime = new Map();
/** @type {Map<LH.Artifacts.TaskNode, {start: number, end: number, duration: number}>} */
const topLevelTaskToEvent = new Map();
/** @type {Map<Lantern.Types.TraceEvent, LH.Artifacts.TaskNode>} */
const traceEventToTask = new Map();
for (const task of tasks) {
traceEventToTask.set(task.event, task);
}
// Use lantern TBT timings to calculate the TBT impact of top level tasks.
for (const [node, timing] of tbtNodeTimings) {
if (node.type !== 'cpu') continue;
const event = {
start: timing.startTime,
end: timing.endTime,
duration: timing.duration,
};
const tbtImpact = calculateTbtImpactForEvent(event, startTimeMs, endTimeMs);
const blockingTime = calculateTbtImpactForEvent(event, -Infinity, Infinity);
const task = traceEventToTask.get(node.event);
if (!task) continue;
topLevelTaskToEvent.set(task, event);
taskToImpact.set(task, tbtImpact);
taskToBlockingTime.set(task, blockingTime);
}
// Interpolate the TBT impact of remaining tasks using the top level ancestor tasks.
// We don't have any lantern estimates for tasks that are not top level, so we need to estimate
// the lantern timing based on the task's observed timing relative to it's top level task's observed timing.
for (const task of tasks) {
if (taskToImpact.has(task) || taskToBlockingTime.has(task)) continue;
const topLevelTask = this.getTopLevelTask(task);
const topLevelEvent = topLevelTaskToEvent.get(topLevelTask);
if (!topLevelEvent) continue;
const startRatio = (task.startTime - topLevelTask.startTime) / topLevelTask.duration;
const start = startRatio * topLevelEvent.duration + topLevelEvent.start;
const endRatio = (topLevelTask.endTime - task.endTime) / topLevelTask.duration;
const end = topLevelEvent.end - endRatio * topLevelEvent.duration;
const event = {
start,
end,
duration: end - start,
};
const tbtImpact = calculateTbtImpactForEvent(event, startTimeMs, endTimeMs, topLevelEvent);
const blockingTime = calculateTbtImpactForEvent(event, -Infinity, Infinity, topLevelEvent);
taskToImpact.set(task, tbtImpact);
taskToBlockingTime.set(task, blockingTime);
}
return this.createImpactTasks(tasks, taskToImpact, taskToBlockingTime);
}
/**
* @param {LH.Artifacts.MetricComputationDataInput} metricComputationData
* @param {LH.Artifacts.ComputedContext} context
* @return {Promise<LH.Artifacts.TBTImpactTask[]>}
*/
static async compute_(metricComputationData, context) {
const tbtResult = await TotalBlockingTime.request(metricComputationData, context);
const tasks = await MainThreadTasks.request(metricComputationData.trace, context);
const {startTimeMs, endTimeMs} = await this.getTbtBounds(metricComputationData, context);
if ('pessimisticEstimate' in tbtResult) {
return this.computeImpactsFromLantern(
tasks,
tbtResult.pessimisticEstimate.nodeTimings,
startTimeMs,
endTimeMs
);
}
return this.computeImpactsFromObservedTasks(tasks, startTimeMs, endTimeMs);
}
}
const TBTImpactTasksComputed = makeComputedArtifact(
TBTImpactTasks,
['trace', 'devtoolsLog', 'URL', 'gatherContext', 'settings', 'simulator']
);
export {TBTImpactTasksComputed as TBTImpactTasks};