-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
fix(unused-css-rules): update to support new coverage format #2518
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4608a68
fix(unused-css-rules): update to support new coverage format
patrickhulce c56b19f
re-enable smokehouse tests
patrickhulce b669d5e
Merge branch 'master' into css_coverage
patrickhulce 210c514
feedback
patrickhulce 57de9c9
add tooltips to url
patrickhulce a0b414f
last two feedbacks
patrickhulce 237a94f
clearer total byte logic
patrickhulce 1e35aa9
adjust multipliers
patrickhulce 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
'use strict'; | ||
|
||
const ByteEfficiencyAudit = require('./byte-efficiency-audit'); | ||
const URL = require('../../lib/url-shim'); | ||
|
||
const PREVIEW_LENGTH = 100; | ||
|
||
|
@@ -34,30 +33,26 @@ class UnusedCSSRules extends ByteEfficiencyAudit { | |
*/ | ||
static indexStylesheetsById(styles, networkRecords) { | ||
const indexedNetworkRecords = networkRecords | ||
.filter(record => record._resourceType && record._resourceType._name === 'stylesheet') | ||
.reduce((indexed, record) => { | ||
indexed[record.url] = record; | ||
return indexed; | ||
}, {}); | ||
|
||
return styles.reduce((indexed, stylesheet) => { | ||
indexed[stylesheet.header.styleSheetId] = Object.assign({ | ||
used: [], | ||
unused: [], | ||
usedRules: [], | ||
networkRecord: indexedNetworkRecords[stylesheet.header.sourceURL], | ||
}, stylesheet); | ||
return indexed; | ||
}, {}); | ||
} | ||
|
||
/** | ||
* Counts the number of unused rules and adds count information to sheets. | ||
* Adds used rules to their corresponding stylesheet. | ||
* @param {!Array.<{styleSheetId: string, used: boolean}>} rules The output of the CSSUsage gatherer. | ||
* @param {!Object} indexedStylesheets Stylesheet information indexed by id. | ||
* @return {number} The number of unused rules. | ||
*/ | ||
static countUnusedRules(rules, indexedStylesheets) { | ||
let unused = 0; | ||
|
||
static indexUsedRules(rules, indexedStylesheets) { | ||
rules.forEach(rule => { | ||
const stylesheetInfo = indexedStylesheets[rule.styleSheetId]; | ||
|
||
|
@@ -66,14 +61,48 @@ class UnusedCSSRules extends ByteEfficiencyAudit { | |
} | ||
|
||
if (rule.used) { | ||
stylesheetInfo.used.push(rule); | ||
} else { | ||
unused++; | ||
stylesheetInfo.unused.push(rule); | ||
stylesheetInfo.usedRules.push(rule); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @param {!Object} stylesheetInfo | ||
* @return {{wastedBytes: number, totalBytes: number, wastedPercent: number}} | ||
*/ | ||
static computeUsage(stylesheetInfo) { | ||
let usedUncompressedBytes = 0; | ||
const totalUncompressedBytes = stylesheetInfo.content.length; | ||
|
||
for (const usedRule of stylesheetInfo.usedRules) { | ||
usedUncompressedBytes += usedRule.endOffset - usedRule.startOffset; | ||
} | ||
|
||
const networkRecord = stylesheetInfo.networkRecord; | ||
let totalTransferredBytes; | ||
if (!networkRecord) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @paulirish how's this? |
||
// We don't know how many bytes this sheet used on the network, but we can guess it was | ||
// roughly the size of the content gzipped. | ||
// See https://discuss.httparchive.org/t/file-size-and-compression-savings/145 for multipliers | ||
totalTransferredBytes = Math.round(totalUncompressedBytes * .5); | ||
} else if (networkRecord._resourceType && networkRecord._resourceType._name === 'stylesheet') { | ||
// This was a regular standalone stylesheet, just use the transfer size. | ||
totalTransferredBytes = networkRecord.transferSize; | ||
} else { | ||
// This was a stylesheet that was inlined in a different resource type (e.g. HTML document). | ||
// Use the compression ratio of the resource to estimate the total transferred bytes. | ||
const compressionRatio = (networkRecord._transferSize / networkRecord._resourceSize) || 1; | ||
totalTransferredBytes = Math.round(compressionRatio * totalUncompressedBytes); | ||
} | ||
|
||
const percentUnused = (totalUncompressedBytes - usedUncompressedBytes) / totalUncompressedBytes; | ||
const wastedBytes = Math.round(percentUnused * totalTransferredBytes); | ||
|
||
return unused; | ||
return { | ||
wastedBytes, | ||
wastedPercent: percentUnused * 100, | ||
totalBytes: totalTransferredBytes, | ||
}; | ||
} | ||
|
||
/** | ||
|
@@ -115,40 +144,21 @@ class UnusedCSSRules extends ByteEfficiencyAudit { | |
/** | ||
* @param {!Object} stylesheetInfo The stylesheetInfo object. | ||
* @param {string} pageUrl The URL of the page, used to identify inline styles. | ||
* @return {{url: string, label: string, code: string}} The result for the details object. | ||
* @return {?{url: string, wastedBytes: number, totalBytes: number}} | ||
*/ | ||
static mapSheetToResult(stylesheetInfo, pageUrl) { | ||
const numUsed = stylesheetInfo.used.length; | ||
const numUnused = stylesheetInfo.unused.length; | ||
|
||
if ((numUsed === 0 && numUnused === 0) || stylesheetInfo.isDuplicate) { | ||
if (stylesheetInfo.isDuplicate) { | ||
return null; | ||
} | ||
|
||
let url = stylesheetInfo.header.sourceURL; | ||
if (!url || url === pageUrl) { | ||
const contentPreview = UnusedCSSRules.determineContentPreview(stylesheetInfo.content); | ||
url = '*inline*```' + contentPreview + '```'; | ||
} else { | ||
url = URL.getURLDisplayName(url); | ||
url = {type: 'code', text: contentPreview}; | ||
} | ||
|
||
// If we don't know for sure how many bytes this sheet used on the network, | ||
// we can guess it was roughly the size of the content gzipped. | ||
const totalBytes = stylesheetInfo.networkRecord ? | ||
stylesheetInfo.networkRecord.transferSize : | ||
Math.round(stylesheetInfo.content.length / 3); | ||
|
||
const percentUnused = numUnused / (numUsed + numUnused); | ||
const wastedBytes = Math.round(percentUnused * totalBytes); | ||
|
||
return { | ||
url, | ||
numUnused, | ||
wastedBytes, | ||
wastedPercent: percentUnused * 100, | ||
totalBytes, | ||
}; | ||
const usage = UnusedCSSRules.computeUsage(stylesheetInfo); | ||
return Object.assign({url}, usage); | ||
} | ||
|
||
/** | ||
|
@@ -163,21 +173,21 @@ class UnusedCSSRules extends ByteEfficiencyAudit { | |
const devtoolsLogs = artifacts.devtoolsLogs[ByteEfficiencyAudit.DEFAULT_PASS]; | ||
return artifacts.requestNetworkRecords(devtoolsLogs).then(networkRecords => { | ||
const indexedSheets = UnusedCSSRules.indexStylesheetsById(styles, networkRecords); | ||
UnusedCSSRules.countUnusedRules(usage, indexedSheets); | ||
const results = Object.keys(indexedSheets).map(sheetId => { | ||
return UnusedCSSRules.mapSheetToResult(indexedSheets[sheetId], pageUrl); | ||
}).filter(sheet => sheet && sheet.wastedBytes > 1024); | ||
UnusedCSSRules.indexUsedRules(usage, indexedSheets); | ||
|
||
const results = Object.keys(indexedSheets) | ||
.map(sheetId => UnusedCSSRules.mapSheetToResult(indexedSheets[sheetId], pageUrl)) | ||
.filter(sheet => sheet && sheet.wastedBytes > 1024); | ||
|
||
const headings = [ | ||
{key: 'url', itemType: 'url', text: 'URL'}, | ||
{key: 'numUnused', itemType: 'url', text: 'Unused Rules'}, | ||
{key: 'totalKb', itemType: 'text', text: 'Original'}, | ||
{key: 'potentialSavings', itemType: 'text', text: 'Potential Savings'}, | ||
]; | ||
|
||
return { | ||
results, | ||
headings | ||
headings, | ||
}; | ||
}); | ||
} | ||
|
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
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
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.
can we make this more precise since we control the test page?
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.
sure, the score gets bucketed by the generic byte efficiency audit though so it's not that useful. I added an assertion on the "wastedKb" instead which is a little more explicit and useful