forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new_audit: efficient-animated-content, use videos instead of gifs (Go…
- Loading branch information
Showing
8 changed files
with
226 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
lighthouse-core/audits/byte-efficiency/efficient-animated-content.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
lighthouse-core/test/audits/byte-efficiency/efficient-animated-content-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters