Skip to content
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(largest-contentful-paint): add new metric #9706

Merged
merged 54 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
ec75de3
first pass
connorjclark Sep 20, 2019
183b080
update
connorjclark Sep 20, 2019
03f8ab6
i18n
connorjclark Sep 20, 2019
52a8d28
typos
connorjclark Sep 20, 2019
4e2e688
metrics
connorjclark Sep 20, 2019
f1836a0
ts
connorjclark Sep 20, 2019
449c7c5
fix
connorjclark Sep 20, 2019
d821da7
Apply suggestions from code review
connorjclark Sep 20, 2019
9cc6b8c
todo
connorjclark Sep 20, 2019
211a90b
fix lcp
connorjclark Sep 20, 2019
ee9f34e
update sample trace
connorjclark Sep 20, 2019
24db2ed
tweaks
connorjclark Sep 20, 2019
308f0fe
fix metrics-test
connorjclark Sep 20, 2019
33fecc9
rename all those pwa trace requires
connorjclark Sep 20, 2019
2b87da0
just update all the metric snapshots
connorjclark Sep 20, 2019
c473bf5
undo tests
connorjclark Sep 23, 2019
26bb02a
m79 in metrics test
connorjclark Sep 23, 2019
b848a73
m60
connorjclark Sep 23, 2019
967d630
lcp metric trace test
connorjclark Sep 24, 2019
5b80688
Merge remote-tracking branch 'origin/master' into lcp
connorjclark Sep 27, 2019
9da66f9
pr
connorjclark Sep 27, 2019
49aa30a
Update lighthouse-core/lib/tracehouse/trace-processor.js
connorjclark Sep 27, 2019
a71ab04
Merge branch 'lcp' of github.com:GoogleChrome/lighthouse into lcp
connorjclark Sep 27, 2019
7fd52f4
Apply suggestions from code review
connorjclark Sep 30, 2019
f351590
update traces
connorjclark Oct 1, 2019
ff42528
minify trace
connorjclark Oct 1, 2019
5e158e0
space raceeeeeeee
connorjclark Oct 1, 2019
c2c34e2
add broke test
connorjclark Oct 1, 2019
dbb1cde
invalidLcp for analyis
connorjclark Oct 1, 2019
59b34ae
simple trace test for lcp
connorjclark Oct 1, 2019
4cce545
create invalid lcp trace
connorjclark Oct 1, 2019
694ad7d
fix
connorjclark Oct 1, 2019
d74d208
delete stuff
connorjclark Oct 1, 2019
af84dd4
rm mistake
connorjclark Oct 1, 2019
b41c98f
rm candidateIndex
connorjclark Oct 1, 2019
328209d
test
connorjclark Oct 1, 2019
3173aff
update
connorjclark Oct 1, 2019
f713adc
trace of tab
connorjclark Oct 1, 2019
1424c68
trace of tab
connorjclark Oct 1, 2019
81fcec0
test
connorjclark Oct 1, 2019
9a8c269
push
connorjclark Oct 1, 2019
02843ab
lcpInvalidated
connorjclark Oct 1, 2019
697cf9b
typo
connorjclark Oct 1, 2019
2bb6b26
timings
connorjclark Oct 1, 2019
c80abb6
timestamps
connorjclark Oct 1, 2019
5fa41eb
nolcp
connorjclark Oct 1, 2019
ca65238
Update lighthouse-core/lib/tracehouse/trace-processor.js
connorjclark Oct 1, 2019
5fe7a48
more tests
connorjclark Oct 1, 2019
7c35c8b
Update lighthouse-core/lib/tracehouse/trace-processor.js
connorjclark Oct 1, 2019
9df0ea6
tst
connorjclark Oct 1, 2019
917f73a
Merge branch 'lcp' of github.com:GoogleChrome/lighthouse into lcp
connorjclark Oct 1, 2019
0c0dc97
Update lighthouse-core/audits/metrics.js
connorjclark Oct 1, 2019
6cc68e3
Update lighthouse-core/test/computed/metrics/largest-contentful-paint…
connorjclark Oct 1, 2019
793bfc6
sample
connorjclark Oct 2, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lighthouse-core/audits/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const TraceOfTab = require('../computed/trace-of-tab.js');
const Speedline = require('../computed/speedline.js');
const FirstContentfulPaint = require('../computed/metrics/first-contentful-paint.js');
const FirstMeaningfulPaint = require('../computed/metrics/first-meaningful-paint.js');
const LargestContentfulPaint = require('../computed/metrics/largest-contentful-paint.js');
brendankenny marked this conversation as resolved.
Show resolved Hide resolved
const FirstCPUIdle = require('../computed/metrics/first-cpu-idle.js');
const Interactive = require('../computed/metrics/interactive.js');
const SpeedIndex = require('../computed/metrics/speed-index.js');
Expand Down Expand Up @@ -40,7 +41,6 @@ class Metrics extends Audit {
const devtoolsLog = artifacts.devtoolsLogs[Audit.DEFAULT_PASS];
const metricComputationData = {trace, devtoolsLog, settings: context.settings};


/**
* @template TArtifacts
* @template TReturn
Expand All @@ -56,6 +56,7 @@ class Metrics extends Audit {
const speedline = await Speedline.request(trace, context);
const firstContentfulPaint = await FirstContentfulPaint.request(metricComputationData, context);
const firstMeaningfulPaint = await FirstMeaningfulPaint.request(metricComputationData, context);
const largestContentfulPaint = await requestOrUndefined(LargestContentfulPaint, metricComputationData); // eslint-disable-line max-len
const firstCPUIdle = await requestOrUndefined(FirstCPUIdle, metricComputationData);
const interactive = await requestOrUndefined(Interactive, metricComputationData);
const speedIndex = await requestOrUndefined(SpeedIndex, metricComputationData);
Expand All @@ -69,6 +70,8 @@ class Metrics extends Audit {
firstContentfulPaintTs: firstContentfulPaint.timestamp,
firstMeaningfulPaint: firstMeaningfulPaint.timing,
firstMeaningfulPaintTs: firstMeaningfulPaint.timestamp,
largestContentfulPaint: largestContentfulPaint && largestContentfulPaint.timing,
largestContentfulPaintTs: largestContentfulPaint && largestContentfulPaint.timestamp,
firstCPUIdle: firstCPUIdle && firstCPUIdle.timing,
firstCPUIdleTs: firstCPUIdle && firstCPUIdle.timestamp,
interactive: interactive && interactive.timing,
Expand All @@ -88,6 +91,8 @@ class Metrics extends Audit {
observedFirstContentfulPaintTs: traceOfTab.timestamps.firstContentfulPaint,
observedFirstMeaningfulPaint: traceOfTab.timings.firstMeaningfulPaint,
observedFirstMeaningfulPaintTs: traceOfTab.timestamps.firstMeaningfulPaint,
observedLargestContentfulPaint: traceOfTab.timings.largestContentfulPaint,
brendankenny marked this conversation as resolved.
Show resolved Hide resolved
observedLargestContentfulPaintTs: traceOfTab.timestamps.largestContentfulPaint,
observedTraceEnd: traceOfTab.timings.traceEnd,
observedTraceEndTs: traceOfTab.timestamps.traceEnd,
observedLoad: traceOfTab.timings.load,
Expand Down Expand Up @@ -115,7 +120,7 @@ class Metrics extends Audit {
const details = {
type: 'debugdata',
// TODO: Consider not nesting metrics under `items`.
items: [metrics],
items: [metrics, {lcpInvalidated: traceOfTab.lcpInvalidated}],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this should be observedLcpInvalidated or something but I can't imagine it ever mattering (and maybe like fmpFellBack it'll eventually just go unused in the artifacts)

};

return {
Expand All @@ -132,6 +137,8 @@ class Metrics extends Audit {
* @property {number=} firstContentfulPaintTs
* @property {number} firstMeaningfulPaint
* @property {number=} firstMeaningfulPaintTs
* @property {number=} largestContentfulPaint
* @property {number=} largestContentfulPaintTs
* @property {number=} firstCPUIdle
* @property {number=} firstCPUIdleTs
* @property {number=} interactive
Expand All @@ -149,6 +156,8 @@ class Metrics extends Audit {
* @property {number} observedFirstContentfulPaintTs
* @property {number=} observedFirstMeaningfulPaint
* @property {number=} observedFirstMeaningfulPaintTs
* @property {number=} observedLargestContentfulPaint
* @property {number=} observedLargestContentfulPaintTs
* @property {number=} observedTraceEnd
* @property {number=} observedTraceEndTs
* @property {number=} observedLoad
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LanternFirstMeaningfulPaint extends LanternMetric {
return LanternFirstContentfulPaint.getFirstPaintBasedGraph(
dependencyGraph,
fmp,
// See LanterFirstContentfulPaint's getOptimisticGraph implementation for a longer description
// See LanternFirstContentfulPaint's getOptimisticGraph implementation for a longer description
connorjclark marked this conversation as resolved.
Show resolved Hide resolved
// of why we exclude script initiated resources here.
node => node.hasRenderBlockingPriority() && node.initiatorType !== 'script'
);
Expand Down
41 changes: 41 additions & 0 deletions lighthouse-core/computed/metrics/largest-contentful-paint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
connorjclark marked this conversation as resolved.
Show resolved Hide resolved
* @license Copyright 2019 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';

const makeComputedArtifact = require('../computed-artifact.js');
const ComputedMetric = require('./metric.js');
const LHError = require('../../lib/lh-error.js');

class LargestContentfulPaint extends ComputedMetric {
/**
* @param {LH.Artifacts.MetricComputationData} data
* @param {LH.Audit.Context} context
* @return {Promise<LH.Artifacts.LanternMetric>}
*/
// eslint-disable-next-line no-unused-vars
static computeSimulatedMetric(data, context) {
throw new Error('Unimplemented');
}

/**
* @param {LH.Artifacts.MetricComputationData} data
* @return {Promise<LH.Artifacts.Metric>}
*/
static async computeObservedMetric(data) {
brendankenny marked this conversation as resolved.
Show resolved Hide resolved
const {traceOfTab} = data;
if (!traceOfTab.timestamps.largestContentfulPaint) {
throw new LHError(LHError.errors.NO_LCP);
}

return {
// LCP established as existing, so cast
timing: /** @type {number} */ (traceOfTab.timings.largestContentfulPaint),
timestamp: traceOfTab.timestamps.largestContentfulPaint,
};
}
}

module.exports = makeComputedArtifact(LargestContentfulPaint);
4 changes: 4 additions & 0 deletions lighthouse-core/lib/lh-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ const ERRORS = {
code: 'NO_FMP',
message: UIStrings.badTraceRecording,
},
NO_LCP: {
code: 'NO_LCP',
message: UIStrings.badTraceRecording,
},

// TTI calculation failures
FMP_TOO_LATE_FOR_FCPUI: {code: 'FMP_TOO_LATE_FOR_FCPUI', message: UIStrings.pageLoadTookTooLong},
Expand Down
2 changes: 2 additions & 0 deletions lighthouse-core/lib/minify-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ const traceEventsToKeepInProcess = new Set([
'firstMeaningfulPaintCandidate',
'loadEventEnd',
'domContentLoadedEventEnd',
'largestContentfulPaint::Invalidate',
'largestContentfulPaint::Candidate',
]);

/**
Expand Down
26 changes: 26 additions & 0 deletions lighthouse-core/lib/tracehouse/trace-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,28 @@ class TraceProcessor {
firstMeaningfulPaint = lastCandidate;
}

// LCP comes from the latest `largestContentfulPaint::Candidate`, but it can be invalidated
// by a `largestContentfulPaint::Invalidate` event. In the case that the last candidate is
// invalidated, the value will be undefined.
let largestContentfulPaint;
patrickhulce marked this conversation as resolved.
Show resolved Hide resolved
let lcpInvalidated = false;
// Iterate the events backwards.
for (let i = frameEvents.length - 1; i >= 0; i--) {
const e = frameEvents[i];
// If the event's timestamp is before the navigation start, stop.
if (e.ts <= navigationStart.ts) break;
// If the last lcp event in the trace is 'Invalidate', there is inconclusive data to determine LCP.
if (e.name === 'largestContentfulPaint::Invalidate') {
lcpInvalidated = true;
break;
}
// If not an lcp 'Candidate', keep iterating.
if (e.name !== 'largestContentfulPaint::Candidate') continue;
// Found the last LCP candidate in the trace, let's use it.
largestContentfulPaint = e;
break;
}

const load = frameEvents.find(e => e.name === 'loadEventEnd' && e.ts > navigationStart.ts);
const domContentLoaded = frameEvents.find(
e => e.name === 'domContentLoadedEventEnd' && e.ts > navigationStart.ts
Expand Down Expand Up @@ -551,6 +573,7 @@ class TraceProcessor {
firstPaint: getTimestamp(firstPaint),
firstContentfulPaint: getTimestamp(firstContentfulPaint),
firstMeaningfulPaint: getTimestamp(firstMeaningfulPaint),
largestContentfulPaint: getTimestamp(largestContentfulPaint),
traceEnd: fakeEndOfTraceEvt.ts,
load: getTimestamp(load),
domContentLoaded: getTimestamp(domContentLoaded),
Expand All @@ -566,6 +589,7 @@ class TraceProcessor {
firstPaint: maybeGetTiming(timestamps.firstPaint),
firstContentfulPaint: maybeGetTiming(timestamps.firstContentfulPaint),
firstMeaningfulPaint: maybeGetTiming(timestamps.firstMeaningfulPaint),
largestContentfulPaint: maybeGetTiming(timestamps.largestContentfulPaint),
traceEnd: getTiming(timestamps.traceEnd),
load: maybeGetTiming(timestamps.load),
domContentLoaded: maybeGetTiming(timestamps.domContentLoaded),
Expand All @@ -581,9 +605,11 @@ class TraceProcessor {
firstPaintEvt: firstPaint,
firstContentfulPaintEvt: firstContentfulPaint,
firstMeaningfulPaintEvt: firstMeaningfulPaint,
largestContentfulPaintEvt: largestContentfulPaint,
loadEvt: load,
domContentLoadedEvt: domContentLoaded,
fmpFellBack,
lcpInvalidated,
};
}
}
Expand Down
46 changes: 46 additions & 0 deletions lighthouse-core/test/audits/__snapshots__/metrics-test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Performance: metrics evaluates valid input (with lcp) correctly 1`] = `
brendankenny marked this conversation as resolved.
Show resolved Hide resolved
Object {
"estimatedInputLatency": 1324,
"estimatedInputLatencyTs": undefined,
"firstCPUIdle": 14907,
"firstCPUIdleTs": 1671236823075,
"firstContentfulPaint": 4702,
"firstContentfulPaintTs": 1671226617803,
"firstMeaningfulPaint": 4702,
"firstMeaningfulPaintTs": 1671226617803,
"interactive": 14907,
"interactiveTs": 1671236823075,
"largestContentfulPaint": 15024,
"largestContentfulPaintTs": 1671236939268,
"observedDomContentLoaded": 14907,
"observedDomContentLoadedTs": 1671236823075,
"observedFirstContentfulPaint": 4702,
"observedFirstContentfulPaintTs": 1671226617803,
"observedFirstMeaningfulPaint": 4702,
"observedFirstMeaningfulPaintTs": 1671226617803,
"observedFirstPaint": 4702,
"observedFirstPaintTs": 1671226617803,
"observedFirstVisualChange": 4685,
"observedFirstVisualChangeTs": 1671226600754,
"observedLargestContentfulPaint": 15024,
"observedLargestContentfulPaintTs": 1671236939268,
"observedLastVisualChange": 4685,
"observedLastVisualChangeTs": 1671226600754,
"observedLoad": 40464,
"observedLoadTs": 1671262379989,
"observedNavigationStart": 0,
"observedNavigationStartTs": 1671221915754,
"observedSpeedIndex": 5715,
"observedSpeedIndexTs": 1671227631023,
"observedTraceEnd": 44549,
"observedTraceEndTs": 1671266464483,
"speedIndex": 5715,
"speedIndexTs": 1671227630754,
"totalBlockingTime": 1935,
}
`;

exports[`Performance: metrics evaluates valid input correctly 1`] = `
Object {
"estimatedInputLatency": 78,
Expand All @@ -12,6 +54,8 @@ Object {
"firstMeaningfulPaintTs": undefined,
"interactive": 3427,
"interactiveTs": undefined,
"largestContentfulPaint": undefined,
"largestContentfulPaintTs": undefined,
"observedDomContentLoaded": 560,
"observedDomContentLoadedTs": 225414732309,
"observedFirstContentfulPaint": 499,
Expand All @@ -22,6 +66,8 @@ Object {
"observedFirstPaintTs": 225414670868,
"observedFirstVisualChange": 520,
"observedFirstVisualChangeTs": 225414692015,
"observedLargestContentfulPaint": undefined,
"observedLargestContentfulPaintTs": undefined,
"observedLastVisualChange": 818,
"observedLastVisualChangeTs": 225414990015,
"observedLoad": 2199,
Expand Down
20 changes: 19 additions & 1 deletion lighthouse-core/test/audits/metrics-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const TTIComputed = require('../../computed/metrics/interactive.js');
const pwaTrace = require('../fixtures/traces/progressive-app-m60.json');
const pwaDevtoolsLog = require('../fixtures/traces/progressive-app-m60.devtools.log.json');

const lcpTrace = require('../fixtures/traces/lcp-m79.json');
connorjclark marked this conversation as resolved.
Show resolved Hide resolved
const lcpDevtoolsLog = require('../fixtures/traces/lcp-m79.devtools.log.json');

/* eslint-env jest */

describe('Performance: metrics', () => {
Expand All @@ -29,7 +32,22 @@ describe('Performance: metrics', () => {
expect(result.details.items[0]).toMatchSnapshot();
});

it('does to fail the entire audit when TTI errors', async () => {
it('evaluates valid input (with lcp) correctly', async () => {
const artifacts = {
traces: {
[MetricsAudit.DEFAULT_PASS]: lcpTrace,
},
devtoolsLogs: {
[MetricsAudit.DEFAULT_PASS]: lcpDevtoolsLog,
},
};

const context = {settings: {throttlingMethod: 'devtools'}, computedCache: new Map()};
const result = await MetricsAudit.audit(artifacts, context);
expect(result.details.items[0]).toMatchSnapshot();
});

it('does not fail the entire audit when TTI errors', async () => {
const artifacts = {
traces: {
[MetricsAudit.DEFAULT_PASS]: pwaTrace,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @license Copyright 2019 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';

const assert = require('assert');

const LargestContentfulPaint = require('../../../computed/metrics/largest-contentful-paint.js'); // eslint-disable-line max-len
const trace = require('../../fixtures/traces/lcp-m79.json');
const devtoolsLog = require('../../fixtures/traces/lcp-m79.devtools.log.json');
const invalidTrace = require('../../fixtures/traces/progressive-app-m60.json');
const invalidDevtoolsLog = require('../../fixtures/traces/progressive-app-m60.devtools.log.json');

/* eslint-env jest */

describe('Metrics: LCP', () => {
it('should error when computing a simulated value', async () => {
const settings = {throttlingMethod: 'simulate'};
const context = {settings, computedCache: new Map()};
const resultPromise = LargestContentfulPaint.request({trace, devtoolsLog, settings}, context);
await expect(resultPromise).rejects.toThrow();
});

it('should compute an observed value', async () => {
const settings = {throttlingMethod: 'provided'};
const context = {settings, computedCache: new Map()};
const result = await LargestContentfulPaint.request({trace, devtoolsLog, settings}, context);

assert.equal(Math.round(result.timing), 15024);
assert.equal(result.timestamp, 1671236939268);
});
connorjclark marked this conversation as resolved.
Show resolved Hide resolved

it('should fail to compute an observed value for old trace', async () => {
const settings = {throttlingMethod: 'provided'};
const context = {settings, computedCache: new Map()};
const resultPromise = LargestContentfulPaint.request(
{trace: invalidTrace, devtoolsLog: invalidDevtoolsLog, settings}, context);
await expect(resultPromise).rejects.toThrow('NO_LCP');
});
});
1 change: 1 addition & 0 deletions lighthouse-core/test/computed/trace-of-tab-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe('TraceOfTabComputed', () => {
tts: 866553,
},
fmpFellBack: false,
lcpInvalidated: false,
loadEvt: {
args: {
frame: '0x25a638821e30',
Expand Down

Large diffs are not rendered by default.

3,716 changes: 3,716 additions & 0 deletions lighthouse-core/test/fixtures/traces/lcp-m79.json

Large diffs are not rendered by default.

Loading