Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support skipping cache and storage clear #1675

Merged
merged 3 commits into from
Feb 10, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lighthouse-cli/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const cliFlags = yargs
'port'
], 'Configuration:')
.describe({
'disable-cache-clearing': 'Disable clearing of cache and storage before the run',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other name ideas welcome, cache is more specific than what actually happens but is probably more familiar/understandable to users than a generic storage

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disable-storage-reset comes to mind. I actually think being more generic than "cache" is good for the reasons you mention. It applies to all forms of storage on the origin.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

'disable-device-emulation': 'Disable Nexus 5X emulation',
'disable-cpu-throttling': 'Disable CPU throttling',
'disable-network-throttling': 'Disable network throttling',
Expand Down Expand Up @@ -92,6 +93,7 @@ Example: --output-path=./lighthouse-results.html`

// boolean values
.boolean([
'disable-cache-clearing',
'disable-device-emulation',
'disable-cpu-throttling',
'disable-network-throttling',
Expand Down
5 changes: 3 additions & 2 deletions lighthouse-core/gather/gather-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ class GatherRunner {

static setupDriver(driver, options) {
log.log('status', 'Initializing…');
const clearCache = !(options.flags && options.flags.disableCacheClearing);
// Enable emulation based on flags
return driver.assertNoSameOriginServiceWorkerClients(options.url)
.then(_ => driver.beginEmulation(options.flags))
.then(_ => driver.enableRuntimeEvents())
.then(_ => driver.cacheNatives())
.then(_ => driver.cleanAndDisableBrowserCaches())
.then(_ => driver.clearDataForOrigin(options.url))
.then(_ => clearCache && driver.cleanAndDisableBrowserCaches())
.then(_ => clearCache && driver.clearDataForOrigin(options.url))
.then(_ => driver.blockUrlPatterns(options.flags.blockedUrlPatterns || []));
}

Expand Down
54 changes: 54 additions & 0 deletions lighthouse-core/test/gather/gather-runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,60 @@ describe('GatherRunner', function() {
});
});

it('clears the network cache and origin storage', () => {
const asyncFunc = () => Promise.resolve();
const tests = {
calledDisableNetworkCache: false,
calledClearStorage: false,
};
const createCheck = variable => () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to replace fake-driver with a nice interface around something like this :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or we could use sinon :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I'd be fine with that too.

Honestly I hate these tests involving the driver. Rely too much on knowledge of internal implementation instead of externally observable behavior. If we're going to assume the driver calls made and the order they're made in, I'd rather we just setup a debugger protocol record/replay system :)

tests[variable] = true;
return Promise.resolve();
};
const driver = {
assertNoSameOriginServiceWorkerClients: asyncFunc,
beginEmulation: asyncFunc,
enableRuntimeEvents: asyncFunc,
cacheNatives: asyncFunc,
cleanAndDisableBrowserCaches: createCheck('calledDisableNetworkCache'),
clearDataForOrigin: createCheck('calledClearStorage'),
blockUrlPatterns: asyncFunc,
};

return GatherRunner.setupDriver(driver, {flags: {}}).then(_ => {
assert.equal(tests.calledDisableNetworkCache, true);
assert.equal(tests.calledClearStorage, true);
});
});

it('does not clear the network cache and origin storage when flag is set', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...when disable-storage-reset flag is set

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

const asyncFunc = () => Promise.resolve();
const tests = {
calledDisableNetworkCache: false,
calledClearStorage: false,
};
const createCheck = variable => () => {
tests[variable] = true;
return Promise.resolve();
};
const driver = {
assertNoSameOriginServiceWorkerClients: asyncFunc,
beginEmulation: asyncFunc,
enableRuntimeEvents: asyncFunc,
cacheNatives: asyncFunc,
cleanAndDisableBrowserCaches: createCheck('calledDisableNetworkCache'),
clearDataForOrigin: createCheck('calledClearStorage'),
blockUrlPatterns: asyncFunc,
};

return GatherRunner.setupDriver(driver, {
flags: {disableCacheClearing: true}
}).then(_ => {
assert.equal(tests.calledDisableNetworkCache, false);
assert.equal(tests.calledClearStorage, false);
});
});

it('tells the driver to block given URL patterns when blockedUrlPatterns is given', () => {
const receivedUrlPatterns = [];
const urlPatterns = ['http://*.evil.com', '.jpg', '.woff2'];
Expand Down