Skip to content

Commit

Permalink
core(runner): save lhr on -A (#11509)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored Oct 2, 2020
1 parent ddd1496 commit 3e4592d
Show file tree
Hide file tree
Showing 6 changed files with 440 additions and 4 deletions.
10 changes: 10 additions & 0 deletions lighthouse-core/lib/asset-saver.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ async function saveArtifacts(artifacts, basePath) {
log.timeEnd(status);
}

/**
* Save LHR to file located at basePath/lhr.json.
* @param {LH.Result} lhr
* @param {string} basePath
*/
function saveLhr(lhr, basePath) {
fs.writeFileSync(`${basePath}/lhr.json`, JSON.stringify(lhr, null, 2));
}

/**
* Filter traces and extract screenshots to prepare for saving.
* @param {LH.Artifacts} artifacts
Expand Down Expand Up @@ -284,6 +293,7 @@ async function saveLanternNetworkData(devtoolsLog, outputPath) {

module.exports = {
saveArtifacts,
saveLhr,
loadArtifacts,
saveAssets,
prepareAssets,
Expand Down
14 changes: 10 additions & 4 deletions lighthouse-core/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ class Runner {

// User can run -G solo, -A solo, or -GA together
// -G and -A will run partial lighthouse pipelines,
// and -GA will run everything plus save artifacts to disk
// and -GA will run everything plus save artifacts and lhr to disk.

// Gather phase
// Either load saved artifacts off disk or from the browser
let artifacts;
let requestedUrl;
if (settings.auditMode && !settings.gatherMode) {
// No browser required, just load the artifacts from disk.
const path = Runner._getArtifactsPath(settings);
const path = Runner._getDataSavePath(settings);
artifacts = assetSaver.loadArtifacts(path);
requestedUrl = artifacts.URL.requestedUrl;

Expand All @@ -81,7 +81,7 @@ class Runner {
artifacts = await Runner._gatherArtifactsFromBrowser(requestedUrl, runOpts, connection);
// -G means save these to ./latest-run, etc.
if (settings.gatherMode) {
const path = Runner._getArtifactsPath(settings);
const path = Runner._getDataSavePath(settings);
await assetSaver.saveArtifacts(artifacts, path);
}
}
Expand Down Expand Up @@ -156,6 +156,12 @@ class Runner {
// LHR has now been localized.
const lhr = /** @type {LH.Result} */ (i18nLhr);

// Save lhr to ./latest-run, but only if -A is used.
if (settings.auditMode) {
const path = Runner._getDataSavePath(settings);
assetSaver.saveLhr(lhr, path);
}

// Create the HTML, JSON, and/or CSV string
const report = generateReport(lhr, settings.output);

Expand Down Expand Up @@ -448,7 +454,7 @@ class Runner {
* @param {LH.Config.Settings} settings
* @return {string}
*/
static _getArtifactsPath(settings) {
static _getDataSavePath(settings) {
const {auditMode, gatherMode} = settings;

// This enables usage like: -GA=./custom-folder
Expand Down
140 changes: 140 additions & 0 deletions lighthouse-core/test/fixtures/artifacts/alphabet-artifacts/lhr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
"environment": {
"credits": {}
},
"lighthouseVersion": "6.4.0",
"requestedUrl": "https://example.com/",
"finalUrl": "https://example.com/",
"runWarnings": [],
"audits": {
"simple": {
"id": "simple",
"title": "Requires some artifacts",
"description": "Test for always throwing",
"score": 1,
"scoreDisplayMode": "binary"
}
},
"configSettings": {
"output": "json",
"maxWaitForFcp": 30000,
"maxWaitForLoad": 45000,
"throttlingMethod": "simulate",
"throttling": {
"rttMs": 150,
"throughputKbps": 1638.4,
"requestLatencyMs": 562.5,
"downloadThroughputKbps": 1474.5600000000002,
"uploadThroughputKbps": 675,
"cpuSlowdownMultiplier": 4
},
"auditMode": "/Users/cjamcl/src/lighthouse/lighthouse-core/test/fixtures/artifacts/alphabet-artifacts/",
"gatherMode": false,
"disableStorageReset": false,
"emulatedFormFactor": "mobile",
"internalDisableDeviceScreenEmulation": false,
"channel": "node",
"budgets": null,
"locale": "en",
"blockedUrlPatterns": null,
"additionalTraceCategories": null,
"extraHeaders": null,
"precomputedLanternData": null,
"onlyAudits": null,
"onlyCategories": null,
"skipAudits": null
},
"categories": {},
"timing": {
"entries": [
{
"startTime": 368.43,
"name": "lh:init:config",
"duration": 0.66,
"entryType": "measure"
},
{
"startTime": 368.48,
"name": "lh:config:requireAudits",
"duration": 0.6,
"entryType": "measure"
},
{
"startTime": 369.09,
"name": "lh:runner:run",
"duration": 0.29,
"entryType": "measure"
},
{
"startTime": 369.25,
"name": "lh:runner:auditing",
"duration": 0.1,
"entryType": "measure"
},
{
"startTime": 369.27,
"name": "lh:audit:simple",
"duration": 0.07,
"entryType": "measure"
},
{
"startTime": 369.35,
"name": "lh:runner:generate",
"duration": 0.02,
"entryType": "measure"
}
],
"total": 0.29
},
"i18n": {
"rendererFormattedStrings": {
"auditGroupExpandTooltip": "Show audits",
"calculatorLink": "See calculator.",
"crcInitialNavigation": "Initial Navigation",
"crcLongestDurationLabel": "Maximum critical path latency:",
"dropdownCopyJSON": "Copy JSON",
"dropdownDarkTheme": "Toggle Dark Theme",
"dropdownPrintExpanded": "Print Expanded",
"dropdownPrintSummary": "Print Summary",
"dropdownSaveGist": "Save as Gist",
"dropdownSaveHTML": "Save as HTML",
"dropdownSaveJSON": "Save as JSON",
"dropdownViewer": "Open in Viewer",
"errorLabel": "Error!",
"errorMissingAuditInfo": "Report error: no audit information",
"footerIssue": "File an issue",
"labDataTitle": "Lab Data",
"lsPerformanceCategoryDescription": "[Lighthouse](https://developers.google.com/web/tools/lighthouse/) analysis of the current page on an emulated mobile network. Values are estimated and may vary.",
"manualAuditsGroupTitle": "Additional items to manually check",
"notApplicableAuditsGroupTitle": "Not applicable",
"opportunityResourceColumnLabel": "Opportunity",
"opportunitySavingsColumnLabel": "Estimated Savings",
"passedAuditsGroupTitle": "Passed audits",
"runtimeDesktopEmulation": "Emulated Desktop",
"runtimeMobileEmulation": "Emulated Moto G4",
"runtimeNoEmulation": "No emulation",
"runtimeSettingsAxeVersion": "Axe version",
"runtimeSettingsBenchmark": "CPU/Memory Power",
"runtimeSettingsChannel": "Channel",
"runtimeSettingsCPUThrottling": "CPU throttling",
"runtimeSettingsDevice": "Device",
"runtimeSettingsFetchTime": "Fetch Time",
"runtimeSettingsNetworkThrottling": "Network throttling",
"runtimeSettingsTitle": "Runtime Settings",
"runtimeSettingsUA": "User agent (host)",
"runtimeSettingsUANetwork": "User agent (network)",
"runtimeSettingsUrl": "URL",
"runtimeUnknown": "Unknown",
"snippetCollapseButtonLabel": "Collapse snippet",
"snippetExpandButtonLabel": "Expand snippet",
"thirdPartyResourcesLabel": "Show 3rd-party resources",
"throttlingProvided": "Provided by environment",
"toplevelWarningsMessage": "There were issues affecting this run of Lighthouse:",
"varianceDisclaimer": "Values are estimated and may vary. The [performance score is calculated](https://web.dev/performance-scoring/) directly from these metrics.",
"warningAuditsGroupTitle": "Passed audits but with warnings",
"warningHeader": "Warnings: "
},
"icuMessagePaths": {}
},
"stackPacks": []
}
142 changes: 142 additions & 0 deletions lighthouse-core/test/fixtures/artifacts/empty-artifacts/lhr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"environment": {
"credits": {}
},
"lighthouseVersion": "6.4.0",
"requestedUrl": "https://example.com/",
"finalUrl": "https://example.com/",
"runWarnings": [
"Really important audit warning!"
],
"audits": {
"test-audit": {
"id": "test-audit",
"title": "A test audit",
"description": "An audit for testing",
"score": 1,
"scoreDisplayMode": "binary"
}
},
"configSettings": {
"output": "json",
"maxWaitForFcp": 30000,
"maxWaitForLoad": 45000,
"throttlingMethod": "simulate",
"throttling": {
"rttMs": 150,
"throughputKbps": 1638.4,
"requestLatencyMs": 562.5,
"downloadThroughputKbps": 1474.5600000000002,
"uploadThroughputKbps": 675,
"cpuSlowdownMultiplier": 4
},
"auditMode": "/Users/cjamcl/src/lighthouse/lighthouse-core/test/fixtures/artifacts/empty-artifacts/",
"gatherMode": false,
"disableStorageReset": false,
"emulatedFormFactor": "mobile",
"internalDisableDeviceScreenEmulation": false,
"channel": "node",
"budgets": null,
"locale": "en",
"blockedUrlPatterns": null,
"additionalTraceCategories": null,
"extraHeaders": null,
"precomputedLanternData": null,
"onlyAudits": null,
"onlyCategories": null,
"skipAudits": null
},
"categories": {},
"timing": {
"entries": [
{
"startTime": 714.61,
"name": "lh:init:config",
"duration": 0.59,
"entryType": "measure"
},
{
"startTime": 714.65,
"name": "lh:config:requireAudits",
"duration": 0.54,
"entryType": "measure"
},
{
"startTime": 715.2,
"name": "lh:runner:run",
"duration": 0.29,
"entryType": "measure"
},
{
"startTime": 715.36,
"name": "lh:runner:auditing",
"duration": 0.12,
"entryType": "measure"
},
{
"startTime": 715.37,
"name": "lh:audit:test-audit",
"duration": 0.09,
"entryType": "measure"
},
{
"startTime": 715.48,
"name": "lh:runner:generate",
"duration": 0.02,
"entryType": "measure"
}
],
"total": 0.29
},
"i18n": {
"rendererFormattedStrings": {
"auditGroupExpandTooltip": "Show audits",
"calculatorLink": "See calculator.",
"crcInitialNavigation": "Initial Navigation",
"crcLongestDurationLabel": "Maximum critical path latency:",
"dropdownCopyJSON": "Copy JSON",
"dropdownDarkTheme": "Toggle Dark Theme",
"dropdownPrintExpanded": "Print Expanded",
"dropdownPrintSummary": "Print Summary",
"dropdownSaveGist": "Save as Gist",
"dropdownSaveHTML": "Save as HTML",
"dropdownSaveJSON": "Save as JSON",
"dropdownViewer": "Open in Viewer",
"errorLabel": "Error!",
"errorMissingAuditInfo": "Report error: no audit information",
"footerIssue": "File an issue",
"labDataTitle": "Lab Data",
"lsPerformanceCategoryDescription": "[Lighthouse](https://developers.google.com/web/tools/lighthouse/) analysis of the current page on an emulated mobile network. Values are estimated and may vary.",
"manualAuditsGroupTitle": "Additional items to manually check",
"notApplicableAuditsGroupTitle": "Not applicable",
"opportunityResourceColumnLabel": "Opportunity",
"opportunitySavingsColumnLabel": "Estimated Savings",
"passedAuditsGroupTitle": "Passed audits",
"runtimeDesktopEmulation": "Emulated Desktop",
"runtimeMobileEmulation": "Emulated Moto G4",
"runtimeNoEmulation": "No emulation",
"runtimeSettingsAxeVersion": "Axe version",
"runtimeSettingsBenchmark": "CPU/Memory Power",
"runtimeSettingsChannel": "Channel",
"runtimeSettingsCPUThrottling": "CPU throttling",
"runtimeSettingsDevice": "Device",
"runtimeSettingsFetchTime": "Fetch Time",
"runtimeSettingsNetworkThrottling": "Network throttling",
"runtimeSettingsTitle": "Runtime Settings",
"runtimeSettingsUA": "User agent (host)",
"runtimeSettingsUANetwork": "User agent (network)",
"runtimeSettingsUrl": "URL",
"runtimeUnknown": "Unknown",
"snippetCollapseButtonLabel": "Collapse snippet",
"snippetExpandButtonLabel": "Expand snippet",
"thirdPartyResourcesLabel": "Show 3rd-party resources",
"throttlingProvided": "Provided by environment",
"toplevelWarningsMessage": "There were issues affecting this run of Lighthouse:",
"varianceDisclaimer": "Values are estimated and may vary. The [performance score is calculated](https://web.dev/performance-scoring/) directly from these metrics.",
"warningAuditsGroupTitle": "Passed audits but with warnings",
"warningHeader": "Warnings: "
},
"icuMessagePaths": {}
},
"stackPacks": []
}
Loading

0 comments on commit 3e4592d

Please sign in to comment.