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(lhr): move runtime config to report, configSettings #5122

Merged
merged 5 commits into from
May 6, 2018
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
1 change: 1 addition & 0 deletions lighthouse-core/closure/closure-type-checking.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ gulp.task('compile-report', () => {
// externs
'closure/third_party/commonjs.js',
'closure/typedefs/viewer-externs.js',
'closure/typedefs/devtools-externs.js',

'lib/file-namer.js',
'report/html/renderer/*.js',
Expand Down
53 changes: 53 additions & 0 deletions lighthouse-core/closure/typedefs/devtools-externs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @license Copyright 2018 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';

/**
* Typing externs file needed for DevTools compilation.
* @externs
*/

/**
* @struct
* @record
*/
function ThrottlingSettings() {}

/** @type {number} */
ThrottlingSettings.prototype.rttMs;

/** @type {number} */
ThrottlingSettings.prototype.throughputKbps;

/** @type {number} */
ThrottlingSettings.prototype.requestLatencyMs;

/** @type {number} */
ThrottlingSettings.prototype.downloadThroughputKbps;

/** @type {number} */
ThrottlingSettings.prototype.uploadThroughputKbps;

/** @type {number} */
ThrottlingSettings.prototype.cpuSlowdownMultiplier;

var LH = {};
LH.Config = {};

/**
* @struct
* @record
*/
LH.Config.Settings = function() {};

/** @type {boolean} */
LH.Config.Settings.prototype.disableDeviceEmulation;

/** @type {string} */
LH.Config.Settings.prototype.throttlingMethod;

/** @type {ThrottlingSettings} */
LH.Config.Settings.prototype.throttling;
46 changes: 0 additions & 46 deletions lighthouse-core/lib/emulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
const Driver = require('../gather/driver'); // eslint-disable-line no-unused-vars
const mobile3G = require('../config/constants').throttling.mobile3G;

const NBSP = '\xa0';

/**
* Nexus 5X metrics adapted from emulated_devices/module.json
* @type {LH.Crdp.Emulation.SetDeviceMetricsOverrideRequest}
Expand Down Expand Up @@ -130,55 +128,11 @@ function disableCPUThrottling(driver) {
return driver.sendCommand('Emulation.setCPUThrottlingRate', NO_CPU_THROTTLE_METRICS);
}

/**
* @param {LH.Config.Settings} settings
* @return {{deviceEmulation: string, cpuThrottling: string, networkThrottling: string}}
*/
function getEmulationDesc(settings) {
let cpuThrottling;
let networkThrottling;

/** @type {LH.ThrottlingSettings} */
const throttling = settings.throttling || {};

switch (settings.throttlingMethod) {
case 'provided':
cpuThrottling = 'Provided by environment';
networkThrottling = 'Provided by environment';
break;
case 'devtools': {
const {cpuSlowdownMultiplier, requestLatencyMs} = throttling;
cpuThrottling = `${cpuSlowdownMultiplier}x slowdown (DevTools)`;
networkThrottling = `${requestLatencyMs}${NBSP}ms HTTP RTT, ` +
`${throttling.downloadThroughputKbps}${NBSP}Kbps down, ` +
`${throttling.uploadThroughputKbps}${NBSP}Kbps up (DevTools)`;
break;
}
case 'simulate': {
const {cpuSlowdownMultiplier, rttMs, throughputKbps} = throttling;
cpuThrottling = `${cpuSlowdownMultiplier}x slowdown (Simulated)`;
networkThrottling = `${rttMs}${NBSP}ms TCP RTT, ` +
`${throughputKbps}${NBSP}Kbps throughput (Simulated)`;
break;
}
default:
cpuThrottling = 'Unknown';
networkThrottling = 'Unknown';
}

return {
deviceEmulation: settings.disableDeviceEmulation ? 'Disabled' : 'Nexus 5X',
cpuThrottling,
networkThrottling,
};
}

module.exports = {
enableNexus5X,
enableNetworkThrottling,
clearAllNetworkEmulation,
enableCPUThrottling,
disableCPUThrottling,
goOffline,
getEmulationDesc,
};
9 changes: 3 additions & 6 deletions lighthouse-core/report/html/renderer/report-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class ReportRenderer {
this._dom.find('.lh-env__item__ua', header).textContent = report.userAgent;

const env = this._dom.find('.lh-env__items', header);
report.runtimeConfig.environment.forEach(runtime => {
const environment = Util.getEnvironmentDisplayValues(report.configSettings || {});
environment.forEach(runtime => {
const item = this._dom.cloneTemplate('#tmpl-lh-env__items', env);
this._dom.find('.lh-env__name', item).textContent = runtime.name;
this._dom.find('.lh-env__description', item).textContent = runtime.description;
Expand Down Expand Up @@ -263,11 +264,7 @@ ReportRenderer.GroupJSON; // eslint-disable-line no-unused-expressions
* audits: !Object<string, !ReportRenderer.AuditResultJSON>,
* reportCategories: !Array<!ReportRenderer.CategoryJSON>,
* reportGroups: !Object<string, !ReportRenderer.GroupJSON>,
* runtimeConfig: {
* blockedUrlPatterns: !Array<string>,
* extraHeaders: !Object<string, string>,
* environment: !Array<{description: string, enabled: boolean, name: string}>
* }
* configSettings: !LH.Config.Settings,
* }}
*/
ReportRenderer.ReportJSON; // eslint-disable-line no-unused-expressions
65 changes: 65 additions & 0 deletions lighthouse-core/report/html/renderer/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,71 @@ class Util {
static chainDuration(startTime, endTime) {
return Util.formatNumber((endTime - startTime) * 1000);
}

/**
* @param {LH.Config.Settings} settings
* @return {Array<{name: string, description: string}>}
*/
static getEnvironmentDisplayValues(settings) {
const emulationDesc = Util.getEmulationDescriptions(settings);

return [
{
name: 'Device Emulation',
description: emulationDesc.deviceEmulation,
},
{
name: 'Network Throttling',
description: emulationDesc.networkThrottling,
},
{
name: 'CPU Throttling',
description: emulationDesc.cpuThrottling,
},
];
}

/**
* @param {LH.Config.Settings} settings
* @return {{deviceEmulation: string, networkThrottling: string, cpuThrottling: string}}
*/
static getEmulationDescriptions(settings) {
let cpuThrottling;
let networkThrottling;

const throttling = settings.throttling;

switch (settings.throttlingMethod) {
case 'provided':
cpuThrottling = 'Provided by environment';
networkThrottling = 'Provided by environment';
break;
case 'devtools': {
const {cpuSlowdownMultiplier, requestLatencyMs} = throttling;
cpuThrottling = `${Util.formatNumber(cpuSlowdownMultiplier)}x slowdown (DevTools)`;
networkThrottling = `${Util.formatNumber(requestLatencyMs)}${NBSP}ms HTTP RTT, ` +
`${Util.formatNumber(throttling.downloadThroughputKbps)}${NBSP}Kbps down, ` +
`${Util.formatNumber(throttling.uploadThroughputKbps)}${NBSP}Kbps up (DevTools)`;
break;
}
case 'simulate': {
const {cpuSlowdownMultiplier, rttMs, throughputKbps} = throttling;
cpuThrottling = `${Util.formatNumber(cpuSlowdownMultiplier)}x slowdown (Simulated)`;
networkThrottling = `${Util.formatNumber(rttMs)}${NBSP}ms TCP RTT, ` +
`${Util.formatNumber(throughputKbps)}${NBSP}Kbps throughput (Simulated)`;
break;
}
default:
cpuThrottling = 'Unknown';
networkThrottling = 'Unknown';
}

return {
deviceEmulation: settings.disableDeviceEmulation ? 'Disabled' : 'Nexus 5X',
cpuThrottling,
networkThrottling,
};
}
}

if (typeof module !== 'undefined' && module.exports) {
Expand Down
31 changes: 1 addition & 30 deletions lighthouse-core/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const Driver = require('./gather/driver.js');
const GatherRunner = require('./gather/gather-runner');
const ReportScoring = require('./scoring');
const Audit = require('./audits/audit');
const emulation = require('./lib/emulation');
const log = require('lighthouse-logger');
const assetSaver = require('./lib/asset-saver');
const fs = require('fs');
Expand Down Expand Up @@ -127,7 +126,7 @@ class Runner {
url: opts.url,
runWarnings: lighthouseRunWarnings,
audits: resultsById,
runtimeConfig: Runner.getRuntimeConfig(settings),
configSettings: settings,
reportCategories,
reportGroups: opts.config.groups,
timing: {total: Date.now() - startTime},
Expand Down Expand Up @@ -406,34 +405,6 @@ class Runner {
throw new Error(errorString + ` and '${relativePath}')`);
}

/**
* Get runtime configuration specified by the flags
* @param {LH.Config.Settings} settings
* @return {LH.Result.RuntimeConfig}
*/
static getRuntimeConfig(settings) {
const emulationDesc = emulation.getEmulationDesc(settings);

return {
environment: [
{
name: 'Device Emulation',
description: emulationDesc['deviceEmulation'],
},
{
name: 'Network Throttling',
description: emulationDesc['networkThrottling'],
},
{
name: 'CPU Throttling',
description: emulationDesc['cpuThrottling'],
},
],
blockedUrlPatterns: settings.blockedUrlPatterns || [],
extraHeaders: settings.extraHeaders || {},
};
}

/**
* Get path to use for -G and -A modes. Defaults to $CWD/latest-run
* @param {LH.Config.Settings} settings
Expand Down
1 change: 1 addition & 0 deletions lighthouse-core/scripts/cleanup-LHR-for-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ writeFileSync(filename, cleanAndFormatLHR(data), 'utf8');
*/
function cleanAndFormatLHR(lhrString) {
const lhr = JSON.parse(lhrString);
delete lhr.configSettings.auditMode;
delete lhr.timing;
if (extraFlag !== '--only-remove-timing') {
for (const auditResult of Object.values(lhr.audits)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ describe('ReportRenderer', () => {

// Check runtime settings were populated.
const names = Array.from(header.querySelectorAll('.lh-env__name')).slice(1);
const descriptions = header.querySelectorAll('.lh-env__description');
sampleResults.runtimeConfig.environment.forEach((env, i) => {
assert.equal(names[i].textContent, env.name);
assert.equal(descriptions[i].textContent, env.description);
});
const descriptions = Array.from(header.querySelectorAll('.lh-env__description'));
assert.equal(names.length, 3);
assert.equal(descriptions.length, 3);
const descriptionsTxt = descriptions.map(el => el.textContent).join('\n');
assert.ok(/Nexus/.test(descriptionsTxt), 'should have added device emulation');
assert.ok(/RTT/.test(descriptionsTxt), 'should have added network');
assert.ok(/\dx/.test(descriptionsTxt), 'should have added CPU');
});

it('should not mutate a report object', () => {
Expand Down
43 changes: 43 additions & 0 deletions lighthouse-core/test/report/html/renderer/util-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,49 @@ describe('util helpers', () => {
assert.equal(Util.calculateRating(1.00), 'pass');
});

it('builds device emulation string', () => {
const get = opts => Util.getEmulationDescriptions(opts).deviceEmulation;
assert.equal(get({disableDeviceEmulation: true}), 'Disabled');
assert.equal(get({disableDeviceEmulation: false}), 'Nexus 5X');
});

it('builds throttling strings when provided', () => {
const descriptions = Util.getEmulationDescriptions({throttlingMethod: 'provided'});
assert.equal(descriptions.cpuThrottling, 'Provided by environment');
assert.equal(descriptions.networkThrottling, 'Provided by environment');
});

it('builds throttling strings when devtools', () => {
const descriptions = Util.getEmulationDescriptions({
throttlingMethod: 'devtools',
throttling: {
cpuSlowdownMultiplier: 4.5,
requestLatencyMs: 565,
downloadThroughputKbps: 1400.00000000001,
uploadThroughputKbps: 600,
},
});

// eslint-disable-next-line max-len
assert.equal(descriptions.networkThrottling, '565\xa0ms HTTP RTT, 1,400\xa0Kbps down, 600\xa0Kbps up (DevTools)');
assert.equal(descriptions.cpuThrottling, '4.5x slowdown (DevTools)');
});

it('builds throttling strings when simulate', () => {
const descriptions = Util.getEmulationDescriptions({
throttlingMethod: 'simulate',
throttling: {
cpuSlowdownMultiplier: 2,
rttMs: 150,
throughputKbps: 1600,
},
});

// eslint-disable-next-line max-len
assert.equal(descriptions.networkThrottling, '150\xa0ms TCP RTT, 1,600\xa0Kbps throughput (Simulated)');
assert.equal(descriptions.cpuThrottling, '2x slowdown (Simulated)');
});

it('formats display values', () => {
const format = arg => Util.formatDisplayValue(arg);
assert.equal(format(undefined), '');
Expand Down
38 changes: 21 additions & 17 deletions lighthouse-core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -4432,23 +4432,27 @@
"helpText": "Run the [Structured Data Testing Tool](https://search.google.com/structured-data/testing-tool/) and the [Structured Data Linter](http://linter.structured-data.org/) to validate structured data. [Learn more](https://developers.google.com/search/docs/guides/mark-up-content)."
}
},
"runtimeConfig": {
"environment": [
{
"name": "Device Emulation",
"description": "Nexus 5X"
},
{
"name": "Network Throttling",
"description": "562.5 ms HTTP RTT, 1474.5600000000002 Kbps down, 675 Kbps up (DevTools)"
},
{
"name": "CPU Throttling",
"description": "4x slowdown (DevTools)"
}
],
"blockedUrlPatterns": [],
"extraHeaders": {}
"configSettings": {
Copy link
Member

Choose a reason for hiding this comment

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

this is going to be great to have

"output": "json",
"maxWaitForLoad": 45000,
"throttlingMethod": "devtools",
"throttling": {
"rttMs": 150,
"throughputKbps": 1638.4,
"requestLatencyMs": 562.5,
"downloadThroughputKbps": 1474.5600000000002,
"uploadThroughputKbps": 675,
"cpuSlowdownMultiplier": 4
},
"gatherMode": false,
"disableStorageReset": false,
"disableDeviceEmulation": false,
"blockedUrlPatterns": null,
"additionalTraceCategories": null,
"extraHeaders": null,
"onlyAudits": null,
"onlyCategories": null,
"skipAudits": null
},
"reportCategories": [
{
Expand Down
Loading