Skip to content

Commit

Permalink
add metric savings
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianaixba committed Jul 6, 2023
1 parent c0efe60 commit 91ea165
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/audits/prioritize-lcp-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class PrioritizeLcpImage extends Audit {
.find(element => element.traceEventType === 'largest-contentful-paint');

if (!lcpElement || lcpElement.type !== 'image') {
return {score: null, notApplicable: true};
return {score: null, notApplicable: true, metricSavings: {LCP: 0}};
}

const mainResource = await MainResource.request({devtoolsLog, URL}, context);
Expand Down Expand Up @@ -286,6 +286,7 @@ class PrioritizeLcpImage extends Audit {
numericUnit: 'millisecond',
displayValue: wastedMs ? str_(i18n.UIStrings.displayValueMsSavings, {wastedMs}) : '',
details,
metricSavings: {LCP: wastedMs},
};
}
}
Expand Down
12 changes: 12 additions & 0 deletions core/test/audits/prioritize-lcp-image-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ describe('Performance: prioritize-lcp-image audit', () => {
expect(result).toEqual({
score: null,
notApplicable: true,
metricSavings: {LCP: 0},
});
});

Expand All @@ -123,6 +124,7 @@ describe('Performance: prioritize-lcp-image audit', () => {
expect(result).toEqual({
score: null,
notApplicable: true,
metricSavings: {LCP: 0},
});
});

Expand All @@ -139,6 +141,7 @@ describe('Performance: prioritize-lcp-image audit', () => {
expect(results.score).toEqual(1);
expect(results.details.overallSavingsMs).toEqual(0);
expect(results.details.items).toHaveLength(0);
expect(results.metricSavings).toEqual({LCP: 0});
});

it('shouldn\'t be applicable if the lcp is already preloaded', async () => {
Expand All @@ -149,6 +152,7 @@ describe('Performance: prioritize-lcp-image audit', () => {
expect(results.score).toEqual(1);
expect(results.details.overallSavingsMs).toEqual(0);
expect(results.details.items).toHaveLength(0);
expect(results.metricSavings).toEqual({LCP: 0});

// debugData should be included even if image shouldn't be preloaded.
expect(results.details.debugData).toMatchObject({
Expand All @@ -169,6 +173,7 @@ describe('Performance: prioritize-lcp-image audit', () => {
expect(results.score).toEqual(1);
expect(results.details.overallSavingsMs).toEqual(0);
expect(results.details.items).toHaveLength(0);
expect(results.metricSavings).toEqual({LCP: 0});
});

it('should suggest preloading a lcp image if all criteria is met', async () => {
Expand All @@ -179,6 +184,7 @@ describe('Performance: prioritize-lcp-image audit', () => {
expect(results.details.overallSavingsMs).toEqual(30);
expect(results.details.items[0].url).toEqual(imageUrl);
expect(results.details.items[0].wastedMs).toEqual(30);
expect(results.metricSavings).toEqual({LCP: 30});

expect(results.details.debugData).toMatchObject({
initiatorPath: [
Expand Down Expand Up @@ -207,6 +213,7 @@ describe('Performance: prioritize-lcp-image audit', () => {
],
pathLength: 3,
});
expect(results.metricSavings).toEqual({LCP: 180});
});

it('should use the initiator path of the first image instance loaded', async () => {
Expand Down Expand Up @@ -235,6 +242,7 @@ describe('Performance: prioritize-lcp-image audit', () => {
pathLength: 2,
},
},
metricSavings: {LCP: 0},
});
});

Expand Down Expand Up @@ -267,6 +275,7 @@ describe('Performance: prioritize-lcp-image audit', () => {
pathLength: 3,
},
},
metricSavings: {LCP: 180},
});
});

Expand Down Expand Up @@ -298,6 +307,7 @@ describe('Performance: prioritize-lcp-image audit', () => {
pathLength: 3,
},
},
metricSavings: {LCP: 180},
});
});

Expand Down Expand Up @@ -337,6 +347,7 @@ describe('Performance: prioritize-lcp-image audit', () => {
pathLength: 4,
},
},
metricSavings: {LCP: 210},
});
});

Expand All @@ -359,6 +370,7 @@ describe('Performance: prioritize-lcp-image audit', () => {
pathLength: 2,
},
},
metricSavings: {LCP: 0},
});
});
});

0 comments on commit 91ea165

Please sign in to comment.