Skip to content

Commit b333000

Browse files
Adam RaineDevtools-frontend LUCI CQ
authored andcommitted
[RPP] Add byte savings to image delivery insight
This required adding a total `byteSavings` to each image since there could be multiple optimizations for a single image. Bug: 372897377 Change-Id: I1c82f66383441abd1638b85de11681c5c16423b6 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6090115 Commit-Queue: Adam Raine <[email protected]> Reviewed-by: Connor Clark <[email protected]>
1 parent fb2dd05 commit b333000

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

front_end/models/trace/insights/ImageDelivery.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ describeWithEnvironment('ImageDelivery', function() {
4040

4141
const insight =
4242
getInsightOrError('ImageDelivery', insights, getFirstOrError(data.Meta.navigationsByNavigationId.values()));
43+
assert.strictEqual(insight.totalByteSavings, 2007125);
4344
assert.deepStrictEqual(
44-
insight.optimizableImages.map(o => ({url: o.request.args.data.url, optimizations: o.optimizations})),
45+
insight.optimizableImages.map(
46+
o => ({url: o.request.args.data.url, optimizations: o.optimizations, byteSavings: o.byteSavings})),
4547
[
4648
{
49+
byteSavings: 1057876,
4750
optimizations: [
4851
{
4952
byteSavings: 1057876,
@@ -54,6 +57,7 @@ describeWithEnvironment('ImageDelivery', function() {
5457
'https://images.ctfassets.net/u275ja1nivmq/6T6z40ay5GFCUtwV7DONgh/0e23606ed1692d9721ab0f39a8d8a99e/yeti_cover.jpg',
5558
},
5659
{
60+
byteSavings: 682028,
5761
optimizations: [
5862
{
5963
byteSavings: 682028,
@@ -64,6 +68,7 @@ describeWithEnvironment('ImageDelivery', function() {
6468
'https://raw.githubusercontent.com/GoogleChrome/lighthouse/refs/heads/main/cli/test/fixtures/dobetterweb/lighthouse-rotating.gif',
6569
},
6670
{
71+
byteSavings: 49760,
6772
optimizations: [
6873
{
6974
byteSavings: 49760,
@@ -74,6 +79,7 @@ describeWithEnvironment('ImageDelivery', function() {
7479
'https://images.ctfassets.net/u275ja1nivmq/6T6z40ay5GFCUtwV7DONgh/0e23606ed1692d9721ab0f39a8d8a99e/yeti_cover.jpg?fm=webp',
7580
},
7681
{
82+
byteSavings: 41421,
7783
optimizations: [
7884
{
7985
byteSavings: 41421,
@@ -86,6 +92,7 @@ describeWithEnvironment('ImageDelivery', function() {
8692
'https://raw.githubusercontent.com/GoogleChrome/lighthouse/refs/heads/main/cli/test/fixtures/byte-efficiency/lighthouse-2048x1356.webp',
8793
},
8894
{
95+
byteSavings: 176040,
8996
optimizations: [
9097
{
9198
byteSavings: 134075,

front_end/models/trace/insights/ImageDelivery.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export type ImageOptimization = {
101101
export interface OptimizableImage {
102102
request: Types.Events.SyntheticNetworkRequest;
103103
optimizations: ImageOptimization[];
104+
byteSavings: number;
104105
/**
105106
* If the an image resource has multiple `PaintImage`s, we compare its intrinsic size to the largest of the displayed sizes.
106107
*
@@ -112,6 +113,7 @@ export interface OptimizableImage {
112113

113114
export type ImageDeliveryInsightModel = InsightModel<{
114115
optimizableImages: OptimizableImage[],
116+
totalByteSavings: number,
115117
}>;
116118

117119
function getOptimizationMessage(optimization: ImageOptimization): string {
@@ -218,9 +220,20 @@ export function generateInsight(
218220
}
219221
}
220222

223+
// At this point (before looking at image size), the # of optimizations should only ever be 1 or 0
224+
// Math.max handles both cases correctly, and is defensive against future patches that would add
225+
// more than 1 format-specific optimization by this point.
226+
const imageByteSavingsFromFormat = Math.max(0, ...optimizations.map(o => o.byteSavings));
227+
let imageByteSavings = imageByteSavingsFromFormat;
228+
221229
const wastedPixelRatio = 1 - (largestImageDisplayPixels / imageFilePixels);
222230
if (wastedPixelRatio > 0) {
223231
const byteSavings = Math.round(wastedPixelRatio * imageBytes);
232+
233+
// This will compound the byte savings from any potential format changes with the image size
234+
// optimization added here.
235+
imageByteSavings += Math.round(wastedPixelRatio * (imageBytes - imageByteSavingsFromFormat));
236+
224237
optimizations.push({
225238
type: ImageOptimizationType.RESPONSIVE_SIZE,
226239
byteSavings,
@@ -242,11 +255,13 @@ export function generateInsight(
242255
request,
243256
largestImagePaint,
244257
optimizations,
258+
byteSavings: imageByteSavings,
245259
});
246260
}
247261
}
248262

249263
return finalize({
250264
optimizableImages,
265+
totalByteSavings: optimizableImages.reduce((total, img) => total + img.byteSavings, 0),
251266
});
252267
}

front_end/panels/timeline/components/insights/ImageDelivery.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ export class ImageDelivery extends BaseInsightComponent<ImageDeliveryInsightMode
5959
};
6060
}
6161

62+
override getEstimatedSavingsBytes(): number|null {
63+
return this.model?.totalByteSavings ?? null;
64+
}
65+
6266
override renderContent(): LitHtml.LitTemplate {
6367
if (!this.model) {
6468
return LitHtml.nothing;

0 commit comments

Comments
 (0)