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 4 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;
}

/**
* @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.',
'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 @@ -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
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) {
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;
}

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

module.exports = {
saveArtifacts,
loadArtifacts,
saveAssets,
prepareAssets,
saveTrace,
logAssets,
saveLanternNetworkData,
};
54 changes: 50 additions & 4 deletions lighthouse-core/test/computed/load-simulator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
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 () => {
Expand Down Expand Up @@ -45,14 +53,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],
]));
});
});
28 changes: 28 additions & 0 deletions lighthouse-core/test/lib/asset-saver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const dbwTrace = require('../results/artifacts/defaultPass.trace.json');
const dbwResults = require('../results/sample_v2.json');
const Audit = require('../../audits/audit.js');
const fullTraceObj = require('../fixtures/traces/progressive-app-m60.json');
const devtoolsLog = require('../fixtures/traces/progressive-app-m60.devtools.log.json');

// deepStrictEqual can hang on a full trace, we assert trace same-ness like so
function assertTraceEventsEqual(traceEventsA, traceEventsB) {
Expand Down Expand Up @@ -165,4 +166,31 @@ describe('asset-saver helper', () => {
assert.strictEqual(artifacts.traces.defaultPass.traceEvents.length, 12);
});
});

describe('saveLanternNetworkData', () => {
const outputFilename = 'test-lantern-network-data.json';

afterEach(() => {
fs.unlinkSync(outputFilename);
});

it('saves the network analysis to disk', async () => {
await assetSaver.saveLanternNetworkData(devtoolsLog, outputFilename);

const results = JSON.parse(fs.readFileSync(outputFilename, 'utf8'));

expect(results).toEqual({
additionalRttByOrigin: {
'https://pwa.rocks': expect.any(Number),
'https://www.google-analytics.com': expect.any(Number),
'https://www.googletagmanager.com': expect.any(Number),
},
serverResponseTimeByOrigin: {
'https://pwa.rocks': expect.any(Number),
'https://www.google-analytics.com': expect.any(Number),
'https://www.googletagmanager.com': expect.any(Number),
},
});
});
});
});
1 change: 1 addition & 0 deletions lighthouse-core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -3171,6 +3171,7 @@
"blockedUrlPatterns": null,
"additionalTraceCategories": null,
"extraHeaders": null,
"precomputedLanternData": null,
"onlyAudits": null,
"onlyCategories": null,
"skipAudits": null
Expand Down
11 changes: 11 additions & 0 deletions types/externs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ declare global {
cpuSlowdownMultiplier?: number
}

export interface PrecomputedLanternData {
additionalRttByOrigin: {[origin: string]: number};
serverResponseTimeByOrigin: {[origin: string]: number};
}

export type Locale = 'en-US'|'en'|'en-AU'|'en-GB'|'en-IE'|'en-SG'|'en-ZA'|'en-IN'|'ar-XB'|'ar'|'bg'|'bs'|'ca'|'cs'|'da'|'de'|'el'|'en-XA'|'es'|'fi'|'fil'|'fr'|'he'|'hi'|'hr'|'hu'|'gsw'|'id'|'in'|'it'|'iw'|'ja'|'ko'|'ln'|'lt'|'lv'|'mo'|'nl'|'nb'|'no'|'pl'|'pt'|'pt-PT'|'ro'|'ru'|'sk'|'sl'|'sr'|'sr-Latn'|'sv'|'ta'|'te'|'th'|'tl'|'tr'|'uk'|'vi'|'zh'|'zh-HK'|'zh-TW';

export type OutputMode = 'json' | 'html' | 'csv';
Expand Down Expand Up @@ -116,6 +121,8 @@ declare global {
skipAudits?: string[] | null;
// List of extra HTTP Headers to include
extraHeaders?: Crdp.Network.Headers | null; // See extraHeaders TODO in bin.js
// Precomputed lantern estimates to use instead of observed analysis
precomputedLanternData?: PrecomputedLanternData | null;
}

/**
Expand Down Expand Up @@ -157,6 +164,10 @@ declare global {
port: number;
hostname: string;
printConfig: boolean;
// Path to the file where precomputed lantern data should be read from.
precomputedLanternDataPath?: string;
// Path to the file where precomputed lantern data should be written to.
lanternDataOutputPath?: string;
}

export interface RunnerResult {
Expand Down