-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
+226
−2
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7291b14
new-audit(optimized-animated-content): use videos instead of gifs
wardpeet 8448912
Setup smoketests
wardpeet fc30c77
Review changes
wardpeet 27c8f31
change threshold to 100kb
wardpeet 0bb7652
Review changes
wardpeet c8d017c
Rename optimized animated image audit
wardpeet e682b5e
Fix travis (eslint & smoke)
wardpeet 63266f5
Update bytesize - smoke
wardpeet c7f99f2
Fix travis
wardpeet 4e1707d
Review fixes
wardpeet 31a188d
Add typings
wardpeet 18fab9b
Fix jsdoc typing
wardpeet 5f9c066
Fix lhr json
wardpeet 0d7c6a9
Fix smoketest?
wardpeet 8686e8e
fix smoketest
wardpeet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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