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(uses-rel-preconnect): add FCP and LCP savings #15281

Merged
merged 7 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
15 changes: 15 additions & 0 deletions core/audits/uses-rel-preconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {LoadSimulator} from '../computed/load-simulator.js';
import {ProcessedNavigation} from '../computed/processed-navigation.js';
import {PageDependencyGraph} from '../computed/page-dependency-graph.js';
import {LanternLargestContentfulPaint} from '../computed/metrics/lantern-largest-contentful-paint.js';
import {LanternFirstContentfulPaint} from '../computed/metrics/lantern-first-contentful-paint.js';

// Preconnect establishes a "clean" socket. Chrome's socket manager will keep an unused socket
// around for 10s. Meaning, the time delta between processing preconnect a request should be <10s,
Expand Down Expand Up @@ -126,6 +127,7 @@ class UsesRelPreconnectAudit extends Audit {
const settings = context.settings;

let maxWasted = 0;
let maxWastedFCP = 0;
adrianaixba marked this conversation as resolved.
Show resolved Hide resolved
/** @type {Array<LH.IcuMessage>} */
const warnings = [];

Expand All @@ -147,6 +149,13 @@ class UsesRelPreconnectAudit extends Audit {
if (node.type === 'network' ) lcpGraphURLs.add(node.record.url);
});

const fcpGraph =
await LanternFirstContentfulPaint.getPessimisticGraph(pageGraph, processedNavigation);
const fcpGraphURLs = new Set();
fcpGraph.traverse(node => {
if (node.type === 'network' ) fcpGraphURLs.add(node.record.url);
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I realized this is copied but can you fix in both places?

Suggested change
if (node.type === 'network' ) fcpGraphURLs.add(node.record.url);
if (node.type === 'network') fcpGraphURLs.add(node.record.url);

Copy link
Collaborator

Choose a reason for hiding this comment

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

uh why doesnt yarn lint catch this lol

Copy link
Member

Choose a reason for hiding this comment

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

https://eslint.org/docs/latest/rules/no-multi-spaces

Looks like the lint rule only flags if there are multiple spaces.

});

/** @type {Map<string, LH.Artifacts.NetworkRequest[]>} */
const origins = new Map();
networkRecords
Expand Down Expand Up @@ -217,6 +226,10 @@ class UsesRelPreconnectAudit extends Audit {
}

maxWasted = Math.max(wastedMs, maxWasted);

if (fcpGraphURLs.has(firstRecordOfOrigin.url)) {
maxWastedFCP = Math.max(wastedMs, maxWasted);
}
results.push({
url: securityOrigin,
wastedMs: wastedMs,
Expand All @@ -240,6 +253,7 @@ class UsesRelPreconnectAudit extends Audit {
score: 1,
warnings: preconnectLinks.length >= 3 ?
[...warnings, str_(UIStrings.tooManyPreconnectLinksWarning)] : warnings,
metricSavings: {LCP: maxWasted, FCP: maxWastedFCP},
};
}

Expand All @@ -261,6 +275,7 @@ class UsesRelPreconnectAudit extends Audit {
'',
warnings,
details,
metricSavings: {LCP: maxWasted, FCP: maxWastedFCP},
};
}
}
Expand Down
12 changes: 11 additions & 1 deletion core/test/audits/uses-rel-preconnect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ const mainResource = {
url: 'https://www.example.com/',
timing: {receiveHeadersEnd: 0.5},
networkEndTime: 1000,
priority: 'High',
};

function buildArtifacts(networkRecords) {
const trace = createTestTrace({
timeOrigin: 0,
largestContentfulPaint: 5000,
firstContentfulPaint: 5000,
adrianaixba marked this conversation as resolved.
Show resolved Hide resolved
topLevelTasks: [{ts: 1000, duration: 50}],
});
const devtoolsLog = networkRecordsToDevtoolsLog(networkRecords);
Expand Down Expand Up @@ -177,6 +179,7 @@ describe('Performance: uses-rel-preconnect audit', () => {
connectEnd: 300,
receiveHeadersEnd: 2.3,
},
priority: 'High',
},
{
url: 'https://cdn.example.com/second',
Expand All @@ -188,17 +191,19 @@ describe('Performance: uses-rel-preconnect audit', () => {
connectEnd: 400,
receiveHeadersEnd: 3.4,
},
priority: 'High',
},
];

const artifacts = buildArtifacts(networkRecords);
const context = {settings: {}, computedCache: new Map()};
const {numericValue, details} = await UsesRelPreconnect.audit(artifacts, context);
const {numericValue, details, metricSavings} = await UsesRelPreconnect.audit(artifacts, context);
assert.equal(numericValue, 300);
assert.equal(details.items.length, 1);
assert.deepStrictEqual(details.items, [
{url: 'https://cdn.example.com', wastedMs: 300},
]);
assert.deepStrictEqual(metricSavings, {LCP: 300, FCP: 300});
});

it(`should give a list of important preconnected origins`, async () => {
Expand All @@ -214,6 +219,7 @@ describe('Performance: uses-rel-preconnect audit', () => {
connectEnd: 300,
receiveHeadersEnd: 2.3,
},
priority: 'High',
},
{
url: 'https://othercdn.example.com/second',
Expand All @@ -225,6 +231,7 @@ describe('Performance: uses-rel-preconnect audit', () => {
connectEnd: 600,
receiveHeadersEnd: 1.8,
},
priority: 'High',
},
{
url: 'https://unimportant.example.com/second',
Expand All @@ -237,6 +244,7 @@ describe('Performance: uses-rel-preconnect audit', () => {
connectEnd: 600,
receiveHeadersEnd: 1.8,
},
priority: 'High',
},
];

Expand All @@ -246,13 +254,15 @@ describe('Performance: uses-rel-preconnect audit', () => {
numericValue,
details,
warnings,
metricSavings,
} = await UsesRelPreconnect.audit(artifacts, context);
assert.equal(numericValue, 300);
assert.equal(details.items.length, 2);
assert.deepStrictEqual(details.items, [
{url: 'https://othercdn.example.com', wastedMs: 300},
{url: 'http://cdn.example.com', wastedMs: 150},
]);
assert.deepStrictEqual(metricSavings, {LCP: 300, FCP: 300});
assert.equal(warnings.length, 0);
});

Expand Down
3 changes: 2 additions & 1 deletion core/test/create-test-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const lcpImageUrl = 'http://www.example.com/image.png';
* @property {string} [frameUrl]
* @property {number} [timeOrigin]
* @property {number} [largestContentfulPaint]
* @property {number} [firstContentfulPaint]
* @property {number} [traceEnd]
* @property {Array<TopLevelTaskDef>} [topLevelTasks]
* @property {Array<ChildFrame>} [childFrames] Add a child frame with a known `frame` id for easy insertion of child frame events.
Expand Down Expand Up @@ -140,7 +141,7 @@ function createTestTrace(options) {
args: {frame: rootFrame},
}, {
name: 'firstContentfulPaint',
ts: timeOrigin + 10,
ts: options.firstContentfulPaint ? options.firstContentfulPaint * 1000 : timeOrigin + 10,
pid,
tid,
ph: 'R',
Expand Down