From ad3f39e3574a436f60b156d83149f94787ba45d2 Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Wed, 28 Apr 2021 15:19:53 -0500 Subject: [PATCH] tests: add organic TTI savings case to byte efficieny audit (#12418) --- .../byte-efficiency-audit-test.js.snap | 12 ----- .../byte-efficiency-audit-test.js | 51 +++++++++++++++---- 2 files changed, 40 insertions(+), 23 deletions(-) delete mode 100644 lighthouse-core/test/audits/byte-efficiency/__snapshots__/byte-efficiency-audit-test.js.snap diff --git a/lighthouse-core/test/audits/byte-efficiency/__snapshots__/byte-efficiency-audit-test.js.snap b/lighthouse-core/test/audits/byte-efficiency/__snapshots__/byte-efficiency-audit-test.js.snap deleted file mode 100644 index 53c04e346674..000000000000 --- a/lighthouse-core/test/audits/byte-efficiency/__snapshots__/byte-efficiency-audit-test.js.snap +++ /dev/null @@ -1,12 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Byte efficiency base audit should allow overriding of computeWasteWithTTIGraph 1`] = ` -Object { - "default": 560, - "justTTI": 504, -} -`; - -exports[`Byte efficiency base audit should create load simulator with the specified settings 1`] = `960`; - -exports[`Byte efficiency base audit should create load simulator with the specified settings 2`] = `21500`; diff --git a/lighthouse-core/test/audits/byte-efficiency/byte-efficiency-audit-test.js b/lighthouse-core/test/audits/byte-efficiency/byte-efficiency-audit-test.js index 75e2b40904fa..fe6c9ed74447 100644 --- a/lighthouse-core/test/audits/byte-efficiency/byte-efficiency-audit-test.js +++ b/lighthouse-core/test/audits/byte-efficiency/byte-efficiency-audit-test.js @@ -15,6 +15,8 @@ const LoadSimulator = require('../../../computed/load-simulator.js'); const trace = require('../../fixtures/traces/progressive-app-m60.json'); const devtoolsLog = require('../../fixtures/traces/progressive-app-m60.devtools.log.json'); +const traceM78 = require('../../fixtures/traces/lcp-m78.json'); +const devtoolsLogM78 = require('../../fixtures/traces/lcp-m78.devtools.log.json'); const assert = require('assert').strict; /* eslint-env jest */ @@ -230,16 +232,16 @@ describe('Byte efficiency base audit', () => { let result = await MockAudit.audit(artifacts, {settings, computedCache}); // expect modest savings expect(result.numericValue).toBeLessThan(5000); - expect(result.numericValue).toMatchSnapshot(); + expect(result.numericValue).toMatchInlineSnapshot(`960`); settings = {throttlingMethod: 'simulate', throttling: ultraSlowThrottling}; result = await MockAudit.audit(artifacts, {settings, computedCache}); // expect lots of savings expect(result.numericValue).not.toBeLessThan(5000); - expect(result.numericValue).toMatchSnapshot(); + expect(result.numericValue).toMatchInlineSnapshot(`21500`); }); - it('should allow overriding of computeWasteWithTTIGraph', async () => { + it('should compute TTI savings differently from load savings', async () => { class MockAudit extends ByteEfficiencyAudit { static audit_(artifacts, records) { return { @@ -249,15 +251,44 @@ describe('Byte efficiency base audit', () => { } } - class MockJustTTIAudit extends MockAudit { + class MockTtiAudit extends MockAudit { static computeWasteWithTTIGraph(results, graph, simulator) { - // TODO: Pass in a graph that organically has a lower TTI result rather than forcing it - // to be scaled down. - return 0.9 * ByteEfficiencyAudit.computeWasteWithTTIGraph(results, graph, simulator, + return ByteEfficiencyAudit.computeWasteWithTTIGraph(results, graph, simulator, {includeLoad: false}); } } + const artifacts = { + traces: {defaultPass: traceM78}, + devtoolsLogs: {defaultPass: devtoolsLogM78}, + }; + const computedCache = new Map(); + + const modestThrottling = {rttMs: 150, throughputKbps: 1000, cpuSlowdownMultiplier: 2}; + const settings = {throttlingMethod: 'simulate', throttling: modestThrottling}; + const result = await MockAudit.audit(artifacts, {settings, computedCache}); + const resultTti = await MockTtiAudit.audit(artifacts, {settings, computedCache}); + expect(resultTti.numericValue).toBeLessThan(result.numericValue); + expect(result.numericValue).toMatchInlineSnapshot(`2120`); + expect(resultTti.numericValue).toMatchInlineSnapshot(`150`); + }); + + it('should allow overriding of computeWasteWithTTIGraph', async () => { + class MockAudit extends ByteEfficiencyAudit { + static audit_(artifacts, records) { + return { + items: records.map(record => ({url: record.url, wastedBytes: record.transferSize * 0.5})), + headings: [], + }; + } + } + + class MockOverrideAudit extends MockAudit { + static computeWasteWithTTIGraph(results, graph, simulator) { + return 0.5 * ByteEfficiencyAudit.computeWasteWithTTIGraph(results, graph, simulator); + } + } + const artifacts = { traces: {defaultPass: trace}, devtoolsLogs: {defaultPass: devtoolsLog}, @@ -267,9 +298,7 @@ describe('Byte efficiency base audit', () => { const modestThrottling = {rttMs: 150, throughputKbps: 1000, cpuSlowdownMultiplier: 2}; const settings = {throttlingMethod: 'simulate', throttling: modestThrottling}; const result = await MockAudit.audit(artifacts, {settings, computedCache}); - const resultTti = await MockJustTTIAudit.audit(artifacts, {settings, computedCache}); - // expect less savings with just TTI - expect(resultTti.numericValue).toBeLessThan(result.numericValue); - expect({default: result.numericValue, justTTI: resultTti.numericValue}).toMatchSnapshot(); + const resultOverride = await MockOverrideAudit.audit(artifacts, {settings, computedCache}); + expect(resultOverride.numericValue).toEqual(result.numericValue * 0.5); }); });