-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
server-response-time.js
107 lines (92 loc) · 4.48 KB
/
server-response-time.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
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {Audit} from './audit.js';
import * as i18n from '../lib/i18n/i18n.js';
import {MainResource} from '../computed/main-resource.js';
const UIStrings = {
/** Title of a diagnostic audit that provides detail on how long it took from starting a request to when the server started responding. This descriptive title is shown to users when the amount is acceptable and no user action is required. */
title: 'Initial server response time was short',
/** Title of a diagnostic audit that provides detail on how long it took from starting a request to when the server started responding. This imperative title is shown to users when there is a significant amount of execution time that could be reduced. */
failureTitle: 'Reduce initial server response time',
/** Description of a Lighthouse audit that tells the user *why* they should reduce the amount of time it takes their server to start responding to requests. This is displayed after a user expands the section to see more. No character length limits. The last sentence starting with 'Learn' becomes link text to additional documentation. */
description: 'Keep the server response time for the main document short because all other requests depend on it. [Learn more about the Time to First Byte metric](https://developer.chrome.com/docs/lighthouse/performance/time-to-first-byte/).',
/** Used to summarize the total Server Response Time duration for the primary HTML response. The `{timeInMs}` placeholder will be replaced with the time duration, shown in milliseconds (e.g. 210 ms) */
displayValue: `Root document took {timeInMs, number, milliseconds}\xa0ms`,
};
const str_ = i18n.createIcuMessageFn(import.meta.url, UIStrings);
// Due to the way that DevTools throttling works we cannot see if server response took less than ~570ms.
// We set our failure threshold to 600ms to avoid those false positives but we want devs to shoot for 100ms.
const TOO_SLOW_THRESHOLD_MS = 600;
const TARGET_MS = 100;
class ServerResponseTime extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'server-response-time',
title: str_(UIStrings.title),
failureTitle: str_(UIStrings.failureTitle),
description: str_(UIStrings.description),
supportedModes: ['navigation'],
guidanceLevel: 1,
requiredArtifacts: ['devtoolsLogs', 'URL', 'GatherContext'],
scoreDisplayMode: Audit.SCORING_MODES.METRIC_SAVINGS,
};
}
/**
* @param {LH.Artifacts.NetworkRequest} record
* @return {number|null}
*/
static calculateResponseTime(record) {
// Lightrider does not have timings for sendEnd, but we do have this timing which should be
// close to the response time.
if (global.isLightrider && record.lrStatistics) return record.lrStatistics.requestMs;
if (!record.timing) return null;
return record.timing.receiveHeadersStart - record.timing.sendEnd;
}
/**
* @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];
/** @type {LH.Artifacts.NetworkRequest} */
const mainResource = await MainResource.request({devtoolsLog, URL: artifacts.URL}, context);
const responseTime = ServerResponseTime.calculateResponseTime(mainResource);
if (responseTime === null) {
throw new Error('no timing found for main resource');
}
const passed = responseTime < TOO_SLOW_THRESHOLD_MS;
const displayValue = str_(UIStrings.displayValue, {timeInMs: responseTime});
/** @type {LH.Audit.Details.Opportunity['headings']} */
const headings = [
{key: 'url', valueType: 'url', label: str_(i18n.UIStrings.columnURL)},
{key: 'responseTime', valueType: 'timespanMs', label: str_(i18n.UIStrings.columnTimeSpent)},
];
const overallSavingsMs = Math.max(responseTime - TARGET_MS, 0);
const details = Audit.makeOpportunityDetails(
headings,
[{url: mainResource.url, responseTime}],
{overallSavingsMs}
);
return {
numericValue: responseTime,
numericUnit: 'millisecond',
score: Number(passed),
scoreDisplayMode: passed ? Audit.SCORING_MODES.INFORMATIVE : undefined,
displayValue,
details,
metricSavings: {
FCP: overallSavingsMs,
LCP: overallSavingsMs,
},
};
}
}
export default ServerResponseTime;
export {UIStrings};