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 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
11 changes: 11 additions & 0 deletions lighthouse-cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ if (cliFlags.extraHeaders) {
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;
}
patrickhulce marked this conversation as resolved.
Show resolved Hide resolved

/**
* @return {Promise<LH.RunnerResult|void>}
*/
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.',
'lantern-data-output-path': 'Path to the file where lantern simulation data should be written to.',
patrickhulce marked this conversation as resolved.
Show resolved Hide resolved
'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
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 @@ -1223,6 +1223,7 @@ Object {
"output": Array [
"html",
],
"precomputedLanternData": null,
"skipAudits": null,
"throttling": Object {
"cpuSlowdownMultiplier": 4,
Expand Down Expand Up @@ -1352,6 +1353,7 @@ Object {
"output": Array [
"json",
],
"precomputedLanternData": null,
"skipAudits": null,
"throttling": Object {
"cpuSlowdownMultiplier": 4,
Expand Down
24 changes: 24 additions & 0 deletions lighthouse-cli/test/smokehouse/lantern-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @license Copyright 2017 Google Inc. 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';

/**
* Config file for running byte efficiency smokehouse audits.
*/
module.exports = {
extends: 'lighthouse:full',
settings: {
onlyCategories: ['performance'],
precomputedLanternData: {
additionalRttByOrigin: {
'http://localhost:10200': 500,
},
serverResponseTimeByOrigin: {
'http://localhost:10200': 1000,
},
},
},
};
27 changes: 27 additions & 0 deletions lighthouse-cli/test/smokehouse/perf/lantern-expectations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @license Copyright 2017 Google Inc. 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';

/**
* Expected Lighthouse audit values for lantern smoketests
*/
module.exports = [
{
requestedUrl: 'http://localhost:10200/online-only.html',
finalUrl: 'http://localhost:10200/online-only.html',
audits: {
'first-contentful-paint': {
rawValue: '>2000',
},
'first-cpu-idle': {
rawValue: '>2000',
},
'interactive': {
rawValue: '>2000',
},
},
},
];
5 changes: 5 additions & 0 deletions lighthouse-cli/test/smokehouse/run-smoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ const SMOKETESTS = [{
expectations: 'perf/expectations.js',
config: 'lighthouse-core/config/perf-config.js',
batch: 'perf-metric',
}, {
id: 'lantern',
expectations: 'perf/lantern-expectations.js',
config: smokehouseDir + 'lantern-config.js',
batch: 'parallel-first',
}, {
id: 'metrics',
expectations: 'tricky-metrics/expectations.js',
Expand Down
9 changes: 8 additions & 1 deletion lighthouse-core/computed/load-simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class LoadSimulator {
* @return {Promise<Simulator>}
*/
static async compute_(data, context) {
const {throttlingMethod, throttling} = data.settings;
const {throttlingMethod, throttling, precomputedLanternData} = data.settings;
const networkAnalysis = await NetworkAnalysis.request(data.devtoolsLog, context);

/** @type {LH.Gatherer.Simulation.Options} */
Expand All @@ -26,6 +26,13 @@ class LoadSimulator {
serverResponseTimeByOrigin: networkAnalysis.serverResponseTimeByOrigin,
};

if (precomputedLanternData) {
patrickhulce marked this conversation as resolved.
Show resolved Hide resolved
options.additionalRttByOrigin = new Map(Object.entries(
precomputedLanternData.additionalRttByOrigin));
options.serverResponseTimeByOrigin = new Map(Object.entries(
precomputedLanternData.serverResponseTimeByOrigin));
}

switch (throttlingMethod) {
case 'provided':
options.rtt = networkAnalysis.rtt;
Expand Down
1 change: 1 addition & 0 deletions lighthouse-core/config/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const defaultSettings = {
blockedUrlPatterns: null,
additionalTraceCategories: null,
extraHeaders: null,
precomputedLanternData: null,
onlyAudits: null,
onlyCategories: null,
skipAudits: null,
Expand Down
30 changes: 27 additions & 3 deletions lighthouse-core/lib/asset-saver.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ const fs = require('fs');
const path = require('path');
const log = require('lighthouse-logger');
const stream = require('stream');
const Simulator = require('./dependency-graph/simulator/simulator');
const lanternTraceSaver = require('./lantern-trace-saver');
const Metrics = require('./traces/pwmetrics-events');
const Simulator = require('./dependency-graph/simulator/simulator.js');
const lanternTraceSaver = require('./lantern-trace-saver.js');
const Metrics = require('./traces/pwmetrics-events.js');
const rimraf = require('rimraf');
const mkdirp = require('mkdirp');
const NetworkAnalysisComputed = require('../computed/network-analysis.js');

const artifactsFilename = 'artifacts.json';
const traceSuffix = '.trace.json';
Expand Down Expand Up @@ -272,11 +273,34 @@ async function logAssets(artifacts, audits) {
});
}

/**
* @param {LH.DevtoolsLog} devtoolsLog
* @param {string} outputPath
* @return {Promise<void>}
*/
async function saveLanternNetworkData(devtoolsLog, outputPath) {
const context = /** @type {LH.Audit.Context} */ ({computedCache: new Map()});
const networkAnalysis = await NetworkAnalysisComputed.request(devtoolsLog, context);

/** @type {LH.PrecomputedLanternData} */
const lanternData = {additionalRttByOrigin: {}, serverResponseTimeByOrigin: {}};
for (const [origin, value] of networkAnalysis.additionalRttByOrigin.entries()) {
if (origin.startsWith('http')) lanternData.additionalRttByOrigin[origin] = value;
}

for (const [origin, value] of networkAnalysis.serverResponseTimeByOrigin.entries()) {
if (origin.startsWith('http')) lanternData.serverResponseTimeByOrigin[origin] = value;
}
patrickhulce marked this conversation as resolved.
Show resolved Hide resolved

fs.writeFileSync(outputPath, JSON.stringify(lanternData));
}

module.exports = {
saveArtifacts,
loadArtifacts,
saveAssets,
prepareAssets,
saveTrace,
logAssets,
saveLanternNetworkData,
};
64 changes: 52 additions & 12 deletions lighthouse-core/test/computed/load-simulator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@
const assert = require('assert');
const devtoolsLog = require('../fixtures/traces/progressive-app-m60.devtools.log.json');
const LoadSimulator = require('../../computed/load-simulator.js');
const NetworkNode = require('../../lib/dependency-graph/network-node.js');

function createNetworkNode() {
return new NetworkNode({
requestId: '1',
parsedURL: {securityOrigin: 'https://pwa.rocks'},
});
}

describe('Simulator artifact', () => {
it('returns a simulator for "provided" throttling', async () => {
const settings = {throttlingMethod: 'provided'};
const context = {settings, computedCache: new Map()};
const simulator = await LoadSimulator.request({
devtoolsLog,
settings,
}, context);
const simulator = await LoadSimulator.request({devtoolsLog, settings}, context);

assert.equal(Math.round(simulator._rtt), 3);
assert.equal(Math.round(simulator._throughput / 1024), 1590);
Expand All @@ -30,10 +35,7 @@ describe('Simulator artifact', () => {
const throttling = {requestLatencyMs: 375, downloadThroughputKbps: 900};
const settings = {throttlingMethod: 'devtools', throttling};
const context = {settings, computedCache: new Map()};
const simulator = await LoadSimulator.request({
devtoolsLog,
settings,
}, context);
const simulator = await LoadSimulator.request({devtoolsLog, settings}, context);

assert.equal(simulator._rtt, 100);
assert.equal(simulator._throughput / 1024, 1000);
Expand All @@ -45,14 +47,52 @@ describe('Simulator artifact', () => {
const throttling = {rttMs: 120, throughputKbps: 1000, cpuSlowdownMultiplier: 3};
const settings = {throttlingMethod: 'simulate', throttling};
const context = {settings, computedCache: new Map()};
const simulator = await LoadSimulator.request({
devtoolsLog,
settings,
}, context);
const simulator = await LoadSimulator.request({devtoolsLog, settings}, context);

assert.equal(simulator._rtt, 120);
assert.equal(simulator._throughput / 1024, 1000);
assert.equal(simulator._cpuSlowdownMultiplier, 3);
assert.equal(simulator._layoutTaskMultiplier, 1.5);
simulator.simulate(createNetworkNode());

const {additionalRttByOrigin, serverResponseTimeByOrigin} = simulator._connectionPool._options;
expect(additionalRttByOrigin.get('https://pwa.rocks')).toMatchInlineSnapshot(
`0.3960000176447025`
);
expect(serverResponseTimeByOrigin.get('https://pwa.rocks')).toMatchInlineSnapshot(
`159.42199996789026`
);
});

it('returns a simulator with precomputed lantern data', async () => {
const precomputedLanternData = {
additionalRttByOrigin: {
'https://pwa.rocks': 1000,
'https://www.googletagmanager.com': 500,
'https://www.google-analytics.com': 1000,
},
serverResponseTimeByOrigin: {
'https://pwa.rocks': 150,
'https://www.googletagmanager.com': 200,
'https://www.google-analytics.com': 400,
},
};

const settings = {throttlingMethod: 'simulate', precomputedLanternData};
const context = {settings, computedCache: new Map()};
const simulator = await LoadSimulator.request({devtoolsLog, settings}, context);
simulator.simulate(createNetworkNode());

const {additionalRttByOrigin, serverResponseTimeByOrigin} = simulator._connectionPool._options;
expect(additionalRttByOrigin).toEqual(new Map([
['https://pwa.rocks', 1000],
['https://www.googletagmanager.com', 500],
['https://www.google-analytics.com', 1000],
]));
expect(serverResponseTimeByOrigin).toEqual(new Map([
['https://pwa.rocks', 150],
['https://www.googletagmanager.com', 200],
['https://www.google-analytics.com', 400],
]));
patrickhulce marked this conversation as resolved.
Show resolved Hide resolved
});
});
Loading