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(fr): convert base artifacts to gatherer artifacts #12129

Merged
merged 5 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 lighthouse-core/fraggle-rock/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ function initializeConfig(configJSON, context) {
// TODO(FR-COMPAT): validate audits
// TODO(FR-COMPAT): validate categories
// TODO(FR-COMPAT): filter config using onlyAudits/onlyCategories
// TODO(FR-COMPAT): always keep base/shared artifacts/audits (Stacks, FullPageScreenshot, etc)

config = filterConfigByGatherMode(config, context.gatherMode);

Expand Down
121 changes: 81 additions & 40 deletions lighthouse-core/fraggle-rock/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,105 @@

const legacyDefaultConfig = require('../../config/default-config.js');

// Ensure all artifact IDs match the typedefs.
/** @type {Record<keyof LH.FRArtifacts, string>} */
const artifacts = {
DevtoolsLog: '',
Trace: '',
Accessibility: '',
AppCacheManifest: '',
CacheContents: '',
ConsoleMessages: '',
Doctype: '',
DOMStats: '',
EmbeddedContent: '',
FontSize: '',
FormElements: '',
GlobalListeners: '',
IFrameElements: '',
InstallabilityErrors: '',
MetaElements: '',
NetworkUserAgent: '',
PasswordInputsWithPreventedPaste: '',
RobotsTxt: '',
Stacks: '',
TapTargets: '',
ViewportDimensions: '',
WebAppManifest: '',
devtoolsLogs: '',
traces: '',
};

for (const key of Object.keys(artifacts)) {
artifacts[/** @type {keyof typeof artifacts} */ (key)] = key;
}

/** @type {LH.Config.Json} */
const defaultConfig = {
artifacts: [
// Artifacts which can be depended on come first.
{id: 'DevtoolsLog', gatherer: 'devtools-log'},
{id: 'Trace', gatherer: 'trace'},
{id: artifacts.DevtoolsLog, gatherer: 'devtools-log'},
{id: artifacts.Trace, gatherer: 'trace'},

/* eslint-disable max-len */
{id: 'Accessibility', gatherer: 'accessibility'},
{id: 'Appcache', gatherer: 'dobetterweb/appcache'},
{id: 'CacheContents', gatherer: 'cache-contents'},
{id: 'ConsoleMessages', gatherer: 'console-messages'},
{id: 'Doctype', gatherer: 'dobetterweb/doctype'},
{id: 'Domstats', gatherer: 'dobetterweb/domstats'},
{id: 'EmbeddedContent', gatherer: 'seo/embedded-content'},
{id: 'FontSize', gatherer: 'seo/font-size'},
{id: 'FormElements', gatherer: 'form-elements'},
{id: 'GlobalListeners', gatherer: 'global-listeners'},
{id: 'IframeElements', gatherer: 'iframe-elements'},
{id: 'MetaElements', gatherer: 'meta-elements'},
{id: 'PasswordInputsWithPreventedPaste', gatherer: 'dobetterweb/password-inputs-with-prevented-paste'},
{id: 'RobotsTxt', gatherer: 'seo/robots-txt'},
{id: 'TapTargets', gatherer: 'seo/tap-targets'},
{id: 'ViewportDimensions', gatherer: 'viewport-dimensions'},
{id: artifacts.Accessibility, gatherer: 'accessibility'},
{id: artifacts.AppCacheManifest, gatherer: 'dobetterweb/appcache'},
{id: artifacts.CacheContents, gatherer: 'cache-contents'},
{id: artifacts.ConsoleMessages, gatherer: 'console-messages'},
{id: artifacts.Doctype, gatherer: 'dobetterweb/doctype'},
{id: artifacts.DOMStats, gatherer: 'dobetterweb/domstats'},
{id: artifacts.EmbeddedContent, gatherer: 'seo/embedded-content'},
{id: artifacts.FontSize, gatherer: 'seo/font-size'},
{id: artifacts.FormElements, gatherer: 'form-elements'},
{id: artifacts.GlobalListeners, gatherer: 'global-listeners'},
{id: artifacts.IFrameElements, gatherer: 'iframe-elements'},
{id: artifacts.InstallabilityErrors, gatherer: 'installability-errors'},
{id: artifacts.MetaElements, gatherer: 'meta-elements'},
{id: artifacts.NetworkUserAgent, gatherer: 'network-user-agent'},
{id: artifacts.PasswordInputsWithPreventedPaste, gatherer: 'dobetterweb/password-inputs-with-prevented-paste'},
{id: artifacts.RobotsTxt, gatherer: 'seo/robots-txt'},
{id: artifacts.Stacks, gatherer: 'stacks'},
{id: artifacts.TapTargets, gatherer: 'seo/tap-targets'},
{id: artifacts.ViewportDimensions, gatherer: 'viewport-dimensions'},
{id: artifacts.WebAppManifest, gatherer: 'web-app-manifest'},
/* eslint-enable max-len */

// Artifact copies are renamed for compatibility with legacy artifacts.
{id: 'devtoolsLogs', gatherer: 'devtools-log-compat'},
{id: 'traces', gatherer: 'trace-compat'},
{id: artifacts.devtoolsLogs, gatherer: 'devtools-log-compat'},
{id: artifacts.traces, gatherer: 'trace-compat'},
],
navigations: [
{
id: 'default',
artifacts: [
// Artifacts which can be depended on come first.
'DevtoolsLog',
'Trace',
artifacts.DevtoolsLog,
artifacts.Trace,

'Accessibility',
'Appcache',
'CacheContents',
'ConsoleMessages',
'Doctype',
'Domstats',
'EmbeddedContent',
'FontSize',
'FormElements',
'GlobalListeners',
'IframeElements',
'MetaElements',
'PasswordInputsWithPreventedPaste',
'RobotsTxt',
'TapTargets',
'ViewportDimensions',
artifacts.Accessibility,
artifacts.AppCacheManifest,
artifacts.CacheContents,
artifacts.ConsoleMessages,
artifacts.Doctype,
artifacts.DOMStats,
artifacts.EmbeddedContent,
artifacts.FontSize,
artifacts.FormElements,
artifacts.GlobalListeners,
artifacts.IFrameElements,
artifacts.InstallabilityErrors,
artifacts.MetaElements,
artifacts.NetworkUserAgent,
artifacts.PasswordInputsWithPreventedPaste,
artifacts.RobotsTxt,
artifacts.Stacks,
artifacts.TapTargets,
artifacts.ViewportDimensions,
artifacts.WebAppManifest,

// Compat artifacts come last.
'devtoolsLogs',
'traces',
artifacts.devtoolsLogs,
artifacts.traces,
],
},
],
Expand Down
49 changes: 35 additions & 14 deletions lighthouse-core/fraggle-rock/gather/base-artifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,51 @@
*/
'use strict';

const {getBrowserVersion, getBenchmarkIndex} = require('../../gather/driver/environment.js');

/**
* @param {LH.Config.FRConfig} config
* @return {LH.BaseArtifacts}
* @param {LH.Gatherer.FRTransitionalDriver} driver
* @return {Promise<LH.BaseArtifacts>}
*/
function getBaseArtifacts(config) {
// TODO(FR-COMPAT): convert these to regular artifacts
async function getBaseArtifacts(config, driver) {
const HostUserAgent = (await getBrowserVersion(driver.defaultSession)).userAgent;

// Whether Lighthouse was run on a mobile device (i.e. not on a desktop machine).
const HostFormFactor =
HostUserAgent.includes('Android') || HostUserAgent.includes('Mobile') ? 'mobile' : 'desktop';

const BenchmarkIndex = await getBenchmarkIndex(driver.executionContext);

/** @type {Array<string | LH.IcuMessage>} */
const LighthouseRunWarnings = [];

// TODO(FR-COMPAT): support slow host CPU warning
// TODO(FR-COMPAT): support redirected URL warning

return {
// Meta artifacts.
fetchTime: new Date().toJSON(),
LighthouseRunWarnings: [],
URL: {requestedUrl: '', finalUrl: ''},
Timing: [],
Stacks: [],
LighthouseRunWarnings,
settings: config.settings,
HostFormFactor: 'mobile',
HostUserAgent: 'unknown',
NetworkUserAgent: 'unknown',
BenchmarkIndex: 0,
InstallabilityErrors: {errors: []},
// Environment artifacts that can always be computed.
HostFormFactor,
HostUserAgent,
BenchmarkIndex,
// Contextual artifacts whose collection changes based on gather mode.
URL: {requestedUrl: '', finalUrl: ''},
PageLoadError: null, // TODO(FR-COMPAT): support PageLoadError
// Artifacts that have been replaced by regular gatherers in Fraggle Rock.
Stacks: [],
NetworkUserAgent: '',
WebAppManifest: null, // replaced by standard gatherer
InstallabilityErrors: {errors: []}, // replaced by standard gatherer
traces: {},
devtoolsLogs: {},
WebAppManifest: null,
PageLoadError: null,
};
}

module.exports = {getBaseArtifacts};
module.exports = {
getBaseArtifacts,
};
5 changes: 5 additions & 0 deletions lighthouse-core/fraggle-rock/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class Driver {
return this._executionContext;
}

/** @return {Promise<string>} */
async url() {
return this._page.url();
}

/** @return {Promise<void>} */
async connect() {
if (this._session) return;
Expand Down
3 changes: 2 additions & 1 deletion lighthouse-core/fraggle-rock/gather/navigation-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function _setup({driver, config, requestedUrl}) {

// TODO(FR-COMPAT): setupDriver

const baseArtifacts = getBaseArtifacts(config);
const baseArtifacts = await getBaseArtifacts(config, driver);
baseArtifacts.URL.requestedUrl = requestedUrl;

return {baseArtifacts};
Expand Down Expand Up @@ -99,6 +99,7 @@ async function _collectPhaseArtifacts({navigationContext, artifacts, phase}) {
: {};

return gatherer[phase]({
url: await navigationContext.driver.url(),
driver: navigationContext.driver,
gatherMode: 'navigation',
dependencies,
Expand Down
11 changes: 9 additions & 2 deletions lighthouse-core/fraggle-rock/gather/snapshot-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function snapshot(options) {

return Runner.run(
async () => {
const baseArtifacts = getBaseArtifacts(config);
const baseArtifacts = await getBaseArtifacts(config, driver);
baseArtifacts.URL.requestedUrl = url;
baseArtifacts.URL.finalUrl = url;

Expand All @@ -32,8 +32,15 @@ async function snapshot(options) {
const {id, gatherer} = artifactDefn;
const artifactName = /** @type {keyof LH.GathererArtifacts} */ (id);
const dependencies = await collectArtifactDependencies(artifactDefn, artifacts);
/** @type {LH.Gatherer.FRTransitionalContext} */
const context = {
gatherMode: 'snapshot',
url,
driver,
dependencies,
};
const artifact = await Promise.resolve()
.then(() => gatherer.instance.snapshot({gatherMode: 'snapshot', driver, dependencies}))
.then(() => gatherer.instance.snapshot(context))
.catch(err => err);

artifacts[artifactName] = artifact;
Expand Down
18 changes: 15 additions & 3 deletions lighthouse-core/fraggle-rock/gather/timespan-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ async function startTimespan(options) {

for (const {id, gatherer} of config.artifacts || []) {
artifactErrors[id] = Promise.resolve().then(() =>
gatherer.instance.beforeTimespan({gatherMode: 'timespan', driver, dependencies: {}})
gatherer.instance.beforeTimespan({
gatherMode: 'timespan',
url: requestedUrl,
driver,
dependencies: {},
})
);

// Run each beforeTimespan serially, but handle errors in the next pass.
Expand All @@ -39,7 +44,7 @@ async function startTimespan(options) {
const finalUrl = await options.page.url();
return Runner.run(
async () => {
const baseArtifacts = getBaseArtifacts(config);
const baseArtifacts = await getBaseArtifacts(config, driver);
baseArtifacts.URL.requestedUrl = requestedUrl;
baseArtifacts.URL.finalUrl = finalUrl;

Expand All @@ -50,9 +55,16 @@ async function startTimespan(options) {
const {id, gatherer} = artifactDefn;
const artifactName = /** @type {keyof LH.GathererArtifacts} */ (id);
const dependencies = await collectArtifactDependencies(artifactDefn, artifacts);
/** @type {LH.Gatherer.FRTransitionalContext} */
const context = {
gatherMode: 'timespan',
url: finalUrl,
driver,
dependencies,
};
const artifact = await artifactErrors[id]
.then(() =>
gatherer.instance.afterTimespan({gatherMode: 'timespan', driver, dependencies})
gatherer.instance.afterTimespan(context)
)
.catch(err => err);

Expand Down
Loading