-
Notifications
You must be signed in to change notification settings - Fork 9.6k
core(lightwallet): add performance-budget audit #8539
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
Merged
paulirish
merged 11 commits into
GoogleChrome:master
from
khempenius:lw_resource_budget_audit
May 4, 2019
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fcf66da
lightwallet: add resource-budget audit
khempenius f084d6b
Merge branch 'master' of https://github.com/GoogleChrome/lighthouse i…
khempenius 5973c86
Merge branch 'master' of https://github.com/GoogleChrome/lighthouse i…
khempenius 260e86c
lightwallet: more resource-budget changes
khempenius 479f473
lightwallet: Update resource-budget strings
khempenius 1757312
Merge branch 'master' of https://github.com/GoogleChrome/lighthouse i…
khempenius 01c817c
lightwallet: rename resource-budget to performance-budget
khempenius 2ec09cb
LightWallet: performance budget audit
khempenius 64680b8
Merge branch 'master' of https://github.com/GoogleChrome/lighthouse i…
khempenius 1a8cae3
lightwallet: more changes to performance-budget
khempenius 73d969d
lightwallet: revert header change
khempenius 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 hidden or 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,185 @@ | ||
| /** | ||
| * @license Copyright 2019 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'; | ||
|
|
||
| const Audit = require('./audit'); | ||
| const NetworkRecords = require('../computed/network-records.js'); | ||
| const ComputedResourceSummary = require('../computed/resource-summary.js'); | ||
| const i18n = require('../lib/i18n/i18n.js'); | ||
| const MainResource = require('../computed/main-resource.js'); | ||
| const Budget = require('../config/budget.js'); | ||
|
|
||
| const UIStrings = { | ||
| /** Imperative title of a Lighthouse audit that tells the user to minimize the size and quantity of resources used to load the page. */ | ||
| title: 'Keep request counts and file sizes small', | ||
| /** Description of a Lighthouse audit that tells the user that they can setup a budgets for the quantity and size of page resources. No character length limits. */ | ||
| description: 'To set budgets for the quantity and size of page resources,' + | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ' add a budget.json file.', | ||
| /** [ICU Syntax] Label identifying the number of requests*/ | ||
| requestCount: `{count, plural, | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| =1 {1 request} | ||
| other {# requests} | ||
| }`, | ||
| totalResourceType: 'Total', | ||
| /** Label for the 'Document' resource type. */ | ||
| documentResourceType: 'Document', | ||
| /** Label for the 'script' resource type. */ | ||
| scriptResourceType: 'Script', | ||
| /** Label for the 'stylesheet' resource type. */ | ||
| stylesheetResourceType: 'Stylesheet', | ||
| /** Label for the 'image' resource type. */ | ||
| imageResourceType: 'Image', | ||
| /** Label for the 'media' resource type. */ | ||
| mediaResourceType: 'Media', | ||
| /** Label for the 'font' resource type. */ | ||
| fontResourceType: 'Font', | ||
| /** Label for the 'other' resource type. */ | ||
| otherResourceType: 'Other', | ||
| /** Label for the 'third-party' resource type. */ | ||
| thirdPartyResourceType: 'Third-party', | ||
| }; | ||
|
|
||
| const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings); | ||
|
|
||
| class ResourceBudget extends Audit { | ||
| /** | ||
| * @return {LH.Audit.Meta} | ||
| */ | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| static get meta() { | ||
| return { | ||
| id: 'resource-budget', | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| title: str_(UIStrings.title), | ||
| description: str_(UIStrings.description), | ||
| scoreDisplayMode: Audit.SCORING_MODES.INFORMATIVE, | ||
| requiredArtifacts: ['devtoolsLogs'], | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @param {Array<LH.Budget.ResourceBudget>|undefined} budget | ||
| * @param {number} measurement | ||
| * @param {LH.Budget.ResourceType} resourceType | ||
| * @return {number|undefined} | ||
| */ | ||
| static overBudgetAmount(budget, measurement, resourceType) { | ||
| if (budget === undefined) { | ||
| return undefined; | ||
| } | ||
| const resourceBudget = budget.find((b) => b.resourceType === resourceType); | ||
| if (resourceBudget === undefined) { | ||
| return undefined; | ||
| } | ||
| const overBudget = measurement - resourceBudget.budget; | ||
| return overBudget > 0 ? overBudget : undefined; | ||
| } | ||
|
|
||
| /** | ||
| * @param {LH.Budget | undefined} budget | ||
| * @return {LH.Audit.Details.Table['headings']} | ||
| */ | ||
| static tableHeadings(budget) { | ||
| /** @type {LH.Audit.Details.Table['headings']} */ | ||
| const headers = [ | ||
| {key: 'label', itemType: 'text', text: 'Resource Type'}, | ||
| {key: 'count', itemType: 'numeric', text: 'Requests'}, | ||
| {key: 'size', itemType: 'bytes', text: 'File Size'}, | ||
| ]; | ||
|
|
||
| /** @type {LH.Audit.Details.Table['headings']} */ | ||
| const budgetHeaders = [ | ||
| {key: 'countOverBudget', itemType: 'text', text: ''}, | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| {key: 'sizeOverBudget', itemType: 'bytes', text: 'Over Budget'}, | ||
| ]; | ||
| return budget ? headers.concat(budgetHeaders) : headers; | ||
| } | ||
|
|
||
| /** | ||
| * @param {{resourceType: LH.Budget.ResourceType, count: number, size: number}} row | ||
| * @param {LH.Budget | undefined} budget | ||
| * @return {boolean} | ||
| */ | ||
| static shouldIncludeRow(row, budget) { | ||
| if (budget === undefined) { | ||
| return true; | ||
| } else { | ||
| const budgets = (budget.resourceCounts || []).concat(budget.resourceSizes || []); | ||
| return !!budgets.find((b) => { | ||
| return b.resourceType === row.resourceType; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param {LH.Budget|undefined} budget | ||
| * @param {Object<string,{resourceType: LH.Budget.ResourceType, count: number, size: number}>} summary | ||
| * @return {Array<{label: string, count: number, size: number, countOverBudget?: string|undefined, sizeOverBudget?: number|undefined}>} | ||
| */ | ||
| static tableItems(budget, summary) { | ||
| /** @type {Object<LH.Budget.ResourceType,string>} */ | ||
| const strMappings = { | ||
| 'total': str_(UIStrings.totalResourceType), | ||
| 'document': str_(UIStrings.documentResourceType), | ||
| 'script': str_(UIStrings.scriptResourceType), | ||
| 'stylesheet': str_(UIStrings.stylesheetResourceType), | ||
| 'image': str_(UIStrings.imageResourceType), | ||
| 'media': str_(UIStrings.mediaResourceType), | ||
| 'font': str_(UIStrings.fontResourceType), | ||
| 'other': str_(UIStrings.otherResourceType), | ||
| 'third-party': str_(UIStrings.thirdPartyResourceType), | ||
| }; | ||
|
|
||
| /** @type {Array<{label: string, count: number, size: number, countOverBudget?: string|undefined, sizeOverBudget?: number|undefined}>}*/ | ||
| return Object.values(summary).filter((row) => { | ||
| return this.shouldIncludeRow(row, budget); | ||
| }).map((row) => { | ||
| const type = row.resourceType; | ||
|
|
||
| const overCount = this.overBudgetAmount(budget && budget.resourceCounts, row.count, type); | ||
| const overSize = this.overBudgetAmount(budget && budget.resourceSizes, row.size, type); | ||
|
|
||
| return { | ||
| label: strMappings[type], | ||
| count: row.count, | ||
| size: row.size, | ||
| countOverBudget: overCount !== undefined ? | ||
| str_(UIStrings.requestCount, {count: overCount}) : undefined, | ||
| sizeOverBudget: overSize, | ||
| }; | ||
| }).sort((a, b) => { | ||
| const overBudgetComparison = (b.sizeOverBudget || 0) - (a.sizeOverBudget || 0); | ||
| const sizeComparison = b.size - a.size; | ||
| return budget ? overBudgetComparison : sizeComparison; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @param {LH.Artifacts} artifacts | ||
| * @param {LH.Audit.Context} context | ||
| * @return {Promise<LH.Audit.Product>} | ||
| */ | ||
| static async audit(artifacts, context) { | ||
| const devtoolsLog = artifacts.devtoolsLogs[Audit.DEFAULT_PASS]; | ||
| const networkRecords = await NetworkRecords.request(devtoolsLog, context); | ||
| const mainResource = await (MainResource.request({devtoolsLog, URL: artifacts.URL}, context)); | ||
|
|
||
| /** @type{LH.Budget | undefined} */ | ||
| const budget = (context.settings.budgets || []).reverse().find((budget) => { | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return Budget.urlMatchesPattern(mainResource.url, budget.path); | ||
| }); | ||
| const resourceSummary = ComputedResourceSummary.summarize(networkRecords, mainResource.url); | ||
|
|
||
khempenius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const headings = ResourceBudget.tableHeadings(budget); | ||
| const tableItems = ResourceBudget.tableItems(budget, resourceSummary); | ||
|
|
||
| return { | ||
| details: Audit.makeTableDetails(headings, tableItems), | ||
| score: 1, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| module.exports = ResourceBudget; | ||
| module.exports.UIStrings = UIStrings; | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.