Skip to content

Commit

Permalink
core(lantern): add configuration for precomputed network analysis (#7239
Browse files Browse the repository at this point in the history
)
  • Loading branch information
patrickhulce authored Feb 26, 2019
1 parent d76b2f5 commit 41bc409
Show file tree
Hide file tree
Showing 17 changed files with 499 additions and 74 deletions.
133 changes: 72 additions & 61 deletions lighthouse-cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ const path = require('path');
const commands = require('./commands/commands.js');
const printer = require('./printer.js');
const getFlags = require('./cli-flags.js').getFlags;
const runLighthouse = require('./run').runLighthouse;
const runLighthouse = require('./run.js').runLighthouse;
const generateConfig = require('../lighthouse-core/index.js').generateConfig;

const log = require('lighthouse-logger');
const pkg = require('../package.json');
const Sentry = require('../lighthouse-core/lib/sentry');
const Sentry = require('../lighthouse-core/lib/sentry.js');

const updateNotifier = require('update-notifier');
const askPermission = require('./sentry-prompt').askPermission;
const askPermission = require('./sentry-prompt.js').askPermission;

/**
* @return {boolean}
Expand All @@ -41,74 +41,85 @@ function isDev() {
return fs.existsSync(path.join(__dirname, '../.git'));
}

// Tell user if there's a newer version of LH.
updateNotifier({pkg}).notify();

const cliFlags = getFlags();
/**
* @return {Promise<LH.RunnerResult|void>}
*/
async function begin() {
// Tell user if there's a newer version of LH.
updateNotifier({pkg}).notify();

// Process terminating command
if (cliFlags.listAllAudits) {
commands.listAudits();
}
const cliFlags = getFlags();

// Process terminating command
if (cliFlags.listTraceCategories) {
commands.listTraceCategories();
}
// Process terminating command
if (cliFlags.listAllAudits) {
commands.listAudits();
}

const url = cliFlags._[0];

/** @type {LH.Config.Json|undefined} */
let configJson;
if (cliFlags.configPath) {
// Resolve the config file path relative to where cli was called.
cliFlags.configPath = path.resolve(process.cwd(), cliFlags.configPath);
configJson = /** @type {LH.Config.Json} */ (require(cliFlags.configPath));
} else if (cliFlags.preset) {
if (cliFlags.preset === 'mixed-content') {
// The mixed-content audits require headless Chrome (https://crbug.com/764505).
cliFlags.chromeFlags = `${cliFlags.chromeFlags} --headless`;
// Process terminating command
if (cliFlags.listTraceCategories) {
commands.listTraceCategories();
}

configJson = require(`../lighthouse-core/config/${cliFlags.preset}-config.js`);
}
const url = cliFlags._[0];

/** @type {LH.Config.Json|undefined} */
let configJson;
if (cliFlags.configPath) {
// Resolve the config file path relative to where cli was called.
cliFlags.configPath = path.resolve(process.cwd(), cliFlags.configPath);
configJson = /** @type {LH.Config.Json} */ (require(cliFlags.configPath));
} else if (cliFlags.preset) {
if (cliFlags.preset === 'mixed-content') {
// The mixed-content audits require headless Chrome (https://crbug.com/764505).
cliFlags.chromeFlags = `${cliFlags.chromeFlags} --headless`;
}

configJson = require(`../lighthouse-core/config/${cliFlags.preset}-config.js`);
}

// set logging preferences
cliFlags.logLevel = 'info';
if (cliFlags.verbose) {
cliFlags.logLevel = 'verbose';
} else if (cliFlags.quiet) {
cliFlags.logLevel = 'silent';
}
log.setLevel(cliFlags.logLevel);

if (
cliFlags.output.length === 1 &&
cliFlags.output[0] === printer.OutputMode.json &&
!cliFlags.outputPath
) {
cliFlags.outputPath = 'stdout';
}
// set logging preferences
cliFlags.logLevel = 'info';
if (cliFlags.verbose) {
cliFlags.logLevel = 'verbose';
} else if (cliFlags.quiet) {
cliFlags.logLevel = 'silent';
}
log.setLevel(cliFlags.logLevel);

if (
cliFlags.output.length === 1 &&
cliFlags.output[0] === printer.OutputMode.json &&
!cliFlags.outputPath
) {
cliFlags.outputPath = 'stdout';
}

if (cliFlags.extraHeaders) {
// TODO: LH.Flags.extraHeaders is actually a string at this point, but needs to be
// copied over to LH.Settings.extraHeaders, which is LH.Crdp.Network.Headers. Force
// the conversion here, but long term either the CLI flag or the setting should have
// a different name.
// @ts-ignore
let extraHeadersStr = /** @type {string} */ (cliFlags.extraHeaders);
// If not a JSON object, assume it's a path to a JSON file.
if (extraHeadersStr.substr(0, 1) !== '{') {
extraHeadersStr = fs.readFileSync(extraHeadersStr, 'utf-8');
if (cliFlags.extraHeaders) {
// TODO: LH.Flags.extraHeaders is actually a string at this point, but needs to be
// copied over to LH.Settings.extraHeaders, which is LH.Crdp.Network.Headers. Force
// the conversion here, but long term either the CLI flag or the setting should have
// a different name.
// @ts-ignore
let extraHeadersStr = /** @type {string} */ (cliFlags.extraHeaders);
// If not a JSON object, assume it's a path to a JSON file.
if (extraHeadersStr.substr(0, 1) !== '{') {
extraHeadersStr = fs.readFileSync(extraHeadersStr, 'utf-8');
}

cliFlags.extraHeaders = JSON.parse(extraHeadersStr);
}

cliFlags.extraHeaders = JSON.parse(extraHeadersStr);
}
if (cliFlags.precomputedLanternDataPath) {
const lanternDataStr = fs.readFileSync(cliFlags.precomputedLanternDataPath, 'utf8');
/** @type {LH.PrecomputedLanternData} */
const data = JSON.parse(lanternDataStr);
if (!data.additionalRttByOrigin || !data.serverResponseTimeByOrigin) {
throw new Error('Invalid precomputed lantern data file');
}

cliFlags.precomputedLanternData = data;
}

/**
* @return {Promise<LH.RunnerResult|void>}
*/
async function begin() {
if (cliFlags.printConfig) {
const config = generateConfig(configJson, cliFlags);
process.stdout.write(config.getPrintString());
Expand Down
4 changes: 4 additions & 0 deletions lighthouse-cli/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ function getFlags(manualArgv) {
'max-wait-for-load':
'The timeout (in milliseconds) to wait before the page is considered done loading and the run should continue. WARNING: Very high values can lead to large traces and instability',
'extra-headers': 'Set extra HTTP Headers to pass with request',
'precomputed-lantern-data-path': 'Path to the file where lantern simulation data should be read from, overwriting the lantern observed estimates for RTT and server latency.',
'lantern-data-output-path': 'Path to the file where lantern simulation data should be written to, can be used in a future run with the `precomputed-lantern-data-path` flag.',
'only-audits': 'Only run the specified audits',
'only-categories': 'Only run the specified categories',
'skip-audits': 'Run everything except these audits',
Expand Down Expand Up @@ -133,6 +135,8 @@ function getFlags(manualArgv) {
.array('skipAudits')
.array('output')
.string('extraHeaders')
.string('precomputedLanternDataPath')
.string('lanternDataOutputPath')

// default values
.default('chrome-flags', '')
Expand Down
5 changes: 4 additions & 1 deletion lighthouse-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
*/
'use strict';

require('./bin.js').begin();
require('./bin.js').begin().catch(err => {
process.stderr.write(err.stack);
process.exit(1);
});
13 changes: 9 additions & 4 deletions lighthouse-cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

const path = require('path');

const Printer = require('./printer');
const Printer = require('./printer.js');
const ChromeLauncher = require('chrome-launcher');

const yargsParser = require('yargs-parser');
const lighthouse = require('../lighthouse-core');
const lighthouse = require('../lighthouse-core/index.js');
const log = require('lighthouse-logger');
const getFilenamePrefix = require('../lighthouse-core/lib/file-namer').getFilenamePrefix;
const assetSaver = require('../lighthouse-core/lib/asset-saver');
const getFilenamePrefix = require('../lighthouse-core/lib/file-namer.js').getFilenamePrefix;
const assetSaver = require('../lighthouse-core/lib/asset-saver.js');

const opn = require('opn');

Expand Down Expand Up @@ -120,6 +120,11 @@ function handleError(err) {
async function saveResults(runnerResult, flags) {
const cwd = process.cwd();

if (flags.lanternDataOutputPath) {
const devtoolsLog = runnerResult.artifacts.devtoolsLogs.defaultPass;
await assetSaver.saveLanternNetworkData(devtoolsLog, flags.lanternDataOutputPath);
}

const shouldSaveResults = flags.auditMode || (flags.gatherMode === flags.auditMode);
if (!shouldSaveResults) return;
const {lhr, artifacts, report} = runnerResult;
Expand Down
2 changes: 2 additions & 0 deletions lighthouse-cli/test/cli/__snapshots__/index-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ Object {
"output": Array [
"html",
],
"precomputedLanternData": null,
"skipAudits": null,
"throttling": Object {
"cpuSlowdownMultiplier": 4,
Expand Down Expand Up @@ -1343,6 +1344,7 @@ Object {
"output": Array [
"json",
],
"precomputedLanternData": null,
"skipAudits": null,
"throttling": Object {
"cpuSlowdownMultiplier": 4,
Expand Down
Loading

0 comments on commit 41bc409

Please sign in to comment.