From 0265569b1970aa7383c4e12909205e1c8d8f3d6a Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Fri, 20 Jul 2018 19:07:27 -0700 Subject: [PATCH] core(ttfb): reuse requestMainResource (#5657) --- lighthouse-core/audits/time-to-first-byte.js | 60 +++++++------------ .../test/audits/time-to-first-byte-test.js | 47 +++++---------- 2 files changed, 37 insertions(+), 70 deletions(-) diff --git a/lighthouse-core/audits/time-to-first-byte.js b/lighthouse-core/audits/time-to-first-byte.js index 244ac31af37f..3bbebbc0c84e 100644 --- a/lighthouse-core/audits/time-to-first-byte.js +++ b/lighthouse-core/audits/time-to-first-byte.js @@ -35,46 +35,32 @@ class TTFBMetric extends Audit { * @param {LH.Artifacts} artifacts * @return {Promise} */ - 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, + }, + }, + }; } } diff --git a/lighthouse-core/test/audits/time-to-first-byte-test.js b/lighthouse-core/test/audits/time-to-first-byte-test.js index 5a668ae7dc75..d1f4258c07b5 100644 --- a/lighthouse-core/test/audits/time-to-first-byte-test.js +++ b/lighthouse-core/test/audits/time-to-first-byte-test.js @@ -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/'}, }; @@ -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/'}, }; @@ -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)); - } - }); });