-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core(tracehouse): split timeOrigin determination out of computeTraceOfTab #11253
Changes from 5 commits
74d3373
f8eaf86
13674bf
202fbff
555839f
9004793
158b901
face33b
ccb3f29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -242,6 +242,11 @@ const ERRORS = { | |
message: UIStrings.badTraceRecording, | ||
lhrRuntimeError: true, | ||
}, | ||
NO_RESOURCE_SEND: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be added to the proto, too, since it's a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah sg might as well do it now too. re: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
it's just short and I was wondering if there's more context we can give it. That's a very good point, though :) "Resource send" is kind of confusing as representing the request for a resource, though ( I'll stop bikeshedding after this :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
sold :) |
||
code: 'NO_RESOURCE_SEND', | ||
message: UIStrings.badTraceRecording, | ||
lhrRuntimeError: true, | ||
}, | ||
NO_NAVSTART: { | ||
code: 'NO_NAVSTART', | ||
message: UIStrings.badTraceRecording, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
|
||
/** @typedef {Omit<LH.Artifacts.TraceTimes, 'firstContentfulPaint'> & {firstContentfulPaint?: number}} TraceTimesWithoutFCP */ | ||
/** @typedef {Omit<LH.Artifacts.TraceOfTab, 'firstContentfulPaintEvt'|'timings'|'timestamps'> & {timings: TraceTimesWithoutFCP, timestamps: TraceTimesWithoutFCP, firstContentfulPaintEvt?: LH.Artifacts.TraceOfTab['firstContentfulPaintEvt']}} TraceOfTabWithoutFCP */ | ||
/** @typedef {Omit<TraceOfTabWithoutFCP, 'frames'|'processEvents'|'mainThreadEvents'|'mainFrameIds'>} FrameTimings */ | ||
/** @typedef {'lastNavigationStart'|'firstResourceSendRequest'} TimeOriginDeterminationMethod */ | ||
patrickhulce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const log = require('lighthouse-logger'); | ||
|
||
|
@@ -43,6 +45,13 @@ class TraceProcessor { | |
return new Error('No navigationStart event found'); | ||
} | ||
|
||
/** | ||
* @return {Error} | ||
*/ | ||
static createNoResourceSendRequestError() { | ||
return new Error('No ResourceSendRequest event found'); | ||
patrickhulce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* @return {Error} | ||
*/ | ||
|
@@ -471,11 +480,14 @@ class TraceProcessor { | |
|
||
/** | ||
* 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. | ||
* in milliseconds since the time origin in addition to the standard microsecond monotonic timestamps. | ||
* @param {LH.Trace} trace | ||
* @param {{timeOriginDeterminationMethod?: TimeOriginDeterminationMethod}} [options] | ||
* @return {TraceOfTabWithoutFCP} | ||
*/ | ||
static computeTraceOfTab(trace) { | ||
static computeTraceOfTab(trace, options) { | ||
const {timeOriginDeterminationMethod = 'lastNavigationStart'} = options || {}; | ||
|
||
// Parse the trace for our key events and sort them by timestamp. Note: sort | ||
// *must* be stable to keep events correctly nested. | ||
const keyEvents = this.filteredTraceSort(trace.traceEvents, e => { | ||
|
@@ -491,10 +503,140 @@ class TraceProcessor { | |
// Filter to just events matching the frame ID, just to make sure. | ||
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(this._isNavigationStartOfInterest).pop(); | ||
if (!navigationStart) throw this.createNoNavstartError(); | ||
const timeOriginEvt = navigationStart; | ||
// Compute our time origin to use for all relative timings. | ||
const timeOriginEvt = this.computeTimeOrigin( | ||
{keyEvents, frameEvents, mainFrameIds}, | ||
timeOriginDeterminationMethod | ||
); | ||
|
||
// Compute the key frame timings for the main frame. | ||
const frameTimings = this.computeKeyTimingsForFrame(frameEvents, {timeOriginEvt}); | ||
|
||
// 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 = TraceProcessor | ||
.filteredTraceSort(trace.traceEvents, e => e.pid === mainFrameIds.pid); | ||
|
||
const mainThreadEvents = processEvents | ||
.filter(e => e.tid === mainFrameIds.tid); | ||
|
||
const frames = keyEvents | ||
.filter(evt => evt.name === 'FrameCommittedInBrowser') | ||
.map(evt => evt.args.data) | ||
.filter(/** @return {data is {frame: string, url: string}} */ data => { | ||
return Boolean(data && data.frame && data.url); | ||
}); | ||
|
||
// Ensure our traceEnd reflects all page activity. | ||
const traceEnd = this.computeTraceEnd(trace.traceEvents, timeOriginEvt); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason not to just pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As in pass both frameEvents and all trace events? Primary reason I did not do that is it violates the veil of abstraction that If the double computation is problematic I'd prefer to modify the return type of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's not fatally problematic, no more so than breaking the veil :) And definitely not worried about the computation cost. I was just curious about your thinking on which was less confusing, passing in both sets of events or fake computing it and then doing it for real. Breaking the type down might be worse but it's possible it would be ok...e.g. with an inferred return type on I'm also trying to be more literal in my review comments. I was 95% sure you had already considered this question so I was 95% really just asking, and 5% suggesting considering it and then just asking as a consideration follow up :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I like it SG! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Just noticed |
||
|
||
// This could be much more concise with object spread, but the consensus is that explicitness is | ||
// preferred over brevity here. | ||
patrickhulce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return { | ||
frames, | ||
mainThreadEvents, | ||
processEvents, | ||
mainFrameIds, | ||
timings: { | ||
timeOrigin: frameTimings.timings.timeOrigin, | ||
firstPaint: frameTimings.timings.firstPaint, | ||
firstContentfulPaint: frameTimings.timings.firstContentfulPaint, | ||
firstMeaningfulPaint: frameTimings.timings.firstMeaningfulPaint, | ||
largestContentfulPaint: frameTimings.timings.largestContentfulPaint, | ||
traceEnd: traceEnd.timing, | ||
load: frameTimings.timings.load, | ||
domContentLoaded: frameTimings.timings.domContentLoaded, | ||
}, | ||
timestamps: { | ||
timeOrigin: frameTimings.timestamps.timeOrigin, | ||
firstPaint: frameTimings.timestamps.firstPaint, | ||
firstContentfulPaint: frameTimings.timestamps.firstContentfulPaint, | ||
firstMeaningfulPaint: frameTimings.timestamps.firstMeaningfulPaint, | ||
largestContentfulPaint: frameTimings.timestamps.largestContentfulPaint, | ||
traceEnd: traceEnd.timestamp, | ||
load: frameTimings.timestamps.load, | ||
domContentLoaded: frameTimings.timestamps.domContentLoaded, | ||
}, | ||
timeOriginEvt: frameTimings.timeOriginEvt, | ||
firstPaintEvt: frameTimings.firstPaintEvt, | ||
firstContentfulPaintEvt: frameTimings.firstContentfulPaintEvt, | ||
firstMeaningfulPaintEvt: frameTimings.firstMeaningfulPaintEvt, | ||
largestContentfulPaintEvt: frameTimings.largestContentfulPaintEvt, | ||
loadEvt: frameTimings.loadEvt, | ||
domContentLoadedEvt: frameTimings.domContentLoadedEvt, | ||
fmpFellBack: frameTimings.fmpFellBack, | ||
lcpInvalidated: frameTimings.lcpInvalidated, | ||
}; | ||
} | ||
|
||
/** | ||
* Computes the last observable timestamp in a set of trace events. | ||
* | ||
* @param {Array<LH.TraceEvent>} events | ||
* @param {LH.TraceEvent} timeOriginEvt | ||
* @return {{timing: number, timestamp: number}} | ||
*/ | ||
static computeTraceEnd(events, timeOriginEvt) { | ||
let maxTs = -Infinity; | ||
for (const event of events) { | ||
maxTs = Math.max(event.ts + (event.dur || 0), maxTs); | ||
} | ||
|
||
return {timestamp: maxTs, timing: (maxTs - timeOriginEvt.ts) / 1000}; | ||
} | ||
|
||
/** | ||
* Computes the time origin using the specified method. | ||
* | ||
* - firstResourceSendRequest | ||
* Uses the time that the very first network request is sent in the main frame. | ||
* Eventually should be used in place of lastNavigationStart as the default for navigations. | ||
* This method includes the cost of all redirects when evaluating a navigation (which matches lantern behavior). | ||
* The only difference between firstResourceSendRequest and the first `navigationStart` is | ||
* the unload time of `about:blank` (which is a Lighthouse implementation detail and shouldn't be included). | ||
* | ||
* - lastNavigationStart | ||
* Uses the time of the last `navigationStart` event in the main frame. | ||
* The historical time origin of Lighthouse from 2016-Present. | ||
* This method excludes the cost of client-side redirects when evaluating a navigation. | ||
* Can also be skewed by several hundred milliseconds or even seconds when the browser takes a long | ||
* time to unload `about:blank`. | ||
* | ||
* @param {{keyEvents: Array<LH.TraceEvent>, frameEvents: Array<LH.TraceEvent>, mainFrameIds: {frameId: string}}} traceEventSubsets | ||
* @param {TimeOriginDeterminationMethod} method | ||
* @return {LH.TraceEvent} | ||
*/ | ||
static computeTimeOrigin(traceEventSubsets, method) { | ||
switch (method) { | ||
case 'firstResourceSendRequest': { | ||
// Our time origin will be the timestamp of the first request that's sent in the frame. | ||
const fetchStart = traceEventSubsets.keyEvents.find(event => { | ||
if (event.name !== 'ResourceSendRequest') return false; | ||
const data = event.args.data || {}; | ||
return data.frame === traceEventSubsets.mainFrameIds.frameId; | ||
}); | ||
if (!fetchStart) throw this.createNoResourceSendRequestError(); | ||
return fetchStart; | ||
} | ||
case 'lastNavigationStart': { | ||
// Our time origin will be the last frame navigation in the trace | ||
const frameEvents = traceEventSubsets.frameEvents; | ||
const navigationStart = frameEvents.filter(this._isNavigationStartOfInterest).pop(); | ||
if (!navigationStart) throw this.createNoNavstartError(); | ||
return navigationStart; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Computes timings of trace events of key trace events in milliseconds since the time origin | ||
* in addition to the standard microsecond monotonic timestamps. | ||
* @param {Array<LH.TraceEvent>} frameEvents | ||
* @param {{timeOriginEvt: LH.TraceEvent}} options | ||
* @return {FrameTimings} | ||
*/ | ||
static computeKeyTimingsForFrame(frameEvents, options) { | ||
const {timeOriginEvt} = options; | ||
|
||
// Find our first paint of this frame | ||
const firstPaint = frameEvents.find(e => e.name === 'firstPaint' && e.ts > timeOriginEvt.ts); | ||
|
@@ -553,20 +695,7 @@ class TraceProcessor { | |
e => e.name === 'domContentLoadedEventEnd' && e.ts > timeOriginEvt.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 = TraceProcessor | ||
.filteredTraceSort(trace.traceEvents, e => e.pid === mainFrameIds.pid); | ||
|
||
const mainThreadEvents = processEvents | ||
.filter(e => e.tid === mainFrameIds.tid); | ||
|
||
// traceEnd must exist since at least timeOrigin 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)}; | ||
|
||
const traceEndEvt = this.computeTraceEnd(frameEvents, timeOriginEvt); | ||
/** @param {{ts: number}=} event */ | ||
const getTimestamp = (event) => event && event.ts; | ||
/** @type {TraceTimesWithoutFCP} */ | ||
|
@@ -576,41 +705,28 @@ class TraceProcessor { | |
firstContentfulPaint: getTimestamp(firstContentfulPaint), | ||
firstMeaningfulPaint: getTimestamp(firstMeaningfulPaint), | ||
largestContentfulPaint: getTimestamp(largestContentfulPaint), | ||
traceEnd: fakeEndOfTraceEvt.ts, | ||
traceEnd: traceEndEvt.timestamp, | ||
load: getTimestamp(load), | ||
domContentLoaded: getTimestamp(domContentLoaded), | ||
}; | ||
|
||
/** @param {number} ts */ | ||
const getTiming = (ts) => (ts - timeOriginEvt.ts) / 1000; | ||
/** @param {number=} ts */ | ||
const maybeGetTiming = (ts) => ts === undefined ? undefined : getTiming(ts); | ||
const maybeGetTiming = (ts) => ts === undefined ? undefined : (ts - timeOriginEvt.ts) / 1000; | ||
/** @type {TraceTimesWithoutFCP} */ | ||
const timings = { | ||
timeOrigin: 0, | ||
firstPaint: maybeGetTiming(timestamps.firstPaint), | ||
firstContentfulPaint: maybeGetTiming(timestamps.firstContentfulPaint), | ||
firstMeaningfulPaint: maybeGetTiming(timestamps.firstMeaningfulPaint), | ||
largestContentfulPaint: maybeGetTiming(timestamps.largestContentfulPaint), | ||
traceEnd: getTiming(timestamps.traceEnd), | ||
traceEnd: traceEndEvt.timing, | ||
load: maybeGetTiming(timestamps.load), | ||
domContentLoaded: maybeGetTiming(timestamps.domContentLoaded), | ||
}; | ||
|
||
const frames = keyEvents | ||
.filter(evt => evt.name === 'FrameCommittedInBrowser') | ||
.map(evt => evt.args.data) | ||
.filter(/** @return {data is {frame: string, url: string}} */ data => { | ||
return Boolean(data && data.frame && data.url); | ||
}); | ||
|
||
return { | ||
timings, | ||
timestamps, | ||
processEvents, | ||
mainThreadEvents, | ||
mainFrameIds, | ||
frames, | ||
timeOriginEvt: timeOriginEvt, | ||
firstPaintEvt: firstPaint, | ||
firstContentfulPaintEvt: firstContentfulPaint, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol if this is true (and there aren't concrete FR plans for it), why are we refactoring and adding
'firstResourceSendRequest'
? Why not refactor but have'lastNavigationStart'
be the only option right now?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, looking at the excellent comment on
computeTimeOrigin
, maybe the simpler thing is to change this to something more concrete like(the
@see
doesn't actually work like for that yet, but will soon(!), though if the thing isn't imported, I don't know if you actually have to do something like@see {import('../../path/to/trace-processor.js').computeTimeOrigin}
or what)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
firstResourceSendRequest won't be used for FR, but will be used for the #8984 line of work that will continue amidst FR.
That's another option but a motivating example to validate the split seemed worthwhile to pair with the refactor given its simplicity and test uninterestingness if there's only one option :)
sg 👍