From 1826c75036a3f1a6ff98e3a150ca54f5d761c99f Mon Sep 17 00:00:00 2001 From: Adam Raine <6752989+adamraine@users.noreply.github.com> Date: Mon, 23 Jan 2023 17:14:37 -0800 Subject: [PATCH] core(trace-processing): add backport for pubads (#14700) --- .../smokehouse/test-definitions/pubads.js | 22 ++++++++++++++++ core/computed/processed-navigation.js | 25 ++++++++++++++----- .../computed/processed-navigation-test.js | 18 +++++++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/cli/test/smokehouse/test-definitions/pubads.js b/cli/test/smokehouse/test-definitions/pubads.js index 02701e26b8..03409398cd 100644 --- a/cli/test/smokehouse/test-definitions/pubads.js +++ b/cli/test/smokehouse/test-definitions/pubads.js @@ -21,7 +21,29 @@ const expectations = { runWarnings: {length: '>0'}, audits: { // We just want to ensure the plugin had a chance to run without error. + 'tag-load-time': {scoreDisplayMode: 'notApplicable'}, + 'bid-request-from-page-start': {scoreDisplayMode: 'notApplicable'}, + 'ad-request-from-page-start': {scoreDisplayMode: 'notApplicable'}, + 'first-ad-render': {scoreDisplayMode: 'notApplicable'}, + 'cumulative-ad-shift': {scoreDisplayMode: 'notApplicable'}, + 'total-ad-blocking-time': {scoreDisplayMode: 'notApplicable'}, + 'gpt-bids-parallel': {scoreDisplayMode: 'notApplicable'}, + 'serial-header-bidding': {scoreDisplayMode: 'notApplicable'}, + 'bottleneck-requests': {scoreDisplayMode: 'notApplicable'}, + 'script-injected-tags': {scoreDisplayMode: 'notApplicable'}, + 'blocking-load-events': {scoreDisplayMode: 'notApplicable'}, 'ad-render-blocking-resources': {scoreDisplayMode: 'notApplicable'}, + 'ad-blocking-tasks': {scoreDisplayMode: 'notApplicable'}, + 'ad-request-critical-path': {scoreDisplayMode: 'notApplicable'}, + 'ads-in-viewport': {scoreDisplayMode: 'notApplicable'}, + 'async-ad-tags': {scoreDisplayMode: 'notApplicable'}, + 'loads-ad-tag-over-https': {scoreDisplayMode: 'notApplicable'}, + 'loads-gpt-from-official-source': {scoreDisplayMode: 'notApplicable'}, + 'viewport-ad-density': {scoreDisplayMode: 'notApplicable'}, + 'ad-top-of-viewport': {scoreDisplayMode: 'notApplicable'}, + 'duplicate-tags': {scoreDisplayMode: 'notApplicable'}, + 'deprecated-gpt-api-usage': {scoreDisplayMode: 'notApplicable'}, + 'gpt-errors-overall': {scoreDisplayMode: 'notApplicable'}, }, }, }; diff --git a/core/computed/processed-navigation.js b/core/computed/processed-navigation.js index f2e602de52..81208396b2 100644 --- a/core/computed/processed-navigation.js +++ b/core/computed/processed-navigation.js @@ -10,12 +10,25 @@ import LHTraceProcessor from '../lib/lh-trace-processor.js'; class ProcessedNavigation { /** - * @param {LH.Trace} trace - * @param {LH.Artifacts.ComputedContext} context - * @return {Promise} - */ - static async compute_(trace, context) { - const processedTrace = await ProcessedTrace.request(trace, context); + * @param {LH.Trace | LH.Artifacts.ProcessedTrace} traceOrProcessedTrace + * @return {traceOrProcessedTrace is LH.Artifacts.ProcessedTrace} + */ + static isProcessedTrace(traceOrProcessedTrace) { + return 'timeOriginEvt' in traceOrProcessedTrace; + } + + /** + * @param {LH.Trace | LH.Artifacts.ProcessedTrace} traceOrProcessedTrace + * @param {LH.Artifacts.ComputedContext} context + * @return {Promise} + */ + static async compute_(traceOrProcessedTrace, context) { + // TODO: Remove this backport once pubads passes in a raw trace. + if (this.isProcessedTrace(traceOrProcessedTrace)) { + return LHTraceProcessor.processNavigation(traceOrProcessedTrace); + } + + const processedTrace = await ProcessedTrace.request(traceOrProcessedTrace, context); return LHTraceProcessor.processNavigation(processedTrace); } } diff --git a/core/test/computed/processed-navigation-test.js b/core/test/computed/processed-navigation-test.js index 8944ade584..05ef8aa690 100644 --- a/core/test/computed/processed-navigation-test.js +++ b/core/test/computed/processed-navigation-test.js @@ -5,6 +5,7 @@ */ import {ProcessedNavigation} from '../../computed/processed-navigation.js'; +import {ProcessedTrace} from '../../computed/processed-trace.js'; import {readJson} from '../test-utils.js'; const pwaTrace = readJson('../fixtures/traces/progressive-app-m60.json', import.meta); @@ -117,6 +118,23 @@ describe('ProcessedTrace', () => { }); }); + it('accepts a processed trace as input', async () => { + const context = {computedCache: new Map()}; + const processedTrace = await ProcessedTrace.request(pwaTrace, context); + const processedNavigation = await ProcessedNavigation.request(processedTrace, context); + + expect(processedNavigation.timings).toEqual({ + domContentLoaded: 560.294, + firstContentfulPaint: 498.87, + firstContentfulPaintAllFrames: 498.87, + firstMeaningfulPaint: 783.328, + firstPaint: 498.853, + load: 2198.898, + timeOrigin: 0, + traceEnd: 12539.872, + }); + }); + it('fails with NO_NAVSTART', async () => { const context = {computedCache: new Map()}; const compute = async () => {