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(network): handle LR transferSize #5895

Merged
merged 2 commits into from
Aug 23, 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
28 changes: 28 additions & 0 deletions lighthouse-core/lib/network-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ module.exports = class NetworkRequest {
this.initiatorRequest = undefined;
/** @type {HeaderEntry[]} */
this.responseHeaders = [];
/** @type {string} */
this.responseHeadersText = '';

this.fetchedViaServiceWorker = false;
/** @type {string|undefined} */
Expand Down Expand Up @@ -172,6 +174,7 @@ module.exports = class NetworkRequest {
}

this._updateResponseReceivedTimeIfNecessary();
this._updateTransferSizeForLightRiderIfNecessary();
}

/**
Expand Down Expand Up @@ -234,12 +237,15 @@ module.exports = class NetworkRequest {
this.timing = response.timing;
if (resourceType) this.resourceType = RESOURCE_TYPES[resourceType];
this.mimeType = response.mimeType;
this.responseHeadersText = response.headersText || '';
this.responseHeaders = NetworkRequest._headersDictToHeadersArray(response.headers);

this.fetchedViaServiceWorker = !!response.fromServiceWorker;

if (this.fromMemoryCache) this.timing = undefined;
if (this.timing) this._recomputeTimesWithResourceTiming(this.timing);

this._updateTransferSizeForLightRiderIfNecessary();
}

/**
Expand Down Expand Up @@ -269,6 +275,28 @@ module.exports = class NetworkRequest {
this.responseReceivedTime = Math.min(this.endTime, this.responseReceivedTime);
}

/**
* LR loses transfer size information and passes it in the 'X-Original-Content-Length' header.
*/
_updateTransferSizeForLightRiderIfNecessary() {
// Bail if we're not in LightRider, this only applies there.
if (!global.isLightRider) return;
Copy link
Member

Choose a reason for hiding this comment

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

I figure this is the best global reference we got.. better than self? WFM

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah was just using what we decided to go with in web-inspector.js last time

// Bail if we somehow already have transfer size data.
if (this.transferSize) return;
// Bail if we didn't get any response headers.
if (!this.responseHeadersText) return;

const originalContentLength = this.responseHeaders.
find(item => item.name === 'X-Original-Content-Length');
// Bail if the x-original-content-length header was missing.
if (!originalContentLength) return;

// Transfer size is the original content length + length of headers
const contentBytes = parseFloat(originalContentLength.value);
Copy link
Member

Choose a reason for hiding this comment

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

I am tickled. :)

const headerBytes = this.responseHeadersText.length;
this.transferSize = contentBytes + headerBytes;
}

/**
* Convert the requestId to backend-version by removing the `:redirect` portion
*
Expand Down
3 changes: 3 additions & 0 deletions lighthouse-extension/app/src/lighthouse-ext-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ async function runLighthouseInExtension(flags, categoryIDs) {
* @return {Promise<string|Array<string>|void>}
*/
async function runLighthouseInLR(connection, url, flags, {lrDevice, categoryIDs, logAssets}) {
// Certain fixes need to kick-in under LR, see https://github.com/GoogleChrome/lighthouse/issues/5839
Copy link
Member

Choose a reason for hiding this comment

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

One if the origin RTT estimates only applies in LR. Do you want to re use this there? Fine if you don't.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nah that's ok that fallback is a lot more sound than this one :)

global.isLightRider = true;

// Override default device to be desktop, since LR default device has viewport 1x1.
connection.sendCommand('Emulation.setDeviceMetricsOverride',
{width: 800, height: 600, deviceScaleFactor: 0, mobile: false});
Expand Down
11 changes: 11 additions & 0 deletions typings/node.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @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.
*/

declare module NodeJS {
interface Global {
isLightRider?: boolean;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@brendankenny on a scale of 1-5, how offensive is this to you?

}
}