Skip to content

Commit

Permalink
core(simulator): add DNS information
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Jul 3, 2018
1 parent dc0a4a4 commit f1bf96d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
79 changes: 79 additions & 0 deletions lighthouse-core/lib/dependency-graph/simulator/dns-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @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';

const DNS_RESOLUTION_RTT_MULTIPLIER = 1.5;

module.exports = class DNSCache {
/**
* @param {{rtt: number}} options
*/
constructor(options) {
this._options = Object.assign(
{
rtt: undefined,
},
options
);

if (!this._options.rtt) {
throw new Error('Cannot create DNS cache with no rtt');
}

this._rtt = this._options.rtt;
/** @type {Map<string, {resolvedAt: number}>} */
this._resolvedDomainNames = new Map();
}

/**
* @param {LH.Artifacts.NetworkRequest} request
* @param {number=} requestedAt
* @return {number}
*/
getTimeUntilResolution(request, requestedAt) {
const shouldUpdateCache = typeof requestedAt === 'undefined';
requestedAt = requestedAt || 0;

const domain = request.parsedURL.host;
const cacheEntry = this._resolvedDomainNames.get(domain);
let timeUntilResolved = this._rtt * DNS_RESOLUTION_RTT_MULTIPLIER;
if (cacheEntry) {
const timeUntilCachedIsResolved = Math.max(cacheEntry.resolvedAt - requestedAt, 0);
timeUntilResolved = Math.min(timeUntilCachedIsResolved, timeUntilResolved);
}

const resolvedAt = requestedAt + timeUntilResolved;
if (shouldUpdateCache) this._updateCacheResolvedAtIfNeeded(request, resolvedAt);

return timeUntilResolved;
}

/**
* @param {LH.Artifacts.NetworkRequest} request
* @param {number} resolvedAt
*/
_updateCacheResolvedAtIfNeeded(request, resolvedAt) {
const domain = request.parsedURL.host;
const cacheEntry = this._resolvedDomainNames.get(domain) || {resolvedAt};
cacheEntry.resolvedAt = Math.min(cacheEntry.resolvedAt, resolvedAt);
this._resolvedDomainNames.set(domain, cacheEntry);
}

/**
* Forcefully sets the DNS resolution time for a record.
* Useful for testing and alternate execution simulations.
*
* @param {string} domain
* @param {number} resolvedAt
*/
setResolvedAt(domain, resolvedAt) {
this._resolvedDomainNames.set(domain, {resolvedAt});
}

static get RTT_MULTIPLIER() {
return DNS_RESOLUTION_RTT_MULTIPLIER;
}
};
18 changes: 13 additions & 5 deletions lighthouse-core/lib/dependency-graph/simulator/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const BaseNode = require('../base-node');
const TcpConnection = require('./tcp-connection');
const ConnectionPool = require('./connection-pool');
const DNSCache = require('./dns-cache');
const mobile3G = require('../../../config/constants').throttling.mobile3G;

/** @typedef {BaseNode.Node} Node */
Expand Down Expand Up @@ -66,6 +67,7 @@ class Simulator {
/** @type {Map<string, number>} */
this._numberInProgressByType = new Map();
this._nodes = {};
this._dns = new DNSCache({rtt: this._rtt});
// @ts-ignore
this._connectionPool = /** @type {ConnectionPool} */ (null);
}
Expand Down Expand Up @@ -260,20 +262,23 @@ class Simulator {
* @return {number}
*/
_estimateNetworkTimeRemaining(networkNode) {
const record = networkNode.record;
const timingData = this._getTimingData(networkNode);

let timeElapsed = 0;
if (networkNode.fromDiskCache) {
// Rough access time for seeking to location on disk and reading sequentially
// Rough access time for seeking to location on disk and reading sequentially = 8ms + 20ms/MB
// @see http://norvig.com/21-days.html#answers
const sizeInMb = (networkNode.record.resourceSize || 0) / 1024 / 1024;
const sizeInMb = (record.resourceSize || 0) / 1024 / 1024;
timeElapsed = 8 + 20 * sizeInMb - timingData.timeElapsed;
} else {
// If we're estimating time remaining, we already acquired a connection for this record, definitely non-null
const connection = /** @type {TcpConnection} */ (this._acquireConnection(networkNode.record));
const connection = /** @type {TcpConnection} */ (this._acquireConnection(record));
const dnsResolutionTime = this._dns.getTimeUntilResolution(record, timingData.startTime);
const timeAlreadyElapsed = timingData.timeElapsed;
const calculation = connection.simulateDownloadUntil(
networkNode.record.transferSize - timingData.bytesDownloaded,
{timeAlreadyElapsed: timingData.timeElapsed, maximumTimeToElapse: Infinity}
record.transferSize - timingData.bytesDownloaded,
{timeAlreadyElapsed, dnsResolutionTime, maximumTimeToElapse: Infinity}
);

timeElapsed = calculation.timeElapsed;
Expand Down Expand Up @@ -318,9 +323,11 @@ class Simulator {
const record = node.record;
// If we're updating the progress, we already acquired a connection for this record, definitely non-null
const connection = /** @type {TcpConnection} */ (this._acquireConnection(record));
const dnsResolutionTime = this._dns.getTimeUntilResolution(record, timingData.startTime);
const calculation = connection.simulateDownloadUntil(
record.transferSize - timingData.bytesDownloaded,
{
dnsResolutionTime,
timeAlreadyElapsed: timingData.timeElapsed,
maximumTimeToElapse: timePeriodLength - timingData.timeElapsedOvershoot,
}
Expand Down Expand Up @@ -386,6 +393,7 @@ class Simulator {

// initialize the necessary data containers
this._flexibleOrdering = !!options.flexibleOrdering;
this._dns = new DNSCache({rtt: this._rtt});
this._initializeConnectionPool(graph);
this._initializeAuxiliaryData();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ class TcpConnection {
* @return {DownloadResults}
*/
simulateDownloadUntil(bytesToDownload, options) {
const {timeAlreadyElapsed = 0, maximumTimeToElapse = Infinity} = options || {};
const {timeAlreadyElapsed = 0, maximumTimeToElapse = Infinity, dnsResolutionTime = 0} =
options || {};

if (this._warmed && this._h2) {
bytesToDownload -= this._h2OverflowBytesDownloaded;
Expand All @@ -133,6 +134,8 @@ class TcpConnection {
let handshakeAndRequest = oneWayLatency;
if (!this._warmed) {
handshakeAndRequest =
// DNS lookup
dnsResolutionTime +
// SYN
oneWayLatency +
// SYN ACK
Expand Down Expand Up @@ -188,6 +191,7 @@ module.exports = TcpConnection;

/**
* @typedef DownloadOptions
* @property {number} [dnsResolutionTime]
* @property {number} [timeAlreadyElapsed]
* @property {number} [maximumTimeToElapse]
*/
Expand Down

0 comments on commit f1bf96d

Please sign in to comment.