-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathtrace-of-tab.js
192 lines (165 loc) · 7.87 KB
/
trace-of-tab.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
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
/**
* @fileoverview Singluar helper to parse a raw trace and extract the most useful data for
* various tools. This artifact will take a trace and then:
*
* 1. Find the TracingStartedInPage and navigationStart events of our intended tab & frame.
* 2. Find the firstContentfulPaint and marked firstMeaningfulPaint events
* 3. Isolate only the trace events from the tab's process (including all threads like compositor)
* * Sort those trace events in chronological order (as order isn't guaranteed)
* 4. Return all those items in one handy bundle.
*/
const makeComputedArtifact = require('./computed-artifact.js');
const log = require('lighthouse-logger');
const TracingProcessor = require('../lib/traces/tracing-processor.js');
const LHError = require('../lib/lh-error.js');
const Sentry = require('../lib/sentry.js');
const ACCEPTABLE_NAVIGATION_URL_REGEX = /^(chrome|https?):/;
class TraceOfTab {
/**
* Returns true if the event is a navigation start event of a document whose URL seems valid.
*
* @param {LH.TraceEvent} event
*/
static isNavigationStartOfInterest(event) {
return event.name === 'navigationStart' &&
(!event.args.data || !event.args.data.documentLoaderURL ||
ACCEPTABLE_NAVIGATION_URL_REGEX.test(event.args.data.documentLoaderURL));
}
/**
* @param {LH.TraceEvent[]} traceEvents
* @param {(e: LH.TraceEvent) => boolean} filter
*/
static filteredStableSort(traceEvents, filter) {
// create an array of the indices that we want to keep
const indices = [];
for (let srcIndex = 0; srcIndex < traceEvents.length; srcIndex++) {
if (filter(traceEvents[srcIndex])) {
indices.push(srcIndex);
}
}
// sort by ts, if there's no ts difference sort by index
indices.sort((indexA, indexB) => {
const result = traceEvents[indexA].ts - traceEvents[indexB].ts;
return result ? result : indexA - indexB;
});
// create a new array using the target indices from previous sort step
const sorted = [];
for (let i = 0; i < indices.length; i++) {
sorted.push(traceEvents[indices[i]]);
}
return sorted;
}
/**
* Finds key trace events, identifies main process/thread, and returns timings of trace events
* in milliseconds since navigation start in addition to the standard microsecond monotonic timestamps.
* @param {LH.Trace} trace
* @return {Promise<LH.Artifacts.TraceOfTab>}
*/
static async compute_(trace) {
// Parse the trace for our key events and sort them by timestamp. Note: sort
// *must* be stable to keep events correctly nested.
const keyEvents = TraceOfTab.filteredStableSort(trace.traceEvents, e => {
return e.cat.includes('blink.user_timing') ||
e.cat.includes('loading') ||
e.cat.includes('devtools.timeline') ||
e.cat === '__metadata';
});
// Find the inspected frame
const mainFrameIds = TracingProcessor.findMainFrameIds(keyEvents);
// Filter to just events matching the frame ID for sanity
const frameEvents = keyEvents.filter(e => e.args.frame === mainFrameIds.frameId);
// Our navStart will be the last frame navigation in the trace
const navigationStart = frameEvents.filter(TraceOfTab.isNavigationStartOfInterest).pop();
if (!navigationStart) throw new LHError(LHError.errors.NO_NAVSTART);
// Find our first paint of this frame
const firstPaint = frameEvents.find(e => e.name === 'firstPaint' && e.ts > navigationStart.ts);
// FCP will follow at/after the FP. Used in so many places we require it.
const firstContentfulPaint = frameEvents.find(
e => e.name === 'firstContentfulPaint' && e.ts > navigationStart.ts
);
if (!firstContentfulPaint) throw new LHError(LHError.errors.NO_FCP);
// fMP will follow at/after the FP
let firstMeaningfulPaint = frameEvents.find(
e => e.name === 'firstMeaningfulPaint' && e.ts > navigationStart.ts
);
let fmpFellBack = false;
// If there was no firstMeaningfulPaint event found in the trace, the network idle detection
// may have not been triggered before Lighthouse finished tracing.
// In this case, we'll use the last firstMeaningfulPaintCandidate we can find.
// However, if no candidates were found (a bogus trace, likely), we fail.
if (!firstMeaningfulPaint) {
// Track this with Sentry since it's likely a bug we should investigate.
Sentry.captureMessage('No firstMeaningfulPaint found, using fallback', {level: 'warning'});
const fmpCand = 'firstMeaningfulPaintCandidate';
fmpFellBack = true;
log.verbose('trace-of-tab', `No firstMeaningfulPaint found, falling back to last ${fmpCand}`);
const lastCandidate = frameEvents.filter(e => e.name === fmpCand).pop();
if (!lastCandidate) {
log.verbose('trace-of-tab', 'No `firstMeaningfulPaintCandidate` events found in trace');
}
firstMeaningfulPaint = lastCandidate;
}
const load = frameEvents.find(e => e.name === 'loadEventEnd' && e.ts > navigationStart.ts);
const domContentLoaded = frameEvents.find(
e => e.name === 'domContentLoadedEventEnd' && e.ts > navigationStart.ts
);
// subset all trace events to just our tab's process (incl threads other than main)
// stable-sort events to keep them correctly nested.
const processEvents = TraceOfTab
.filteredStableSort(trace.traceEvents, e => e.pid === mainFrameIds.pid);
const mainThreadEvents = processEvents
.filter(e => e.tid === mainFrameIds.tid);
// traceEnd must exist since at least navigationStart event was verified as existing.
const traceEnd = trace.traceEvents.reduce((max, evt) => {
return max.ts > evt.ts ? max : evt;
});
const fakeEndOfTraceEvt = {ts: traceEnd.ts + (traceEnd.dur || 0)};
/** @param {{ts: number}=} event */
const getTimestamp = (event) => event && event.ts;
/** @type {LH.Artifacts.TraceTimes} */
const timestamps = {
navigationStart: navigationStart.ts,
firstPaint: getTimestamp(firstPaint),
firstContentfulPaint: firstContentfulPaint.ts,
firstMeaningfulPaint: getTimestamp(firstMeaningfulPaint),
traceEnd: fakeEndOfTraceEvt.ts,
load: getTimestamp(load),
domContentLoaded: getTimestamp(domContentLoaded),
};
/** @param {number} ts */
const getTiming = (ts) => (ts - navigationStart.ts) / 1000;
/** @param {number=} ts */
const maybeGetTiming = (ts) => ts === undefined ? undefined : getTiming(ts);
/** @type {LH.Artifacts.TraceTimes} */
const timings = {
navigationStart: 0,
firstPaint: maybeGetTiming(timestamps.firstPaint),
firstContentfulPaint: getTiming(timestamps.firstContentfulPaint),
firstMeaningfulPaint: maybeGetTiming(timestamps.firstMeaningfulPaint),
traceEnd: getTiming(timestamps.traceEnd),
load: maybeGetTiming(timestamps.load),
domContentLoaded: maybeGetTiming(timestamps.domContentLoaded),
};
return {
timings,
timestamps,
processEvents,
mainThreadEvents,
mainFrameIds,
navigationStartEvt: navigationStart,
firstPaintEvt: firstPaint,
firstContentfulPaintEvt: firstContentfulPaint,
firstMeaningfulPaintEvt: firstMeaningfulPaint,
loadEvt: load,
domContentLoadedEvt: domContentLoaded,
fmpFellBack,
};
}
}
module.exports = makeComputedArtifact(TraceOfTab);