forked from checkinholiday/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.js
71 lines (63 loc) · 1.98 KB
/
metrics.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
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {Audit} from './audit.js';
import {TimingSummary} from '../computed/metrics/timing-summary.js';
/** @type {Set<keyof LH.Artifacts.TimingSummary>} */
const DECIMAL_METRIC_KEYS = new Set([
'cumulativeLayoutShift',
'cumulativeLayoutShiftMainFrame',
'observedCumulativeLayoutShift',
'observedCumulativeLayoutShiftMainFrame',
]);
class Metrics extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'metrics',
scoreDisplayMode: Audit.SCORING_MODES.INFORMATIVE,
title: 'Metrics',
description: 'Collects all available metrics.',
supportedModes: ['navigation'],
requiredArtifacts: ['traces', 'devtoolsLogs', 'GatherContext', 'URL'],
};
}
/**
* @param {LH.Artifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<LH.Audit.Product>}
*/
static async audit(artifacts, context) {
const gatherContext = artifacts.GatherContext;
const trace = artifacts.traces[Audit.DEFAULT_PASS];
const devtoolsLog = artifacts.devtoolsLogs[Audit.DEFAULT_PASS];
const URL = artifacts.URL;
const summary = await TimingSummary
.request({trace, devtoolsLog, gatherContext, settings: context.settings, URL}, context);
const metrics = summary.metrics;
const debugInfo = summary.debugInfo;
for (const [name, value] of Object.entries(metrics)) {
const key = /** @type {keyof LH.Artifacts.TimingSummary} */ (name);
if (typeof value === 'number' && !DECIMAL_METRIC_KEYS.has(key)) {
metrics[key] = Math.round(value);
}
}
/** @type {LH.Audit.Details.DebugData} */
const details = {
type: 'debugdata',
// TODO: Consider not nesting metrics under `items`.
items: [metrics, debugInfo],
};
return {
score: 1,
numericValue: metrics.interactive || 0,
numericUnit: 'millisecond',
details,
};
}
}
export default Metrics;