Skip to content

Commit

Permalink
core(driver): request smaller trace in m71+
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Sep 26, 2018
1 parent 3a6f6c5 commit e9a4b6e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
15 changes: 13 additions & 2 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -916,13 +916,24 @@ class Driver {
}

/**
* @param {{additionalTraceCategories?: string|null}=} settings
* @param {LH.Gatherer.PassContext} passContext
* @return {Promise<void>}
*/
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.
Expand Down
8 changes: 4 additions & 4 deletions lighthouse-core/gather/gather-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 15 additions & 2 deletions lighthouse-core/test/gather/driver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,16 @@ 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');
});
});

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');
Expand All @@ -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',
Expand Down
1 change: 1 addition & 0 deletions typings/gatherer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
}
Expand Down

0 comments on commit e9a4b6e

Please sign in to comment.