-
Notifications
You must be signed in to change notification settings - Fork 9.6k
feat(lightwallet): Add --budgets-path & Budgets constructor #8427
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
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b8e359f
Add --budgets-path & Budgets constructor
khempenius 133a45f
Update budgets validation
khempenius f680fb6
Add Budgets to SharedFlagSettings
khempenius bea7d9f
Add budgets.json test fixture
khempenius 6f5df6f
LightWallet: use snakecase for ids, add links to docs
khempenius 49275da
Change budgets.json to array; code cleanup
khempenius bab413d
Initialize budgets in config.js
khempenius 616c71a
Rename "budgets" to "budget"
khempenius c966322
formatting, interface nesting tweaks
brendankenny 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
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,118 @@ | ||
| /** | ||
| * @license Copyright 2016 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'; | ||
|
|
||
| class Budgets { | ||
| /** | ||
| * @param {LH.Budgets.ResourceBudget} resourceBudget | ||
| * @return {LH.Budgets.ResourceBudget} | ||
| */ | ||
| static validateResourceBudget(resourceBudget) { | ||
| const validResourceTypes = [ | ||
| 'total', | ||
| 'document', | ||
| 'script', | ||
| 'stylesheet', | ||
| 'image', | ||
| 'media', | ||
| 'font', | ||
| 'other', | ||
| 'thirdParty', | ||
| ]; | ||
| if (!validResourceTypes.includes(resourceBudget.resourceType)) { | ||
| throw new Error(`Invalid resource type: ${resourceBudget.resourceType}. \n` + | ||
| `Valid resource types are: ${ validResourceTypes.join(', ') }`); | ||
| } | ||
| if (isNaN(resourceBudget.budget)) { | ||
| throw new Error('Invalid budget: ${resourceBudget.budget}'); | ||
| } | ||
| return resourceBudget; | ||
| } | ||
|
|
||
| /** | ||
| * @param {LH.Budgets.TimingBudget} timingBudget | ||
| * @return {LH.Budgets.TimingBudget} | ||
| */ | ||
| static validateTimingBudget(timingBudget) { | ||
| const validTimingMetrics = [ | ||
| 'firstContentfulPaint', | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 'firstCpuIdle', | ||
| 'timeToInteractive', | ||
| 'firstMeaningfulPaint', | ||
| 'estimaatedInputLatency', | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ]; | ||
| if (!validTimingMetrics.includes(timingBudget.metric)) { | ||
| throw new Error(`Invalid timing metric: ${timingBudget.metric}. \n` + | ||
| `Valid timing metrics are: ${validTimingMetrics.join(', ')}`); | ||
| } | ||
| if (isNaN(timingBudget.budget)) { | ||
| throw new Error('Invalid budget: ${timingBudget.budget}'); | ||
| } | ||
| if (timingBudget.tolerance !== undefined && isNaN(timingBudget.tolerance)) { | ||
| throw new Error('Invalid tolerance: ${timingBudget.tolerance}'); | ||
| } | ||
| return timingBudget; | ||
| } | ||
|
|
||
| /** | ||
| * @constructor | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @implements {LH.Budgets.Json} | ||
| * @param {LH.Budgets.Json} budgetsJSON | ||
| */ | ||
| constructor(budgetsJSON) { | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /** @type {Array<LH.Budgets.Budget>} */ | ||
| this.budgets = []; | ||
|
|
||
| budgetsJSON.budgets.forEach((b) => { | ||
| /** @type {LH.Budgets.Budget} */ | ||
| const budget = {}; | ||
| const validBudgetProperties = ['resourceSizes', 'resourceCounts', 'timings']; | ||
| for (const prop in b) { | ||
| if (!validBudgetProperties.includes(prop)) { | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| throw new Error(`Unsupported budget property: ${prop}. \n` + | ||
| `Valid properties are: ${validBudgetProperties.join(', ')}`); | ||
| } | ||
| } | ||
| if (b.resourceSizes !== undefined) { | ||
| budget.resourceSizes = b.resourceSizes.map((r) => { | ||
| return Budgets.validateResourceBudget({ | ||
| resourceType: r.resourceType, | ||
| budget: r.budget, | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| if (b.resourceCounts !== undefined) { | ||
| budget.resourceCounts = b.resourceCounts.map((r) => { | ||
| return Budgets.validateResourceBudget({ | ||
| resourceType: r.resourceType, | ||
| budget: r.budget, | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| if (b.timings !== undefined) { | ||
| budget.timings = b.timings.map((t) => { | ||
| if (t.tolerance !== undefined) { | ||
| return Budgets.validateTimingBudget({ | ||
| metric: t.metric, | ||
| budget: t.budget, | ||
| tolerance: t.tolerance, | ||
| }); | ||
| } else { | ||
| return Budgets.validateTimingBudget({ | ||
| metric: t.metric, | ||
| budget: t.budget, | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| this.budgets.push(budget); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| module.exports = Budgets; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /** | ||
| * @license Copyright 2016 Google Inc. All Rights Reserved. | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * 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 Budgets = require('../../config/budgets'); | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const assert = require('assert'); | ||
| /* eslint-env jest */ | ||
|
|
||
| describe('Budgets', () => { | ||
| let budgetsJson; | ||
| beforeEach(() => { | ||
| budgetsJson = { | ||
| budgets: [{ | ||
| resourceSizes: [{ | ||
| resourceType: 'script', | ||
| budget: 123, | ||
| }, | ||
| { | ||
| resourceType: 'image', | ||
| budget: 456, | ||
| }], | ||
| resourceCounts: [{ | ||
| resourceType: 'total', | ||
| budget: 100, | ||
| }, | ||
| { | ||
| resourceType: 'thirdParty', | ||
| budget: 10, | ||
| }], | ||
| timings: [{ | ||
| metric: 'timeToInteractive', | ||
| budget: 2000, | ||
| tolerance: 1000, | ||
| }, | ||
| { | ||
| metric: 'firstContentfulPaint', | ||
| budget: 1000, | ||
| tolerance: 500, | ||
| }], | ||
| }, | ||
| { | ||
| resourceSizes: [ | ||
| { | ||
| resourceType: 'script', | ||
| budget: 1000, | ||
| }, | ||
| ], | ||
| }], | ||
| }; | ||
| }); | ||
| it('initializes correctly', () => { | ||
khempenius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const budgets = new Budgets(budgetsJson); | ||
| assert.equal(budgets.budgets.length, 2); | ||
|
|
||
| assert.equal(budgets.budgets[0].resourceSizes.length, 2); | ||
| assert.equal(budgets.budgets[0].resourceSizes[0].resourceType, 'script'); | ||
| assert.equal(budgets.budgets[0].resourceSizes[0].budget, 123); | ||
|
|
||
| assert.equal(budgets.budgets[0].resourceCounts.length, 2); | ||
| assert.equal(budgets.budgets[0].resourceCounts[0].resourceType, 'total'); | ||
| assert.equal(budgets.budgets[0].resourceCounts[0].budget, 100); | ||
|
|
||
| assert.equal(budgets.budgets[0].timings.length, 2); | ||
| assert.equal(budgets.budgets[0].timings[1].metric, 'firstContentfulPaint'); | ||
| assert.equal(budgets.budgets[0].timings[1].budget, 1000); | ||
| assert.equal(budgets.budgets[0].timings[1].tolerance, 500); | ||
| }); | ||
| it('throws error if an unsupported budget property is used', () => { | ||
| budgetsJson.budgets[0].sizes = []; | ||
| assert.throws(_ => new Budgets(budgetsJson), /Unsupported budget property/); | ||
| }); | ||
| describe('resource budget validation', () => { | ||
| it('throws when an invalid resource type is supplied', () => { | ||
| budgetsJson.budgets[0].resourceSizes[0].resourceType = 'movies'; | ||
| assert.throws(_ => new Budgets(budgetsJson), /Invalid resource type/); | ||
| }); | ||
| it('throws when an invalid budget is supplied', () => { | ||
| budgetsJson.budgets[0].resourceSizes[0].budget = '100 MB'; | ||
| assert.throws(_ => new Budgets(budgetsJson), /Invalid budget/); | ||
| }); | ||
| }); | ||
| describe('timing budget validation', () => { | ||
| it('throws when an invalid metric is supplied', () => { | ||
| budgetsJson.budgets[0].timings[0].metric = 'lastMeaningfulPaint'; | ||
| assert.throws(_ => new Budgets(budgetsJson), /Invalid timing metric/); | ||
| }); | ||
| it('throws when an invalid budget is supplied', () => { | ||
| budgetsJson.budgets[0].timings[0].budget = '100KB'; | ||
| assert.throws(_ => new Budgets(budgetsJson), /Invalid budget/); | ||
| }); | ||
| it('throws when an invalid tolerance is supplied', () => { | ||
| budgetsJson.budgets[0].timings[0].tolerance = '100ms'; | ||
| assert.throws(_ => new Budgets(budgetsJson), /Invalid tolerance/); | ||
| }); | ||
| }); | ||
| }); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /** | ||
| * @license Copyright 2018 Google Inc. All Rights Reserved. | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * 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. | ||
| */ | ||
|
|
||
| declare global { | ||
| module LH { | ||
| module Budgets { | ||
| export interface Json { | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| budgets: Array<Budget>; | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| export interface Budget { | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| resourceCounts?: Array<ResourceBudget>; | ||
| resourceSizes?: Array<ResourceBudget>; | ||
| timings?: Array<TimingBudget>; | ||
| } | ||
|
|
||
| export interface ResourceBudget { | ||
| resourceType: string; | ||
| budget: number; | ||
| } | ||
|
|
||
| export interface TimingBudget { | ||
| metric: string; | ||
| budget: number; | ||
| tolerance?: number; | ||
| } | ||
| } | ||
| } | ||
| } | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // empty export to keep file a module | ||
| export { } | ||
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
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.