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

clients(lr): insert assets in lhr for logging purposes #9002

Merged
merged 6 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 16 additions & 6 deletions clients/lightrider/lightrider-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,29 @@ async function runLighthouseInLR(connection, url, flags, lrOpts) {
}

try {
const results = await lighthouse(url, flags, config, connection);
if (!results) return;
const runnerResult = await lighthouse(url, flags, config, connection);
if (!runnerResult) return;

if (logAssets) {
await assetSaver.logAssets(results.artifacts, results.lhr.audits);
const assetsToLog =
Copy link
Member

Choose a reason for hiding this comment

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

this refactor doesn't seem to be a win. if anything, logAssets() should call its own private prepare method, but due to other comments, i dont think we'll end up needing this code anyhow.

await assetSaver.prepareAssetsForLogging(runnerResult.artifacts, runnerResult.lhr.audits);
assetSaver.logAssets(assetsToLog);
}

// pre process the LHR for proto
if (flags.output === 'json' && typeof results.report === 'string') {
return preprocessor.processForProto(results.report);
if (flags.output === 'json' && typeof runnerResult.report === 'string') {
// When LR is called with |internal: {keep_raw_response: true, save_lighthouse_assets: true}|,
// this code will log artifacts to raw_response.artifacts.
if (logAssets) {
const reportObj = JSON.parse(runnerResult.report);
Copy link
Member

Choose a reason for hiding this comment

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

let's use runnerResult.lhr instead of parsing the string back

reportObj.artifacts = runnerResult.artifacts;
runnerResult.report = JSON.stringify(reportObj);
}

return preprocessor.processForProto(runnerResult.report);
}

return results.report;
return runnerResult.report;
} catch (err) {
// If an error ruined the entire lighthouse run, attempt to return a meaningful error.
let runtimeError;
Expand Down
40 changes: 29 additions & 11 deletions lighthouse-core/lib/asset-saver.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,22 +255,39 @@ async function saveAssets(artifacts, audits, pathWithBasename) {
* Log trace(s) and associated devtoolsLog(s) to console.
* @param {LH.Artifacts} artifacts
* @param {LH.Audit.Results} audits
* @return {Promise<void>}
* @return {Promise<Array<{name: string, contents: string}>>}
*/
async function logAssets(artifacts, audits) {
Copy link
Member

Choose a reason for hiding this comment

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

once we have the LR-side to process your nested artifacts, I think we can delete this logAssets function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

async function prepareAssetsForLogging(artifacts, audits) {
/** @type {Array<{name: string, contents: string}>} */
const assetsToLog = [];

const allAssets = await prepareAssets(artifacts, audits);
allAssets.map(passAssets => {
const dtlogdata = JSON.stringify(passAssets.devtoolsLog);
// eslint-disable-next-line no-console
console.log(`loggedAsset %%% devtoolslog-${passAssets.passName}.json %%% ${dtlogdata}`);
assetsToLog.push({
name: `devtoolslog-${passAssets.passName}.json`,
contents: JSON.stringify(passAssets.devtoolsLog),
});
const traceIter = traceJsonGenerator(passAssets.traceData);
let traceJson = '';
for (const trace of traceIter) {
traceJson += trace;
}
// eslint-disable-next-line no-console
console.log(`loggedAsset %%% trace-${passAssets.passName}.json %%% ${traceJson}`);
const traceJson = [...traceIter].join();
assetsToLog.push({
name: `trace-${passAssets.passName}.json`,
contents: traceJson,
});
});

return assetsToLog;
}


/**
* Log trace(s) and associated devtoolsLog(s) to console.
* @param {Array<{name: string, contents: string}>} assetsToLog
*/
function logAssets(assetsToLog) {
for (const asset of assetsToLog) {
// eslint-disable-next-line no-console
console.log(`loggedAsset %%% ${asset.name} %%% ${asset.contents}`);
}
}

/**
Expand All @@ -291,6 +308,7 @@ module.exports = {
loadArtifacts,
saveAssets,
prepareAssets,
prepareAssetsForLogging,
saveTrace,
logAssets,
saveLanternNetworkData,
Expand Down