Skip to content

Commit

Permalink
Merge branch 'master' of github.com:GoogleChrome/lighthouse
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Sep 2, 2016
2 parents 364ba40 + 229aead commit 96069e4
Show file tree
Hide file tree
Showing 22 changed files with 174 additions and 190 deletions.
3 changes: 0 additions & 3 deletions lighthouse-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const cli = yargs

.group([
'mobile',
'load-page',
'save-assets',
'save-artifacts',
'list-all-audits',
Expand All @@ -56,7 +55,6 @@ const cli = yargs
], 'Configuration:')
.describe({
'mobile': 'Emulates a Nexus 5X',
'load-page': 'Loads the page',
'save-assets': 'Save the trace contents & screenshots to disk',
'save-artifacts': 'Save all gathered artifacts to disk',
'list-all-audits': 'Prints a list of all available audits and exits',
Expand Down Expand Up @@ -89,7 +87,6 @@ Example: --output-path=./lighthouse-results.html`

// default values
.default('mobile', true)
.default('load-page', true)
.default('output', Printer.OUTPUT_MODE.pretty)
.default('output-path', 'stdout')
.check(argv => {
Expand Down
4 changes: 3 additions & 1 deletion lighthouse-cli/test/fixtures/smoketest-offline-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
"passes": [{
"loadPage": true,
"gatherers": [
"viewport"
"viewport",
"url"
]
},{
"loadPage": true,
"network": true,
"gatherers": [
"service-worker",
"offline"
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/audits/first-meaningful-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class FirstMeaningfulPaint extends Audit {
e.name === 'Paint' ||
e.name === 'TracingStartedInPage';
}).forEach(event => {
// Grab the page's ID from TracingStartedInPage
// Grab the page's ID from the first TracingStartedInPage in the trace
if (event.name === 'TracingStartedInPage' && !mainFrameID) {
mainFrameID = event.args.data.page;
}
Expand Down
40 changes: 37 additions & 3 deletions lighthouse-core/audits/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,29 @@

'use strict';

const url = require('url');
const Audit = require('./audit');

/**
* @param {string} targetURL
* @return {string}
*/
function getOrigin(targetURL) {
const parsedURL = url.parse(targetURL);
return `${parsedURL.protocol}//${parsedURL.hostname}` +
(parsedURL.port ? `:${parsedURL.port}` : '');
}

/**
* @param {!Array<!ServiceWorkerVersion>} versions
* @param {string} url
* @return {(!ServiceWorkerVersion|undefined)}
*/
function getActivatedServiceWorker(versions, url) {
const origin = getOrigin(url);
return versions.find(v => v.status === 'activated' && getOrigin(v.scriptURL) === origin);
}

class ServiceWorker extends Audit {
/**
* @return {!AuditMeta}
Expand All @@ -28,7 +49,7 @@ class ServiceWorker extends Audit {
category: 'Offline',
name: 'service-worker',
description: 'Has a registered Service Worker',
requiredArtifacts: ['ServiceWorker']
requiredArtifacts: ['URL', 'ServiceWorker']
};
}

Expand All @@ -37,9 +58,22 @@ class ServiceWorker extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
if (!artifacts.ServiceWorker.versions) {
// Error in ServiceWorker gatherer.
return ServiceWorker.generateAuditResult({
rawValue: false,
debugString: artifacts.ServiceWorker.debugString
});
}

// Find active service worker for this URL. Match against artifacts.URL so
// audit accounts for any redirects.
const version = getActivatedServiceWorker(artifacts.ServiceWorker.versions, artifacts.URL);
const debugString = version ? undefined : 'No active service worker found for this origin.';

return ServiceWorker.generateAuditResult({
rawValue: !!artifacts.ServiceWorker.version,
debugString: artifacts.ServiceWorker.debugString
rawValue: !!version,
debugString: debugString
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/closure/typedefs/Audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ AuditMeta.prototype.description;
AuditMeta.prototype.optimalValue;

/** @type {!Array<string>} */
AuditMeta.prototype.requiredArtifacts
AuditMeta.prototype.requiredArtifacts;
4 changes: 2 additions & 2 deletions lighthouse-core/closure/typedefs/ServiceWorkerArtifact.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ function ServiceWorkerArtifact() {}
/** @type {(string|undefined)} */
ServiceWorkerArtifact.prototype.debugString;

/** @type {(!ServiceWorkerVersion|undefined)} */
ServiceWorkerArtifact.prototype.version;
/** @type {(!Array<!ServiceWorkerVersion>|undefined)} */
ServiceWorkerArtifact.prototype.versions;

/**
* @struct
Expand Down
2 changes: 2 additions & 0 deletions lighthouse-core/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
]
},
{
"passName": "offlinePass",
"loadPage": true,
"network": true,
"gatherers": [
"service-worker",
"offline"
Expand Down
24 changes: 24 additions & 0 deletions lighthouse-core/gather/drivers/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,30 @@ class Driver {
]);
}

/**
* Emulate internet disconnection.
* @return {!Promise}
*/
goOffline() {
return this.sendCommand('Network.enable').then(_ => emulation.goOffline(this));
}

/**
* Enable internet connection, using emulated mobile settings if
* `options.flags.mobile` is true.
* @param {!Object} options
* @return {!Promise}
*/
goOnline(options) {
return this.sendCommand('Network.enable').then(_ => {
if (options.flags.mobile) {
return emulation.enableNetworkThrottling(this);
}

return emulation.disableNetworkThrottling(this);
});
}

cleanAndDisableBrowserCaches() {
return Promise.all([
this.clearBrowserCache(),
Expand Down
4 changes: 0 additions & 4 deletions lighthouse-core/gather/gather-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,6 @@ class GatherRunner {
options.flags.mobile = true;
}

if (typeof options.flags.loadPage === 'undefined') {
options.flags.loadPage = true;
}

passes = this.instantiateGatherers(passes, options.config.configDir);

return driver.connect()
Expand Down
61 changes: 8 additions & 53 deletions lighthouse-core/gather/gatherers/offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,68 +14,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* global XMLHttpRequest, __returnResults */

'use strict';

const Gatherer = require('./gatherer');

// *WARNING* do not use fetch.. due to it requiring window focus to fire.
// Request the current page by issuing a XMLHttpRequest request to ''
// and storing the status code on the window.
// This runs in the context of the page, so we don't cover it with unit tests.
/* istanbul ignore next */
const requestPage = function() {
const oReq = new XMLHttpRequest();
oReq.onload = oReq.onerror = e => {
// __returnResults is injected by driver.evaluateAsync
__returnResults(e.currentTarget.status);
};
oReq.open('GET', '');
oReq.send();
};

class Offline extends Gatherer {

static config(opts) {
return {
offline: opts.offline,
// values of 0 remove any active throttling. crbug.com/456324#c9
latency: 0,
downloadThroughput: 0,
uploadThroughput: 0
};
beforePass(options) {
return options.driver.goOffline();
}

static goOffline(driver) {
// Network.enable must be called for Network.emulateNetworkConditions to work
return driver.sendCommand('Network.enable').then(_ => {
driver.sendCommand('Network.emulateNetworkConditions', Offline.config({offline: true}));
});
}

static goOnline(driver) {
return driver.sendCommand('Network.emulateNetworkConditions', Offline.config({offline: false}));
}
afterPass(options, tracingData) {
const navigationRecord = tracingData.networkRecords.filter(record => {
return record._url === options.url && record._fetchedViaServiceWorker;
}).pop(); // Take the last record that matches.

afterPass(options) {
const driver = options.driver;
this.artifact = navigationRecord ? navigationRecord.statusCode : -1;

// TODO eventually we will want to walk all network
// requests that the page initially made and retry them.
return Offline
.goOffline(driver)
.then(_ => driver.evaluateAsync(`(${requestPage.toString()}())`))
.then(offlineResponseCode => {
this.artifact = offlineResponseCode;
})
.then(_ => Offline.goOnline(driver))
.catch(_ => {
this.artifact = {
offlineResponseCode: -1
};
});
return options.driver.goOnline(options);
}
}

Expand Down
26 changes: 1 addition & 25 deletions lighthouse-core/gather/gatherers/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,13 @@
const Gatherer = require('./gatherer');

class ServiceWorker extends Gatherer {

/**
* @param {string} url
* @return {string}
*/
static getOrigin(url) {
const parsedURL = require('url').parse(url);
return `${parsedURL.protocol}//${parsedURL.hostname}` +
(parsedURL.port ? `:${parsedURL.port}` : '');
}

/**
* @param {!Array<!ServiceWorkerVersion>} versions
* @param {string} url
* @return {(!ServiceWorkerVersion|undefined)}
*/
static getActivatedServiceWorker(versions, url) {
const origin = this.getOrigin(url);
return versions.find(v => v.status === 'activated' && this.getOrigin(v.scriptURL) === origin);
}

beforePass(options) {
const driver = options.driver;
return driver
.getServiceWorkerVersions()
.then(data => {
const version = ServiceWorker.getActivatedServiceWorker(data.versions, options.url);
const debugString = version ? undefined : 'No active service worker found for this origin.';
return {
version,
debugString
versions: data.versions
};
})
.catch(err => {
Expand Down
27 changes: 26 additions & 1 deletion lighthouse-core/lib/emulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ const TYPICAL_MOBILE_THROTTLING_METRICS = {
offline: false
};

const OFFLINE_METRICS = {
offline: true,
// values of 0 remove any active throttling. crbug.com/456324#c9
latency: 0,
downloadThroughput: 0,
uploadThroughput: 0
};

const NO_THROTTLING_METRICS = {
latency: 0,
downloadThroughput: 0,
uploadThroughput: 0,
offline: false
};

function enableNexus5X(driver) {
/**
* Finalizes touch emulation by enabling `"ontouchstart" in window` feature detect
Expand Down Expand Up @@ -90,7 +105,17 @@ function enableNetworkThrottling(driver) {
return driver.sendCommand('Network.emulateNetworkConditions', TYPICAL_MOBILE_THROTTLING_METRICS);
}

function disableNetworkThrottling(driver) {
return driver.sendCommand('Network.emulateNetworkConditions', NO_THROTTLING_METRICS);
}

function goOffline(driver) {
return driver.sendCommand('Network.emulateNetworkConditions', OFFLINE_METRICS);
}

module.exports = {
enableNexus5X,
enableNetworkThrottling
enableNetworkThrottling,
disableNetworkThrottling,
goOffline
};
2 changes: 1 addition & 1 deletion lighthouse-core/lib/network-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class NetworkRecorder extends EventEmitter {

onResourceChangedPriority(data) {
this.networkManager._dispatcher.resourceChangedPriority(data.requestId,
data.newPriority, data.timestamp);
data.newPriority, data.timestamp);
}

static recordsFromLogs(logs) {
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/lib/traces/tracing-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class TraceProcessor {
percentiles = [0.5, 0.75, 0.9, 0.99, 1];
}

// Find the main thread.
// Find the main thread via the first TracingStartedInPage event in the trace
const startEvent = trace.traceEvents.find(event => {
return event.name === 'TracingStartedInPage';
});
Expand Down
4 changes: 0 additions & 4 deletions lighthouse-core/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ class Runner {
opts.flags.mobile = true;
}

if (typeof opts.flags.loadPage === 'undefined') {
opts.flags.loadPage = true;
}

const config = opts.config;

// save the initialUrl provided by the user
Expand Down
Loading

0 comments on commit 96069e4

Please sign in to comment.