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

This commit is contained in:
Ward Peeters 2018-04-27 23:43:21 +02:00 коммит произвёл Paul Irish
Родитель 43269549b2
Коммит 6045fa989f
8 изменённых файлов: 226 добавлений и 2 удалений

Просмотреть файл

@ -156,6 +156,8 @@
<!-- 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

Двоичные данные
lighthouse-cli/test/fixtures/dobetterweb/lighthouse-rotating.gif поставляемый Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 912 KiB

Просмотреть файл

@ -18,6 +18,7 @@ module.exports = {
'dom-size',
'render-blocking-resources',
'errors-in-console',
'efficient-animated-content',
],
},
};

Просмотреть файл

@ -35,13 +35,13 @@ module.exports = [
extendedInfo: {
value: {
results: {
length: 17,
length: '>15',
},
},
},
details: {
items: {
length: 17,
length: '>15',
},
},
},
@ -163,6 +163,22 @@ module.exports = [
},
},
},
'efficient-animated-content': {
extendedInfo: {
value: {
wastedKb: 666,
},
},
details: {
items: [
{
url: 'http://localhost:10200/dobetterweb/lighthouse-rotating.gif',
totalBytes: 934285,
wastedBytes: 682028,
},
],
},
},
},
},
];

Просмотреть файл

@ -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;

Просмотреть файл

@ -161,6 +161,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',
@ -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'},

Просмотреть файл

@ -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);
});
});

Просмотреть файл

@ -3221,6 +3221,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": "",
@ -4704,6 +4729,11 @@
"weight": 0,
"group": "perf-hint"
},
{
"id": "efficient-animated-content",
"weight": 0,
"group": "perf-hint"
},
{
"id": "total-byte-weight",
"weight": 0,