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(runner): independent gather and audit functions #13569

Merged
merged 22 commits into from
Feb 3, 2022
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
Prev Previous commit
Next Next commit
Revert "+ runner state"
This reverts commit cd390a4.
  • Loading branch information
adamraine committed Jan 18, 2022
commit 90e31dcb7fe4cef0efddeb55c1971ec17decc4eb
9 changes: 6 additions & 3 deletions lighthouse-core/fraggle-rock/gather/navigation-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ async function navigation(options) {
skipAboutBlank: configContext.skipAboutBlank,
};

const runner = new Runner(config, computedCache);
await runner.gatherPhase(
const gatherResult = await Runner.gatherPhase(
async () => {
const driver = new Driver(page);
const requestedUrl = URL.normalizeUrl(url);
Expand All @@ -298,9 +297,13 @@ async function navigation(options) {
await _cleanup(context);

return finalizeArtifacts(baseArtifacts, artifacts);
},
{
config,
computedCache,
}
);
return runner.auditPhase();
return Runner.auditPhase(gatherResult);
}

module.exports = {
Expand Down
9 changes: 6 additions & 3 deletions lighthouse-core/fraggle-rock/gather/snapshot-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ async function snapshot(options) {
const computedCache = new Map();
const url = await options.page.url();

const runner = new Runner(config, computedCache);
await runner.gatherPhase(
const gatherResult = await Runner.gatherPhase(
async () => {
const baseArtifacts = await getBaseArtifacts(config, driver, {gatherMode: 'snapshot'});
baseArtifacts.URL.requestedUrl = url;
Expand All @@ -50,9 +49,13 @@ async function snapshot(options) {

const artifacts = await awaitArtifacts(artifactState);
return finalizeArtifacts(baseArtifacts, artifacts);
},
{
config,
computedCache,
}
);
return runner.auditPhase();
return Runner.auditPhase(gatherResult);
}

module.exports = {
Expand Down
9 changes: 6 additions & 3 deletions lighthouse-core/fraggle-rock/gather/timespan-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ async function startTimespan(options) {
return {
async endTimespan() {
const finalUrl = await options.page.url();
const runner = new Runner(config, computedCache);
await runner.gatherPhase(
const gatherResult = await Runner.gatherPhase(
async () => {
baseArtifacts.URL.requestedUrl = requestedUrl;
baseArtifacts.URL.finalUrl = finalUrl;
Expand All @@ -63,9 +62,13 @@ async function startTimespan(options) {

const artifacts = await awaitArtifacts(artifactState);
return finalizeArtifacts(baseArtifacts, artifacts);
},
{
config,
computedCache,
}
);
return runner.auditPhase();
return Runner.auditPhase(gatherResult);
},
};
}
Expand Down
7 changes: 3 additions & 4 deletions lighthouse-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ async function lighthouse(url, flags = {}, configJSON, userConnection) {
const connection = userConnection || new ChromeProtocol(flags.port, flags.hostname);

// kick off a lighthouse run
const runner = new Runner(config, computedCache);
await runner.gatherPhase(() => {
const gatherResult = await Runner.gatherPhase(() => {
const requestedUrl = URL.normalizeUrl(url);
return Runner._gatherArtifactsFromBrowser(requestedUrl, options, connection);
});
return runner.auditPhase();
}, options);
return Runner.auditPhase(gatherResult);
}

/**
Expand Down
46 changes: 15 additions & 31 deletions lighthouse-core/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,14 @@ const {version: lighthouseVersion} = require('../package.json');
/** @typedef {import('./lib/arbitrary-equality-map.js')} ArbitraryEqualityMap */
/** @typedef {LH.Config.Config} Config */

/**
* @template {LH.Config.Config | LH.Config.FRConfig} TConfig
*/
class Runner {
/**
* @param {TConfig} config
* @param {Map<string, ArbitraryEqualityMap>=} computedCache
*/
constructor(config, computedCache) {
/** @type {LH.Artifacts|undefined} */
this.artifacts = undefined;
/** @type {TConfig} */
this.config = config;
/** @type {Map<string, ArbitraryEqualityMap>} */
this.computedCache = computedCache || new Map();
}

/**
* @template {LH.Config.Config | LH.Config.FRConfig} TConfig
* @param {LH.Gatherer.GatherResult<TConfig>} gatherResult
* @return {Promise<LH.RunnerResult|undefined>}
*/
async auditPhase() {
const {artifacts, config, computedCache} = this;
if (!artifacts) throw new Error('Must run gather phase before audit phase');

static async auditPhase(gatherResult) {
const {artifacts, config, computedCache} = gatherResult;
const settings = config.settings;
try {
const runnerStatus = {msg: 'Audit phase', id: 'lh:runner:auditPhase'};
Expand Down Expand Up @@ -117,7 +101,7 @@ class Runner {
categories,
categoryGroups: config.groups || undefined,
stackPacks: stackPacks.getStackPacks(artifacts.Stacks),
timing: Runner._getTiming(artifacts),
timing: this._getTiming(artifacts),
i18n: {
rendererFormattedStrings: format.getRendererFormattedStrings(settings.locale),
icuMessagePaths: {},
Expand Down Expand Up @@ -150,12 +134,13 @@ class Runner {
* -G and -A will run partial lighthouse pipelines,
* and -GA will run everything plus save artifacts and lhr to disk.
*
* @template {LH.Config.Config | LH.Config.FRConfig} TConfig
* @param {(runnerData: {config: TConfig, driverMock?: Driver}) => Promise<LH.Artifacts>} gatherFn
* @param {{driverMock?: Driver}=} options
* @return {Promise<LH.Artifacts>}
* @param {{config: TConfig, driverMock?: Driver, computedCache: Map<string, ArbitraryEqualityMap>}} options
* @return {Promise<LH.Gatherer.GatherResult<TConfig>>}
*/
async gatherPhase(gatherFn, options) {
const settings = this.config.settings;
static async gatherPhase(gatherFn, options) {
const settings = options.config.settings;

// Gather phase
// Either load saved artifacts from disk or from the browser.
Expand All @@ -174,7 +159,7 @@ class Runner {
let artifacts;
if (settings.auditMode && !settings.gatherMode) {
// No browser required, just load the artifacts from disk.
const path = Runner._getDataSavePath(settings);
const path = this._getDataSavePath(settings);
artifacts = assetSaver.loadArtifacts(path);
const requestedUrl = artifacts.URL.requestedUrl;

Expand All @@ -183,21 +168,20 @@ class Runner {
}
} else {
artifacts = await gatherFn({
config: this.config,
driverMock: options?.driverMock,
config: options.config,
driverMock: options.driverMock,
});

// -G means save these to disk (e.g. ./latest-run).
if (settings.gatherMode) {
const path = Runner._getDataSavePath(settings);
const path = this._getDataSavePath(settings);
await assetSaver.saveArtifacts(artifacts, path);
}
}

log.timeEnd(runnerStatus);

this.artifacts = artifacts;
return artifacts;
return {artifacts, config: options.config, computedCache: options.computedCache};
} catch (err) {
throw Runner.createRunnerError(err, settings);
}
Expand Down
6 changes: 6 additions & 0 deletions types/gatherer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ declare module Gatherer {

type FRGatherPhase = keyof Omit<Gatherer.FRGathererInstance, 'name'|'meta'>

interface GatherResult<TConfig extends LH.Config.Config | LH.Config.FRConfig> {
artifacts: Artifacts;
config: TConfig;
computedCache: Map<string, ArbitraryEqualityMap>;
}

interface FRGathererInstance<TDependencies extends DependencyKey = DefaultDependenciesKey> {
name: keyof GathererArtifacts; // temporary COMPAT measure until artifact config support is available
meta: GathererMeta<TDependencies>;
Expand Down