Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lighthouse-cli/test/cli/__snapshots__/index-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,7 @@ Object {
"additionalTraceCategories": null,
"auditMode": false,
"blockedUrlPatterns": null,
"budgetsPath": null,
"channel": "cli",
"disableStorageReset": false,
"emulatedFormFactor": "mobile",
Expand Down Expand Up @@ -1327,6 +1328,7 @@ Object {
"additionalTraceCategories": null,
"auditMode": true,
"blockedUrlPatterns": null,
"budgetsPath": null,
"channel": "cli",
"disableStorageReset": false,
"emulatedFormFactor": "mobile",
Expand Down
118 changes: 118 additions & 0 deletions lighthouse-core/config/budgets.js
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',
'firstCpuIdle',
'timeToInteractive',
'firstMeaningfulPaint',
'estimaatedInputLatency',
];
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
* @implements {LH.Budgets.Json}
* @param {LH.Budgets.Json} budgetsJSON
*/
constructor(budgetsJSON) {
/** @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)) {
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;
1 change: 1 addition & 0 deletions lighthouse-core/config/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const defaultSettings = {
disableStorageReset: false,
emulatedFormFactor: 'mobile',
channel: 'node',
budgetsPath: null,

// the following settings have no defaults but we still want ensure that `key in settings`
// in config will work in a typechecked way
Expand Down
99 changes: 99 additions & 0 deletions lighthouse-core/test/config/budgets-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* @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';

const Budgets = require('../../config/budgets');
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', () => {
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/);
});
});
});
1 change: 1 addition & 0 deletions lighthouse-core/test/results/artifacts/artifacts.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"disableStorageReset": false,
"emulatedFormFactor": "mobile",
"channel": "cli",
"budgetsPath": null,
"locale": "en-US",
"blockedUrlPatterns": null,
"additionalTraceCategories": null,
Expand Down
1 change: 1 addition & 0 deletions lighthouse-core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -2963,6 +2963,7 @@
"disableStorageReset": false,
"emulatedFormFactor": "mobile",
"channel": "cli",
"budgetsPath": null,
"locale": "en-US",
"blockedUrlPatterns": null,
"additionalTraceCategories": null,
Expand Down
35 changes: 35 additions & 0 deletions types/budgets.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @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.
*/

declare global {
module LH {
module Budgets {
export interface Json {
budgets: Array<Budget>;
}

export interface Budget {
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;
}
}
}
}

// empty export to keep file a module
export { }
2 changes: 2 additions & 0 deletions types/externs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ declare global {
channel?: string
/** Precomputed lantern estimates to use instead of observed analysis. */
precomputedLanternData?: PrecomputedLanternData | null;
// Flag indicating the path to the budgets.json file for LightWallet
budgetsPath?: string | null;
}

/**
Expand Down