diff --git a/lighthouse-core/gather/driver.js b/lighthouse-core/gather/driver.js index f96df0b09ae0..07717411d4ac 100644 --- a/lighthouse-core/gather/driver.js +++ b/lighthouse-core/gather/driver.js @@ -916,13 +916,24 @@ class Driver { } /** - * @param {{additionalTraceCategories?: string|null}=} settings + * @param {LH.Gatherer.PassContext} passContext * @return {Promise} */ - beginTrace(settings) { + beginTrace(passContext) { + const settings = passContext.settings; const additionalCategories = (settings && settings.additionalTraceCategories && settings.additionalTraceCategories.split(',')) || []; const traceCategories = this._traceCategories.concat(additionalCategories); + + // In Chrome 71+, we trade the chatty 'toplevel' cat for our own, reducing overall trace size + // TODO(COMPAT): Once m71 ships to stable, change the traceCategories for real + const reChrome71 = /Chrome\/(Headless)?7[1-9]/; + if (passContext.hostUserAgent && reChrome71.test(passContext.hostUserAgent)) { + traceCategories.forEach((cat, idx) => { + if (cat === 'toplevel') traceCategories[idx] = 'disabled-by-default-lighthouse'; + }); + } + const uniqueCategories = Array.from(new Set(traceCategories)); // Check any domains that could interfere with or add overhead to the trace. diff --git a/lighthouse-core/gather/gather-runner.js b/lighthouse-core/gather/gather-runner.js index 918aa0e13553..3af0c0a046ff 100644 --- a/lighthouse-core/gather/gather-runner.js +++ b/lighthouse-core/gather/gather-runner.js @@ -211,11 +211,10 @@ class GatherRunner { static async pass(passContext, gathererResults) { const driver = passContext.driver; const config = passContext.passConfig; - const settings = passContext.settings; const gatherers = config.gatherers; - const recordTrace = config.recordTrace; - const isPerfRun = !settings.disableStorageReset && recordTrace && config.useThrottling; + const isPerfRun = !passContext.settings.disableStorageReset && recordTrace && + config.useThrottling; const gatherernames = gatherers.map(g => g.instance.name).join(', '); const status = 'Loading page & waiting for onload'; @@ -226,7 +225,7 @@ class GatherRunner { // Always record devtoolsLog await driver.beginDevtoolsLog(); // Begin tracing if requested by config. - if (recordTrace) await driver.beginTrace(settings); + if (recordTrace) await driver.beginTrace(passContext); // Navigate. await GatherRunner.loadPage(driver, passContext); @@ -402,6 +401,7 @@ class GatherRunner { // If the main document redirects, we'll update this to keep track url: options.requestedUrl, settings: options.settings, + hostUserAgent: baseArtifacts.HostUserAgent, passConfig, // *pass() functions and gatherers can push to this warnings array. LighthouseRunWarnings: baseArtifacts.LighthouseRunWarnings, diff --git a/lighthouse-core/test/gather/driver-test.js b/lighthouse-core/test/gather/driver-test.js index 0f04bdc230c4..cddc07a3df53 100644 --- a/lighthouse-core/test/gather/driver-test.js +++ b/lighthouse-core/test/gather/driver-test.js @@ -224,7 +224,7 @@ describe('Browser Driver', () => { }); it('will request default traceCategories', () => { - return driverStub.beginTrace().then(() => { + return driverStub.beginTrace({settings: {}}).then(() => { const traceCmd = sendCommandParams.find(obj => obj.command === 'Tracing.start'); const categories = traceCmd.params.categories; assert.ok(categories.includes('devtools.timeline'), 'contains devtools.timeline'); @@ -232,7 +232,8 @@ describe('Browser Driver', () => { }); it('will use requested additionalTraceCategories', () => { - return driverStub.beginTrace({additionalTraceCategories: 'v8,v8.execute,toplevel'}).then(() => { + const passContext = {settings: {additionalTraceCategories: 'v8,v8.execute,toplevel'}}; + return driverStub.beginTrace(passContext).then(() => { const traceCmd = sendCommandParams.find(obj => obj.command === 'Tracing.start'); const categories = traceCmd.params.categories; assert.ok(categories.includes('blink'), 'contains default categories'); @@ -242,6 +243,18 @@ describe('Browser Driver', () => { }); }); + + it('will adjust traceCategories based on host user agent', () => { + // eslint-disable-next-line max-len + const passContext = {hostUserAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/Headless71.0.3561.0 Safari/537.36'}; + return driverStub.beginTrace(passContext).then(() => { + const traceCmd = sendCommandParams.find(obj => obj.command === 'Tracing.start'); + const categories = traceCmd.params.categories; + assert.ok(categories.includes('disabled-by-default-lighthouse'), 'contains new categories'); + assert.equal(categories.indexOf('toplevel'), -1, 'excludes toplevel'); + }); + }); + it('should send the Network.setExtraHTTPHeaders command when there are extra-headers', () => { return driverStub.setExtraHTTPHeaders({ 'Cookie': 'monster', diff --git a/typings/gatherer.d.ts b/typings/gatherer.d.ts index db4755b36939..f0a9fc1efadc 100644 --- a/typings/gatherer.d.ts +++ b/typings/gatherer.d.ts @@ -18,6 +18,7 @@ declare global { passConfig: Config.Pass settings: Config.Settings; options?: object; + hostUserAgent: string; /** Push to this array to add top-level warnings to the LHR. */ LighthouseRunWarnings: Array; }