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

new_audit: efficient-animated-content - use videos instead of gifs #4885

Merged
merged 15 commits into from
Apr 27, 2018
2 changes: 2 additions & 0 deletions lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ <h2>Do better web tester page</h2>
<!-- PASS(image-aspect-ratio) -->
<img src="lighthouse-480x318.jpg" width="480" height="318">

<!-- FAIL(efficient-animated-content): animated gif found -->
<img src="lighthouse-rotating.gif" width="811" height="462">

<!-- Some websites overwrite the original Error object. The captureJSCallUsage function
relies on the native Error object and prepareStackTrace from V8. When overwriting the stack
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions lighthouse-cli/test/smokehouse/dbw-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
'link-blocking-first-paint',
'script-blocking-first-paint',
'errors-in-console',
'efficient-animated-content',
],
},
};
20 changes: 18 additions & 2 deletions lighthouse-cli/test/smokehouse/dobetterweb/dbw-expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ module.exports = [
extendedInfo: {
value: {
results: {
length: 17,
length: '>15',
},
},
},
details: {
items: {
length: 17,
length: '>15',
},
},
},
Expand Down Expand Up @@ -185,6 +185,22 @@ module.exports = [
},
},
},
'efficient-animated-content': {
extendedInfo: {
value: {
wastedKb: 666,
},
},
details: {
items: [
{
url: 'http://localhost:10200/dobetterweb/lighthouse-rotating.gif',
totalBytes: 934285,
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 yeah @paulirish I told ward I'd tackle a more fine-grained assertion when I finish the other lantern opportunity rejiggering

Copy link
Member

Choose a reason for hiding this comment

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

sgtm i like the look of this

wastedBytes: 682028,
},
],
},
},
},
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @license Copyright 2018 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.
*/
/*
* @fileoverview Audit a page to ensure that videos are used instead of animated gifs
*/
'use strict';

const WebInspector = require('../../lib/web-inspector');
const ByteEfficiencyAudit = require('./byte-efficiency-audit');

// If GIFs are above this size, we'll flag them
// See https://github.com/GoogleChrome/lighthouse/pull/4885#discussion_r178406623 and https://github.com/GoogleChrome/lighthouse/issues/4696#issuecomment-370979920
const GIF_BYTE_THRESHOLD = 100 * 1024;

class EfficientAnimatedContent extends ByteEfficiencyAudit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
name: 'efficient-animated-content',
scoreDisplayMode: ByteEfficiencyAudit.SCORING_MODES.NUMERIC,
description: 'Use video formats for animated content',
helpText: 'Large GIFs are inefficient for delivering animated content. Consider using ' +
'MPEG4/WebM videos for animations and PNG/WebP for static images instead of GIF to save ' +
'network bytes. [Learn more](https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/replace-animated-gifs-with-video/)',
requiredArtifacts: ['devtoolsLogs'],
};
}

/**
* Calculate rough savings percentage based on 1000 real gifs transcoded to video
* @param {number} bytes
* @return {number} rough savings percentage
* @see https://github.com/GoogleChrome/lighthouse/issues/4696#issuecomment-380296510 bytes
*/
static getPercentSavings(bytes) {
return Math.round((29.1 * Math.log10(bytes) - 100.7)) / 100;
}

/**
* @param {!LH.Artifacts} artifacts
* @return {Promise<LH.Audit.Product>}
*/
static async audit_(artifacts) {
const devtoolsLogs = artifacts.devtoolsLogs[EfficientAnimatedContent.DEFAULT_PASS];

const networkRecords = await artifacts.requestNetworkRecords(devtoolsLogs);
const unoptimizedContent = networkRecords.filter(
record => record.mimeType === 'image/gif' &&
record._resourceType === WebInspector.resourceTypes.Image &&
record.resourceSize > GIF_BYTE_THRESHOLD
);

/** @type {Array<{url: string, totalBytes: number, wastedBytes: number}>}*/
const results = unoptimizedContent.map(record => {
return {
url: record.url,
totalBytes: record.resourceSize,
wastedBytes: Math.round(record.resourceSize *
EfficientAnimatedContent.getPercentSavings(record.resourceSize)),
};
});

const headings = [
{key: 'url', itemType: 'url', text: 'URL'},
{
key: 'totalBytes',
itemType: 'bytes',
displayUnit: 'kb',
granularity: 1,
text: 'Transfer Size',
},
{
key: 'wastedBytes',
itemType: 'bytes',
displayUnit: 'kb',
granularity: 1,
text: 'Byte Savings',
},
];

return {
results,
headings,
};
}
}

module.exports = EfficientAnimatedContent;
2 changes: 2 additions & 0 deletions lighthouse-core/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ module.exports = {
'byte-efficiency/uses-optimized-images',
'byte-efficiency/uses-text-compression',
'byte-efficiency/uses-responsive-images',
'byte-efficiency/efficient-animated-content',
'dobetterweb/appcache-manifest',
'dobetterweb/dom-size',
'dobetterweb/external-anchors-use-rel-noopener',
Expand Down Expand Up @@ -283,6 +284,7 @@ module.exports = {
{id: 'time-to-first-byte', weight: 0, group: 'perf-hint'},
{id: 'redirects', weight: 0, group: 'perf-hint'},
{id: 'uses-rel-preload', weight: 0, group: 'perf-hint'},
{id: 'efficient-animated-content', weight: 0, group: 'perf-hint'},
{id: 'total-byte-weight', weight: 0, group: 'perf-info'},
{id: 'uses-long-cache-ttl', weight: 0, group: 'perf-info'},
{id: 'dom-size', weight: 0, group: 'perf-info'},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @license Copyright 2018 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';

/* eslint-env mocha */
const EfficientAnimatedContent =
require('../../../audits/byte-efficiency/efficient-animated-content');
const WebInspector = require('../../../lib/web-inspector');
const assert = require('assert');

describe('Page uses videos for animated GIFs', () => {
it('should flag gifs above 100kb as unoptimized', async () => {
const networkRecords = [
{
_resourceType: WebInspector.resourceTypes.Image,
mimeType: 'image/gif',
resourceSize: 100240,
url: 'https://example.com/example.gif',
},
{
_resourceType: WebInspector.resourceTypes.Image,
mimeType: 'image/gif',
resourceSize: 110000,
url: 'https://example.com/example2.gif',
},
];
const artifacts = {
devtoolsLogs: {[EfficientAnimatedContent.DEFAULT_PASS]: []},
requestNetworkRecords: () => Promise.resolve(networkRecords),
};

const {results} = await EfficientAnimatedContent.audit_(artifacts);
assert.equal(results.length, 1);
assert.equal(results[0].url, 'https://example.com/example2.gif');
assert.equal(results[0].totalBytes, 110000);
assert.equal(Math.round(results[0].wastedBytes), 50600);
});

it(`shouldn't flag content that looks like a gif but isn't`, async () => {
const networkRecords = [
{
mimeType: 'image/gif',
_resourceType: WebInspector.resourceTypes.Media,
resourceSize: 150000,
},
];
const artifacts = {
devtoolsLogs: {[EfficientAnimatedContent.DEFAULT_PASS]: []},
requestNetworkRecords: () => Promise.resolve(networkRecords),
};

const {results} = await EfficientAnimatedContent.audit_(artifacts);
assert.equal(results.length, 0);
});

it(`shouldn't flag non gif content`, async () => {
const networkRecords = [
{
_resourceType: WebInspector.resourceTypes.Document,
mimeType: 'text/html',
resourceSize: 150000,
},
{
_resourceType: WebInspector.resourceTypes.Stylesheet,
mimeType: 'text/css',
resourceSize: 150000,
},
];
const artifacts = {
devtoolsLogs: {[EfficientAnimatedContent.DEFAULT_PASS]: []},
requestNetworkRecords: () => Promise.resolve(networkRecords),
};

const {results} = await EfficientAnimatedContent.audit_(artifacts);
assert.equal(results.length, 0);
});
});
30 changes: 30 additions & 0 deletions lighthouse-core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -3133,6 +3133,31 @@
}
}
},
"efficient-animated-content": {
"score": 1,
"displayValue": "",
"rawValue": 0,
"extendedInfo": {
"value": {
"wastedMs": 0,
"wastedKb": 0,
"results": []
}
},
"scoreDisplayMode": "numeric",
"name": "efficient-animated-content",
"description": "Use video formats for animated content",
"helpText": "Large GIFs are inefficient for delivering animated content. Consider using MPEG4/WebM videos for animations and PNG/WebP for static images instead of GIF to save network bytes. [Learn more](https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/replace-animated-gifs-with-video/)",
"details": {
"type": "table",
"headings": [],
"items": [],
"summary": {
"wastedMs": 0,
"wastedBytes": 0
}
}
},
"appcache-manifest": {
"score": 0,
"displayValue": "",
Expand Down Expand Up @@ -4754,6 +4779,11 @@
"weight": 0,
"group": "perf-hint"
},
{
"id": "efficient-animated-content",
"weight": 0,
"group": "perf-hint"
},
{
"id": "total-byte-weight",
"weight": 0,
Expand Down