forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timing-budget.js
175 lines (159 loc) · 6.69 KB
/
timing-budget.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {Audit} from './audit.js';
import {TimingSummary} from '../computed/metrics/timing-summary.js';
import {MainResource} from '../computed/main-resource.js';
import {Budget} from '../config/budget.js';
import * as i18n from '../lib/i18n/i18n.js';
const UIStrings = {
/** Title of a Lighthouse audit that compares how quickly the page loads against targets set by the user. Timing budgets are a type of performance budget. */
title: 'Timing budget',
/** Description of a Lighthouse audit where a user sets budgets for how quickly the page loads. No character length limits. The last sentence starting with 'Learn' becomes link text to additional documentation. */
description: 'Set a timing budget to help you keep an eye on the performance of your site. Performant sites load fast and respond to user input events quickly. [Learn more about performance budgets](https://developers.google.com/web/tools/lighthouse/audits/budgets).',
/** Label for a column in a data table; entries will be the names of different timing metrics, e.g. "Time to Interactive", "First Contentful Paint", etc. */
columnTimingMetric: 'Metric',
/** Label for a column in a data table; entries will be the measured value of a particular timing metric. Most entries will have a unit of milliseconds, but units could be other things as well. */
columnMeasurement: 'Measurement',
};
const str_ = i18n.createIcuMessageFn(import.meta.url, UIStrings);
/** @typedef {{metric: LH.Budget.TimingMetric, label: LH.IcuMessage, measurement?: LH.Audit.Details.NumericValue|number, overBudget?: LH.Audit.Details.NumericValue|number}} BudgetItem */
class TimingBudget extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'timing-budget',
title: str_(UIStrings.title),
description: str_(UIStrings.description),
scoreDisplayMode: Audit.SCORING_MODES.INFORMATIVE,
supportedModes: ['navigation'],
requiredArtifacts: ['devtoolsLogs', 'traces', 'URL', 'GatherContext'],
};
}
/**
* @param {LH.Budget.TimingMetric} timingMetric
* @return {LH.IcuMessage}
*/
static getRowLabel(timingMetric) {
/** @type {Record<LH.Budget.TimingMetric, string>} */
const strMappings = {
'first-contentful-paint': i18n.UIStrings.firstContentfulPaintMetric,
'interactive': i18n.UIStrings.interactiveMetric,
'first-meaningful-paint': i18n.UIStrings.firstMeaningfulPaintMetric,
'max-potential-fid': i18n.UIStrings.maxPotentialFIDMetric,
'total-blocking-time': i18n.UIStrings.totalBlockingTimeMetric,
'speed-index': i18n.UIStrings.speedIndexMetric,
'largest-contentful-paint': i18n.UIStrings.largestContentfulPaintMetric,
'cumulative-layout-shift': i18n.UIStrings.cumulativeLayoutShiftMetric,
};
return str_(strMappings[timingMetric]);
}
/**
* @param {LH.Budget.TimingMetric} timingMetric
* @param {LH.Artifacts.TimingSummary} summary
* @return {number|undefined}
*/
static getMeasurement(timingMetric, summary) {
/** @type {Record<LH.Budget.TimingMetric, number|undefined>} */
const measurements = {
'first-contentful-paint': summary.firstContentfulPaint,
'interactive': summary.interactive,
'first-meaningful-paint': summary.firstMeaningfulPaint,
'max-potential-fid': summary.maxPotentialFID,
'total-blocking-time': summary.totalBlockingTime,
'speed-index': summary.speedIndex,
'largest-contentful-paint': summary.largestContentfulPaint,
'cumulative-layout-shift': summary.cumulativeLayoutShift,
};
return measurements[timingMetric];
}
/**
* @param {LH.Util.Immutable<LH.Budget>} budget
* @param {LH.Artifacts.TimingSummary} summary
* @return {Array<BudgetItem>}
*/
static tableItems(budget, summary) {
/** @type {Array<BudgetItem>} */
let items = [];
if (!budget.timings) {
return items;
}
items = budget.timings.map((timingBudget) => {
const metricName = timingBudget.metric;
const label = this.getRowLabel(metricName);
const measurement = this.getMeasurement(metricName, summary);
const overBudget = measurement && (measurement > timingBudget.budget)
? (measurement - timingBudget.budget) : undefined;
return {
metric: metricName,
label,
measurement,
overBudget,
};
}).sort((a, b) => {
return (b.overBudget || 0) - (a.overBudget || 0);
});
// CLS requires a different granularity and should be a numeric type.
// Defining type here overrides the column setting so that it doesn't receive ms units.
const clsItem = items.find(item => item.metric === 'cumulative-layout-shift');
if (clsItem) {
if (typeof clsItem.measurement === 'number') {
clsItem.measurement = {
type: 'numeric',
value: Number(clsItem.measurement),
granularity: 0.01,
};
}
if (typeof clsItem.overBudget === 'number') {
clsItem.overBudget = {
type: 'numeric',
value: Number(clsItem.overBudget),
granularity: 0.01,
};
}
}
return items;
}
/**
* @param {LH.Artifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<LH.Audit.Product>}
*/
static async audit(artifacts, context) {
const gatherContext = artifacts.GatherContext;
const devtoolsLog = artifacts.devtoolsLogs[Audit.DEFAULT_PASS];
const trace = artifacts.traces[Audit.DEFAULT_PASS];
const URL = artifacts.URL;
const mainResource = await MainResource.request({URL, devtoolsLog}, context);
const data = {trace, devtoolsLog, gatherContext, settings: context.settings, URL};
const summary = (await TimingSummary.request(data, context)).metrics;
const budget = Budget.getMatchingBudget(context.settings.budgets, mainResource.url);
if (!budget) {
return {
score: 0,
notApplicable: true,
};
}
/** @type {LH.Audit.Details.Table['headings']} */
const headers = [
{key: 'label', valueType: 'text', label: str_(UIStrings.columnTimingMetric)},
/**
* Note: SpeedIndex, unlike other timing metrics, is not measured in milliseconds.
* The renderer applies the correct units to the 'measurement' and 'overBudget' columns for SpeedIndex.
*/
{key: 'measurement', valueType: 'ms', label: str_(UIStrings.columnMeasurement)},
{key: 'overBudget', valueType: 'ms', label: str_(i18n.UIStrings.columnOverBudget)},
];
return {
details: Audit.makeTableDetails(headers, this.tableItems(budget, summary),
{sortedBy: ['overBudget']}),
score: 1,
};
}
}
export default TimingBudget;
export {UIStrings};