-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core(fr): add timespan runner (#11944)
- Loading branch information
1 parent
c04126b
commit fb41e60
Showing
12 changed files
with
331 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @license Copyright 2021 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. | ||
*/ | ||
'use strict'; | ||
|
||
/** | ||
* @param {LH.Config.FRConfig} config | ||
* @return {LH.BaseArtifacts} | ||
*/ | ||
function getBaseArtifacts(config) { | ||
// TODO(FR-COMPAT): convert these to regular artifacts | ||
|
||
return { | ||
fetchTime: new Date().toJSON(), | ||
LighthouseRunWarnings: [], | ||
URL: {requestedUrl: '', finalUrl: ''}, | ||
Timing: [], | ||
Stacks: [], | ||
settings: config.settings, | ||
HostFormFactor: 'mobile', | ||
HostUserAgent: 'unknown', | ||
NetworkUserAgent: 'unknown', | ||
BenchmarkIndex: 0, | ||
InstallabilityErrors: {errors: []}, | ||
traces: {}, | ||
devtoolsLogs: {}, | ||
WebAppManifest: null, | ||
PageLoadError: null, | ||
}; | ||
} | ||
|
||
module.exports = {getBaseArtifacts}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @license Copyright 2020 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. | ||
*/ | ||
'use strict'; | ||
|
||
const Driver = require('./driver.js'); | ||
const Runner = require('../../runner.js'); | ||
const {initializeConfig} = require('../config/config.js'); | ||
const {getBaseArtifacts} = require('./base-artifacts.js'); | ||
|
||
/** @param {{page: import('puppeteer').Page, config?: LH.Config.Json}} options */ | ||
async function snapshot(options) { | ||
const {config} = initializeConfig(options.config, {gatherMode: 'snapshot'}); | ||
const driver = new Driver(options.page); | ||
await driver.connect(); | ||
|
||
const url = await options.page.url(); | ||
|
||
return Runner.run( | ||
async () => { | ||
const baseArtifacts = getBaseArtifacts(config); | ||
baseArtifacts.URL.requestedUrl = url; | ||
baseArtifacts.URL.finalUrl = url; | ||
|
||
/** @type {Partial<LH.GathererArtifacts>} */ | ||
const artifacts = {}; | ||
|
||
for (const {id, gatherer} of config.artifacts || []) { | ||
const artifactName = /** @type {keyof LH.GathererArtifacts} */ (id); | ||
const artifact = await Promise.resolve() | ||
.then(() => gatherer.instance.snapshot({gatherMode: 'snapshot', driver})) | ||
.catch(err => err); | ||
|
||
artifacts[artifactName] = artifact; | ||
} | ||
|
||
return /** @type {LH.Artifacts} */ ({...baseArtifacts, ...artifacts}); // Cast to drop Partial<> | ||
}, | ||
{ | ||
url, | ||
config, | ||
} | ||
); | ||
} | ||
|
||
module.exports = { | ||
snapshot, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* @license Copyright 2021 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. | ||
*/ | ||
'use strict'; | ||
|
||
const Driver = require('./driver.js'); | ||
const Runner = require('../../runner.js'); | ||
const {initializeConfig} = require('../config/config.js'); | ||
const {getBaseArtifacts} = require('./base-artifacts.js'); | ||
|
||
/** | ||
* @param {{page: import('puppeteer').Page, config?: LH.Config.Json}} options | ||
* @return {Promise<{endTimespan(): Promise<LH.RunnerResult|undefined>}>} | ||
*/ | ||
async function startTimespan(options) { | ||
const {config} = initializeConfig(options.config, {gatherMode: 'timespan'}); | ||
const driver = new Driver(options.page); | ||
await driver.connect(); | ||
|
||
const requestedUrl = await options.page.url(); | ||
|
||
/** @type {Record<string, Promise<Error|undefined>>} */ | ||
const artifactErrors = {}; | ||
|
||
for (const {id, gatherer} of config.artifacts || []) { | ||
artifactErrors[id] = await Promise.resolve() | ||
.then(() => gatherer.instance.beforeTimespan({gatherMode: 'timespan', driver})) | ||
.catch(err => err); | ||
} | ||
|
||
return { | ||
async endTimespan() { | ||
const finalUrl = await options.page.url(); | ||
return Runner.run( | ||
async () => { | ||
const baseArtifacts = getBaseArtifacts(config); | ||
baseArtifacts.URL.requestedUrl = requestedUrl; | ||
baseArtifacts.URL.finalUrl = finalUrl; | ||
|
||
/** @type {Partial<LH.GathererArtifacts>} */ | ||
const artifacts = {}; | ||
|
||
for (const {id, gatherer} of config.artifacts || []) { | ||
const artifactName = /** @type {keyof LH.GathererArtifacts} */ (id); | ||
const artifact = artifactErrors[id] | ||
? Promise.reject(artifactErrors[id]) | ||
: await Promise.resolve() | ||
.then(() => gatherer.instance.afterTimespan({gatherMode: 'timespan', driver})) | ||
.catch(err => err); | ||
|
||
artifacts[artifactName] = artifact; | ||
} | ||
|
||
return /** @type {LH.Artifacts} */ ({...baseArtifacts, ...artifacts}); // Cast to drop Partial<> | ||
}, | ||
{ | ||
url: finalUrl, | ||
config, | ||
} | ||
); | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = { | ||
startTimespan, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.