Skip to content

Commit

Permalink
core(ttfb): reuse requestMainResource (#5657)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce authored and paulirish committed Jul 21, 2018
1 parent b2100d6 commit 0265569
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 70 deletions.
60 changes: 23 additions & 37 deletions lighthouse-core/audits/time-to-first-byte.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,32 @@ class TTFBMetric extends Audit {
* @param {LH.Artifacts} artifacts
* @return {Promise<LH.Audit.Product>}
*/
static audit(artifacts) {
const devtoolsLogs = artifacts.devtoolsLogs[Audit.DEFAULT_PASS];
static async audit(artifacts) {
const devtoolsLog = artifacts.devtoolsLogs[Audit.DEFAULT_PASS];
const mainResource = await artifacts.requestMainResource({devtoolsLog, URL: artifacts.URL});

return artifacts.requestNetworkRecords(devtoolsLogs)
.then((networkRecords) => {
/** @type {LH.Audit.DisplayValue} */
let displayValue = '';
const ttfb = TTFBMetric.caclulateTTFB(mainResource);
const passed = ttfb < TTFB_THRESHOLD;

const finalUrl = artifacts.URL.finalUrl;
const finalUrlRequest = networkRecords.find(record => record.url === finalUrl);
if (!finalUrlRequest) {
throw new Error(`finalUrl '${finalUrl} not found in network records.`);
}
const ttfb = TTFBMetric.caclulateTTFB(finalUrlRequest);
const passed = ttfb < TTFB_THRESHOLD;

if (!passed) {
displayValue = ['Root document took %10d', ttfb];
}

/** @type {LH.Result.Audit.OpportunityDetails} */
const details = {
type: 'opportunity',
overallSavingsMs: ttfb - TTFB_THRESHOLD,
headings: [],
items: [],
};
/** @type {LH.Result.Audit.OpportunityDetails} */
const details = {
type: 'opportunity',
overallSavingsMs: ttfb - TTFB_THRESHOLD,
headings: [],
items: [],
};

return {
rawValue: ttfb,
score: Number(passed),
displayValue,
details,
extendedInfo: {
value: {
wastedMs: ttfb - TTFB_THRESHOLD,
},
},
};
});
return {
rawValue: ttfb,
score: Number(passed),
displayValue: passed ? '' : ['Root document took %10d', ttfb],
details,
extendedInfo: {
value: {
wastedMs: ttfb - TTFB_THRESHOLD,
},
},
};
}
}

Expand Down
47 changes: 14 additions & 33 deletions lighthouse-core/test/audits/time-to-first-byte-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ const assert = require('assert');
/* eslint-env jest */
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 mainResource = {
url: 'https://example.com/',
requestId: '0',
timing: {receiveHeadersEnd: 830, sendEnd: 200},
};

const artifacts = {
devtoolsLogs: {[TimeToFirstByte.DEFAULT_PASS]: []},
requestNetworkRecords: () => Promise.resolve(networkRecords),
requestMainResource: () => Promise.resolve(mainResource),
URL: {finalUrl: 'https://example.com/'},
};

Expand All @@ -29,14 +30,15 @@ describe('Performance: time-to-first-byte audit', () => {
});

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 mainResource = {
url: 'https://example.com/',
requestId: '0',
timing: {receiveHeadersEnd: 400, sendEnd: 200},
};

const artifacts = {
devtoolsLogs: {[TimeToFirstByte.DEFAULT_PASS]: []},
requestNetworkRecords: () => Promise.resolve(networkRecords),
requestMainResource: () => Promise.resolve(mainResource),
URL: {finalUrl: 'https://example.com/'},
};

Expand All @@ -45,25 +47,4 @@ describe('Performance: time-to-first-byte audit', () => {
assert.strictEqual(result.score, 1);
});
});

it('throws when somehow finalUrl is not in network records', async () => {
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 badFinalUrl = 'https://badexample.com/';
const artifacts = {
devtoolsLogs: {[TimeToFirstByte.DEFAULT_PASS]: []},
requestNetworkRecords: () => Promise.resolve(networkRecords),
URL: {finalUrl: badFinalUrl},
};

try {
await TimeToFirstByte.audit(artifacts);
throw new Error('TimeToFirstByte did not throw');
} catch (e) {
assert.ok(e.message.includes(badFinalUrl));
}
});
});

0 comments on commit 0265569

Please sign in to comment.