-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Inject Lighthouse metrics back into trace for viewing #1446
Changes from 1 commit
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 |
---|---|---|
@@ -0,0 +1,204 @@ | ||
/** | ||
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. rename to indicate progressive web metrics? 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. renamed. sg |
||
* @license | ||
* Copyright 2016 Google Inc. All rights reserved. | ||
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. 2017 |
||
* | ||
* 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'; | ||
|
||
const log = require('../../../lighthouse-core/lib/log.js'); | ||
|
||
class Metrics { | ||
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. throw in a |
||
|
||
constructor(traceEvents, auditResults) { | ||
this._traceEvents = traceEvents; | ||
this._auditResults = auditResults; | ||
this._fakeEvents = []; | ||
} | ||
|
||
/** | ||
* Returns simplified representation of all metrics | ||
* @return {!Array<{getTs: Function, id: string, name: string}>} metrics to consider | ||
*/ | ||
static get metricsDefinitions() { | ||
return [ | ||
{ | ||
name: 'Navigation Start', | ||
id: 'navstart', | ||
getTs: auditResults => { | ||
const fmpExt = auditResults['first-meaningful-paint'].extendedInfo; | ||
return fmpExt.value.timestamps.navStart; | ||
} | ||
}, | ||
{ | ||
name: 'First Contentful Paint', | ||
id: 'ttfcp', | ||
getTs: auditResults => { | ||
const fmpExt = auditResults['first-meaningful-paint'].extendedInfo; | ||
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. Everything coming from 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. Essentially we have 3 actual audits in lighthouse that collect these metrics. And yeah we stash several key metrics within the extInfo of each of them. 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. was expecting all the |
||
return fmpExt.value.timestamps.fCP; | ||
} | ||
}, | ||
{ | ||
name: 'First Meaningful Paint', | ||
id: 'ttfmp', | ||
getTs: auditResults => { | ||
const fmpExt = auditResults['first-meaningful-paint'].extendedInfo; | ||
return fmpExt.value.timestamps.fMP; | ||
} | ||
}, | ||
{ | ||
name: 'Perceptual Speed Index', | ||
id: 'psi', | ||
getTs: auditResults => { | ||
const siExt = auditResults['speed-index-metric'].extendedInfo; | ||
return siExt.value.timestamps.perceptualSpeedIndex; | ||
} | ||
}, | ||
{ | ||
name: 'First Visual Change', | ||
id: 'fv', | ||
getTs: auditResults => { | ||
const siExt = auditResults['speed-index-metric'].extendedInfo; | ||
return siExt.value.timestamps.firstVisualChange; | ||
} | ||
}, | ||
{ | ||
name: 'Visually Complete 85%', | ||
id: 'vc85', | ||
getTs: auditResults => { | ||
const siExt = auditResults['time-to-interactive'].extendedInfo; | ||
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. Actually, check that all of these are right. Now I'm really confused :) 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. actually vc85 does actually get delivered on the TTI choo-choo train. :) |
||
return siExt.value.timestamps.visuallyReady; | ||
} | ||
}, | ||
{ | ||
name: 'Visually Complete 100%', | ||
id: 'vc', | ||
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. suggest : 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. sounds good. |
||
getTs: auditResults => { | ||
const siExt = auditResults['speed-index-metric'].extendedInfo; | ||
return siExt.value.timestamps.visuallyComplete; | ||
} | ||
}, | ||
{ | ||
name: 'Time to Interactive', | ||
id: 'tti', | ||
getTs: auditResults => { | ||
const ttiExt = auditResults['time-to-interactive'].extendedInfo; | ||
return ttiExt.value.timestamps.timeToInteractive; | ||
} | ||
} | ||
]; | ||
} | ||
|
||
/** | ||
* Returns simplified representation of all metrics' timestamps from monotonic clock | ||
* @param {!Array<!AuditResult>} auditResults | ||
* @return {!Array<{ts: number, id: string, name: string}>} metrics to consider | ||
*/ | ||
gatherMetrics(auditResults) { | ||
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. make static? (or can use |
||
const metricDfns = Metrics.metricsDefinitions; | ||
const resolvedMetrics = []; | ||
metricDfns.forEach(metric => { | ||
// try/catch in case auditResults is missing an audit result | ||
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. an -> in |
||
try { | ||
resolvedMetrics.push({ | ||
id: metric.id, | ||
name: metric.name, | ||
ts: metric.getTs(auditResults) | ||
}); | ||
} catch (e) { | ||
log.error('metrics-evts', `${metric.name} timestamp not found: ${e.message}`); | ||
} | ||
}); | ||
return resolvedMetrics; | ||
} | ||
|
||
/** | ||
* Getter for our navigationStart trace event | ||
*/ | ||
get navigationStartEvt() { | ||
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. make just Could also be an internal function that the constructor calls, populating the |
||
if (!this._navigationStartEvt) { | ||
const filteredEvents = this._traceEvents.filter(e => { | ||
return e.name === 'TracingStartedInPage' || e.cat === 'blink.user_timing'; | ||
}); | ||
|
||
const tracingStartedEvt = filteredEvents.filter(e => e.name === 'TracingStartedInPage')[0]; | ||
const navigationStartEvt = filteredEvents.filter(e => { | ||
return e.name === 'navigationStart' && | ||
e.pid === tracingStartedEvt.pid && e.tid === tracingStartedEvt.tid; | ||
})[0]; | ||
this._navigationStartEvt = navigationStartEvt; | ||
} | ||
return this._navigationStartEvt; | ||
} | ||
|
||
|
||
/** | ||
* Constructs performance.measure trace events, which have start/end events as follows: | ||
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. is "ph: b", beginning? If so, maybe say 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. actually "b" stands for "nestable start" :D |
||
* { "pid": 89922,"tid":1295,"ts":77176783452,"ph":"b","cat":"blink.user_timing","name":"innermeasure","args":{},"tts":1257886,"id":"0xe66c67"} | ||
* { "pid": 89922,"tid":1295,"ts":77176882592,"ph":"e","cat":"blink.user_timing","name":"innermeasure","args":{},"tts":1257898,"id":"0xe66c67"} | ||
* @param {!Object} metric | ||
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. can do more specific |
||
* @param {number} navStartTs | ||
* @return {Array} Pair of trace events (start/end) | ||
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.
|
||
*/ | ||
synthesizeEventPair(metric, navStartTs) { | ||
if (!metric.ts || metric.id === 'navstart') { | ||
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. one alternative: could filter these out in the 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. sg for both. |
||
return []; | ||
} | ||
|
||
// We'll masquerade our fake events to look mostly like navigationStart | ||
const eventBase = { | ||
pid: this.navigationStartEvt.pid, | ||
tid: this.navigationStartEvt.tid, | ||
cat: 'blink.user_timing', | ||
name: metric.name, | ||
args: {}, | ||
// randomized id is same for the pair | ||
id: `0x${((Math.random() * 1000000) | 0).toString(16)}` | ||
}; | ||
const fakeMeasureStartEvent = Object.assign({}, eventBase, { | ||
ts: navStartTs, | ||
ph: 'b' | ||
}); | ||
const fakeMeasureEndEvent = Object.assign({}, eventBase, { | ||
ts: metric.ts, | ||
ph: 'e', | ||
}); | ||
return [fakeMeasureStartEvent, fakeMeasureEndEvent]; | ||
} | ||
|
||
/** | ||
* @returns {Array} User timing raw trace event pairs | ||
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.
|
||
*/ | ||
generateFakeEvents() { | ||
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. not sure if you have more plans for this class, but these could all pretty easily be static. Caching |
||
const metrics = this.gatherMetrics(this._auditResults); | ||
if (metrics.length === 0) { | ||
log.error('metrics-events', 'Metrics collection had errors, not synthetizing trace events'); | ||
return []; | ||
} | ||
|
||
// confirm our navStart's correctly match | ||
const navStartTs = metrics.find(e => e.id === 'navstart').ts; | ||
if (this.navigationStartEvt.ts !== navStartTs) { | ||
log.error('metrics-evts', 'Reference navigationStart doesn\'t match fMP\'s navStart'); | ||
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. great candidate for online error reporting :) |
||
return []; | ||
} | ||
|
||
metrics.forEach(metric => { | ||
log.log('metrics-evts', `Sythesizing trace events for ${metric.name}`); | ||
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.
|
||
this._fakeEvents.push(...this.synthesizeEventPair(metric, navStartTs)); | ||
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 purpose to keeping this as a member variable? If you call |
||
}); | ||
return this._fakeEvents; | ||
} | ||
} | ||
|
||
module.exports = Metrics; |
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.
It seems bad to alter the original trace. Not sure when we did
const traceData = Object.assign({}, trace);
, but really we should be doing something liketraceData.traceEvents = Array.from(traceData.traceEvents)
to get a new array. Is that crazy?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.
On one hand, yeah, it feels bad to alter the trace. It'd be nice to keep the objects separate.
On the other:
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.
very good point :)