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(lantern): add configuration for precomputed network analysis #7239

Merged
merged 6 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@ -1213,6 +1213,7 @@ Object {
"output": Array [
"html",
],
"precomputedLanternData": null,
"skipAudits": null,
"throttling": Object {
"cpuSlowdownMultiplier": 4,
Expand Down Expand Up @@ -1342,6 +1343,7 @@ Object {
"output": Array [
"json",
],
"precomputedLanternData": null,
"skipAudits": null,
"throttling": Object {
"cpuSlowdownMultiplier": 4,
Expand Down
Loading