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

core: add BFCacheFailures artifact #14485

Merged
merged 25 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
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
1 change: 1 addition & 0 deletions core/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const artifacts = {
Trace: '',
Accessibility: '',
AnchorElements: '',
BFCacheFailures: '',
CacheContents: '',
ConsoleMessages: '',
CSSUsage: '',
Expand Down
2 changes: 1 addition & 1 deletion core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Driver {
/** @return {Promise<void>} */
async disconnect() {
if (this.defaultSession === throwingSession) return;
this._targetManager?.disable();
await this._targetManager?.disable();
await this.defaultSession.dispose();
}
}
Expand Down
17 changes: 12 additions & 5 deletions core/gather/driver/target-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,22 @@ class TargetManager extends ProtocolEventEmitter {
async _onFrameNavigated(frameNavigatedEvent) {
// Child frames are handled in `_onSessionAttached`.
if (frameNavigatedEvent.frame.parentId) return;
if (!this._enabled) return;

// It's not entirely clear when this is necessary, but when the page switches processes on
// navigating from about:blank to the `requestedUrl`, resetting `setAutoAttach` has been
// necessary in the past.
await this._rootCdpSession.send('Target.setAutoAttach', {
autoAttach: true,
flatten: true,
waitForDebuggerOnStart: true,
});
try {
await this._rootCdpSession.send('Target.setAutoAttach', {
autoAttach: true,
flatten: true,
waitForDebuggerOnStart: true,
});
} catch (err) {
// The page can be closed at the end of the run before this CDP function returns.
// In these cases, just ignore the error since we won't need the page anyway.
if (this._enabled) throw err;
}
}

/**
Expand Down
158 changes: 158 additions & 0 deletions core/gather/gatherers/bf-cache-failures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/**
* @license Copyright 2022 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

import FRGatherer from '../base-gatherer.js';
import {waitForFrameNavigated, waitForLoadEvent} from '../driver/wait-for-condition.js';
import DevtoolsLog from './devtools-log.js';

class BFCacheFailures extends FRGatherer {
/** @type {LH.Gatherer.GathererMeta<'DevtoolsLog'>} */
meta = {
supportedModes: ['navigation', 'timespan'],
dependencies: {DevtoolsLog: DevtoolsLog.symbol},
};

/**
* @param {LH.Crdp.Page.BackForwardCacheNotRestoredExplanation[]} errorList
* @return {LH.Artifacts.BFCacheFailure}
*/
static processBFCacheEventList(errorList) {
/** @type {LH.Artifacts.BFCacheNotRestoredReasonsTree} */
const notRestoredReasonsTree = {
Circumstantial: {},
PageSupportNeeded: {},
SupportPending: {},
};

for (const err of errorList) {
const bfCacheErrorsMap = notRestoredReasonsTree[err.type];
bfCacheErrorsMap[err.reason] = [];
}

return {notRestoredReasonsTree};
}

/**
* @param {LH.Crdp.Page.BackForwardCacheNotRestoredExplanationTree} errorTree
* @return {LH.Artifacts.BFCacheFailure}
*/
static processBFCacheEventTree(errorTree) {
/** @type {LH.Artifacts.BFCacheNotRestoredReasonsTree} */
const notRestoredReasonsTree = {
Circumstantial: {},
PageSupportNeeded: {},
SupportPending: {},
};

/**
* @param {LH.Crdp.Page.BackForwardCacheNotRestoredExplanationTree} node
*/
function traverse(node) {
for (const error of node.explanations) {
const bfCacheErrorsMap = notRestoredReasonsTree[error.type];
const frameUrls = bfCacheErrorsMap[error.reason] || [];
frameUrls.push(node.url);
bfCacheErrorsMap[error.reason] = frameUrls;
}

for (const child of node.children) {
traverse(child);
}
}

traverse(errorTree);

return {notRestoredReasonsTree};
}

/**
* @param {LH.Crdp.Page.BackForwardCacheNotUsedEvent|undefined} event
* @return {LH.Artifacts.BFCacheFailure}
*/
static processBFCacheEvent(event) {
if (event?.notRestoredExplanationsTree) {
return BFCacheFailures.processBFCacheEventTree(event.notRestoredExplanationsTree);
}
return BFCacheFailures.processBFCacheEventList(event?.notRestoredExplanations || []);
}

/**
* @param {LH.Gatherer.FRTransitionalContext} context
* @return {Promise<LH.Crdp.Page.BackForwardCacheNotUsedEvent|undefined>}
*/
async activelyCollectBFCacheEvent(context) {
const session = context.driver.defaultSession;

/** @type {LH.Crdp.Page.BackForwardCacheNotUsedEvent|undefined} */
let bfCacheEvent = undefined;

/**
* @param {LH.Crdp.Page.BackForwardCacheNotUsedEvent} event
*/
function onBfCacheNotUsed(event) {
bfCacheEvent = event;
}

session.on('Page.backForwardCacheNotUsed', onBfCacheNotUsed);

const history = await session.sendCommand('Page.getNavigationHistory');
const entry = history.entries[history.currentIndex];

await Promise.all([
session.sendCommand('Page.navigate', {url: 'chrome://terms'}),
adamraine marked this conversation as resolved.
Show resolved Hide resolved
waitForLoadEvent(session, 0).promise,
]);

await Promise.all([
session.sendCommand('Page.navigateToHistoryEntry', {entryId: entry.id}),
waitForFrameNavigated(session).promise,
]);

session.off('Page.backForwardCacheNotUsed', onBfCacheNotUsed);

return bfCacheEvent;
}

/**
* @param {LH.Gatherer.FRTransitionalContext<'DevtoolsLog'>} context
* @return {LH.Crdp.Page.BackForwardCacheNotUsedEvent[]}
*/
passivelyCollectBFCacheEvent(context) {
const events = [];
for (const event of context.dependencies.DevtoolsLog) {
if (event.method === 'Page.backForwardCacheNotUsed') {
events.push(event.params);
Copy link
Collaborator

Choose a reason for hiding this comment

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

will we need the urls associated with this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Potentially in the future if we start surfacing more than one failure. In that scenario, it would be easy to add a url parameter to Artifacts.BFCacheFailure. I don't think we should worry about it right now though.

}
}
return events;
}

/**
* @param {LH.Gatherer.FRTransitionalContext<'DevtoolsLog'>} context
* @return {Promise<LH.Artifacts['BFCacheFailures']>}
*/
async getArtifact(context) {
const events = this.passivelyCollectBFCacheEvent(context);
if (context.gatherMode === 'navigation') {
const activelyCollectedEvent = await this.activelyCollectBFCacheEvent(context);
if (activelyCollectedEvent) events.push(activelyCollectedEvent);
}

return events.map(BFCacheFailures.processBFCacheEvent);
}

/**
* @param {LH.Gatherer.PassContext} passContext
* @param {LH.Gatherer.LoadData} loadData
* @return {Promise<LH.Artifacts['BFCacheFailures']>}
*/
async afterPass(passContext, loadData) {
return this.getArtifact({...passContext, dependencies: {DevtoolsLog: loadData.devtoolsLog}});
}
}

export default BFCacheFailures;

1 change: 1 addition & 0 deletions core/legacy/config/legacy-default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ legacyDefaultConfig.passes = [{
'inspector-issues',
'source-maps',
'full-page-screenshot',
'bf-cache-failures',
],
},
{
Expand Down
Loading