-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathinteractive.js
194 lines (174 loc) · 7.17 KB
/
interactive.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
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {makeComputedArtifact} from '../computed-artifact.js';
import {NavigationMetric} from './navigation-metric.js';
import {LanternInteractive} from './lantern-interactive.js';
import {NetworkMonitor} from '../../gather/driver/network-monitor.js';
import {TraceProcessor} from '../../lib/tracehouse/trace-processor.js';
import {LighthouseError} from '../../lib/lh-error.js';
const REQUIRED_QUIET_WINDOW = 5000;
const ALLOWED_CONCURRENT_REQUESTS = 2;
/**
* @fileoverview Computes "Time To Interactive", the time at which the page has loaded critical
* resources and is mostly idle.
* @see https://docs.google.com/document/d/1yE4YWsusi5wVXrnwhR61j-QyjK9tzENIzfxrCjA1NAk/edit#heading=h.yozfsuqcgpc4
*/
class Interactive extends NavigationMetric {
/**
* Finds all time periods where the number of inflight requests is less than or equal to the
* number of allowed concurrent requests (2).
* @param {Array<LH.Artifacts.NetworkRequest>} networkRecords
* @param {{timestamps: {traceEnd: number}}} processedNavigation
* @return {Array<TimePeriod>}
*/
static _findNetworkQuietPeriods(networkRecords, processedNavigation) {
const traceEndTsInMs = processedNavigation.timestamps.traceEnd / 1000;
// Ignore records that failed, never finished, or were POST/PUT/etc.
const filteredNetworkRecords = networkRecords.filter(record => {
return record.finished && record.requestMethod === 'GET' && !record.failed &&
// Consider network records that had 4xx/5xx status code as "failed"
record.statusCode < 400;
});
return NetworkMonitor.findNetworkQuietPeriods(filteredNetworkRecords,
ALLOWED_CONCURRENT_REQUESTS, traceEndTsInMs);
}
/**
* Finds all time periods where there are no long tasks.
* @param {Array<TimePeriod>} longTasks
* @param {{timestamps: {timeOrigin: number, traceEnd: number}}} processedNavigation
* @return {Array<TimePeriod>}
*/
static _findCPUQuietPeriods(longTasks, processedNavigation) {
const timeOriginTsInMs = processedNavigation.timestamps.timeOrigin / 1000;
const traceEndTsInMs = processedNavigation.timestamps.traceEnd / 1000;
if (longTasks.length === 0) {
return [{start: 0, end: traceEndTsInMs}];
}
/** @type {Array<TimePeriod>} */
const quietPeriods = [];
longTasks.forEach((task, index) => {
if (index === 0) {
quietPeriods.push({
start: 0,
end: task.start + timeOriginTsInMs,
});
}
if (index === longTasks.length - 1) {
quietPeriods.push({
start: task.end + timeOriginTsInMs,
end: traceEndTsInMs,
});
} else {
quietPeriods.push({
start: task.end + timeOriginTsInMs,
end: longTasks[index + 1].start + timeOriginTsInMs,
});
}
});
return quietPeriods;
}
/**
* Finds the first time period where a network quiet period and a CPU quiet period overlap.
* @param {Array<TimePeriod>} longTasks
* @param {Array<LH.Artifacts.NetworkRequest>} networkRecords
* @param {LH.Artifacts.ProcessedNavigation} processedNavigation
* @return {{cpuQuietPeriod: TimePeriod, networkQuietPeriod: TimePeriod, cpuQuietPeriods: Array<TimePeriod>, networkQuietPeriods: Array<TimePeriod>}}
*/
static findOverlappingQuietPeriods(longTasks, networkRecords, processedNavigation) {
const minTime = processedNavigation.timestamps.largestContentfulPaint !== undefined ?
processedNavigation.timestamps.largestContentfulPaint / 1000 :
processedNavigation.timestamps.firstContentfulPaint / 1000;
/** @type {function(TimePeriod):boolean} */
const isLongEnoughQuietPeriod = period =>
period.end > minTime + REQUIRED_QUIET_WINDOW &&
period.end - period.start >= REQUIRED_QUIET_WINDOW;
const networkQuietPeriods = this._findNetworkQuietPeriods(networkRecords, processedNavigation)
.filter(isLongEnoughQuietPeriod);
const cpuQuietPeriods = this._findCPUQuietPeriods(longTasks, processedNavigation)
.filter(isLongEnoughQuietPeriod);
const cpuQueue = cpuQuietPeriods.slice();
const networkQueue = networkQuietPeriods.slice();
// We will check for a CPU quiet period contained within a Network quiet period or vice-versa
let cpuCandidate = cpuQueue.shift();
let networkCandidate = networkQueue.shift();
while (cpuCandidate && networkCandidate) {
if (cpuCandidate.start >= networkCandidate.start) {
// CPU starts later than network, window must be contained by network or we check the next
if (networkCandidate.end >= cpuCandidate.start + REQUIRED_QUIET_WINDOW) {
return {
cpuQuietPeriod: cpuCandidate,
networkQuietPeriod: networkCandidate,
cpuQuietPeriods,
networkQuietPeriods,
};
} else {
networkCandidate = networkQueue.shift();
}
} else {
// Network starts later than CPU, window must be contained by CPU or we check the next
if (cpuCandidate.end >= networkCandidate.start + REQUIRED_QUIET_WINDOW) {
return {
cpuQuietPeriod: cpuCandidate,
networkQuietPeriod: networkCandidate,
cpuQuietPeriods,
networkQuietPeriods,
};
} else {
cpuCandidate = cpuQueue.shift();
}
}
}
throw new LighthouseError(
cpuCandidate
? LighthouseError.errors.NO_TTI_NETWORK_IDLE_PERIOD
: LighthouseError.errors.NO_TTI_CPU_IDLE_PERIOD
);
}
/**
* @param {LH.Artifacts.NavigationMetricComputationData} data
* @param {LH.Artifacts.ComputedContext} context
* @return {Promise<LH.Artifacts.LanternMetric>}
*/
static computeSimulatedMetric(data, context) {
const metricData = NavigationMetric.getMetricComputationInput(data);
return LanternInteractive.request(metricData, context);
}
/**
* @param {LH.Artifacts.NavigationMetricComputationData} data
* @return {Promise<LH.Artifacts.Metric>}
*/
static computeObservedMetric(data) {
const {processedTrace, processedNavigation, networkRecords} = data;
if (!processedNavigation.timestamps.domContentLoaded) {
throw new LighthouseError(LighthouseError.errors.NO_DCL);
}
const longTasks = TraceProcessor.getMainThreadTopLevelEvents(processedTrace)
.filter(event => event.duration >= 50);
const quietPeriodInfo = Interactive.findOverlappingQuietPeriods(
longTasks,
networkRecords,
processedNavigation
);
const cpuQuietPeriod = quietPeriodInfo.cpuQuietPeriod;
const timestamp = Math.max(
cpuQuietPeriod.start,
processedNavigation.timestamps.firstContentfulPaint / 1000,
processedNavigation.timestamps.domContentLoaded / 1000
) * 1000;
const timing = (timestamp - processedNavigation.timestamps.timeOrigin) / 1000;
return Promise.resolve({timing, timestamp});
}
}
const InteractiveComputed = makeComputedArtifact(
Interactive,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace', 'URL']
);
export {InteractiveComputed as Interactive};
/**
* @typedef TimePeriod
* @property {number} start In ms.
* @property {number} end In ms.
*/