Skip to content
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

fixes for TTFB #2231

Merged
merged 12 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
71 changes: 71 additions & 0 deletions lighthouse-core/audits/time-to-first-byte.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @license Copyright 2017 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 Audit = require('./audit');
const Util = require('../report/v2/renderer/util');

const TTFB_THRESHOLD = 600;

class TTFBMetric extends Audit {
/**
* @return {!AuditMeta}
*/
static get meta() {
return {
category: 'Performance',
name: 'time-to-first-byte',
description: 'Keep server response times low (TTFB)',
informative: true,
helpText: 'Time To First Byte identifies the time at which your server sends a response.' +
' [Learn more](https://developers.google.com/web/tools/chrome-devtools/network-performance/issues).',
requiredArtifacts: ['devtoolsLogs', 'URL']
};
}

static caclulateTTFB(record) {
const timing = record._timing;

return timing.receiveHeadersEnd - timing.sendEnd;
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
*/
static audit(artifacts) {
const devtoolsLogs = artifacts.devtoolsLogs[Audit.DEFAULT_PASS];

return artifacts.requestNetworkRecords(devtoolsLogs)
.then((networkRecords) => {
let debugString = '';

const finalUrl = artifacts.URL.finalUrl;
const finalUrlRequest = networkRecords.find(record => record._url === finalUrl);
const ttfb = TTFBMetric.caclulateTTFB(finalUrlRequest);
const passed = ttfb < TTFB_THRESHOLD;

if (!passed) {
debugString = `Root document took ${Util.formatMilliseconds(ttfb, 1)} ms` +
'to get the first byte.';
}

return {
rawValue: ttfb,
score: passed,
displayValue: Util.formatMilliseconds(ttfb),
extendedInfo: {
value: {
wastedMs: ttfb - TTFB_THRESHOLD,
},
},
debugString,
};
});
}
}

module.exports = TTFBMetric;
98 changes: 0 additions & 98 deletions lighthouse-core/audits/time-to-firstbyte.js

This file was deleted.

4 changes: 2 additions & 2 deletions lighthouse-core/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module.exports = {
'speed-index-metric',
'screenshot-thumbnails',
'estimated-input-latency',
// 'time-to-firstbyte',
'time-to-first-byte',
'first-interactive',
'consistently-interactive',
'user-timings',
Expand Down Expand Up @@ -232,7 +232,7 @@ module.exports = {
{id: 'uses-optimized-images', weight: 0, group: 'perf-hint'},
{id: 'uses-webp-images', weight: 0, group: 'perf-hint'},
{id: 'uses-request-compression', weight: 0, group: 'perf-hint'},
// {id: 'time-to-firstbyte', weight: 0, group: 'perf-hint'},
{id: 'time-to-first-byte', weight: 0, group: 'perf-hint'},
{id: 'total-byte-weight', weight: 0, group: 'perf-info'},
{id: 'dom-size', weight: 0, group: 'perf-info'},
{id: 'critical-request-chains', weight: 0, group: 'perf-info'},
Expand Down
48 changes: 48 additions & 0 deletions lighthouse-core/test/audits/time-to-first-byte-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @license Copyright 2017 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 TimeToFirstByte = require('../../audits/time-to-first-byte.js');
const assert = require('assert');

/* eslint-env mocha */
describe('Performance: time-to-first-byte audit', () => {
it('fails when ttfb of root document is higher than 600ms', () => {
const networkRecords = [
{_url: 'https://example.com/', _requestId: '0', _timing: {receiveHeadersEnd: 830, sendEnd: 200}},
{_url: 'https://google.com/styles.css', _requestId: '1', _timing: {receiveHeadersEnd: 450, sendEnd: 200}},
{_url: 'https://google.com/image.jpg', _requestId: '2', _timing: {receiveHeadersEnd: 600, sendEnd: 400}},
];
const artifacts = {
devtoolsLogs: {[TimeToFirstByte.DEFAULT_PASS]: []},
requestNetworkRecords: () => Promise.resolve(networkRecords),
URL: {finalUrl: 'https://example.com/'},
};

return TimeToFirstByte.audit(artifacts).then(result => {
assert.strictEqual(result.rawValue, 630);
assert.strictEqual(result.score, false);
});
});

it('succeeds when ttfb of root document is lower than 600ms', () => {
const networkRecords = [
{_url: 'https://example.com/', _requestId: '0', _timing: {receiveHeadersEnd: 400, sendEnd: 200}},
{_url: 'https://google.com/styles.css', _requestId: '1', _timing: {receiveHeadersEnd: 850, sendEnd: 200}},
{_url: 'https://google.com/image.jpg', _requestId: '2', _timing: {receiveHeadersEnd: 1000, sendEnd: 400}},
];
const artifacts = {
devtoolsLogs: {[TimeToFirstByte.DEFAULT_PASS]: []},
requestNetworkRecords: () => Promise.resolve(networkRecords),
URL: {finalUrl: 'https://example.com/'},
};

return TimeToFirstByte.audit(artifacts).then(result => {
assert.strictEqual(result.rawValue, 200);
assert.strictEqual(result.score, true);
});
});
});
79 changes: 0 additions & 79 deletions lighthouse-core/test/audits/time-to-firstbyte-test.js

This file was deleted.