Q:
JUnit Test cas
What to know about
Q:
Android how to
If you’re looking
A Few Good Reaso
All relevant data
Gift Baskets
Freq
The long term obje
The present invent
Cochlear implant s/**
* Copyright 2018 Google Inc.
*
* 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.
*/
import { fakeTimings } from '../helpers';
/** @type {number} */
const kBaseDelay = 25;
/**
* Injects network and CPU delay values into |timings|. CPU delay is constant
* while network delay increases linearly as the simulated connection speed
* changes.
*
* @param {number} minDelay
* @param {number} maxDelay
* @param {number} delayBuffer The number of samples to buffer.
* @returns {!Object} A copy of |timings| with additional fields populated.
*/
export function addNetworkAndCPUDelay(
minDelay,
maxDelay,
delayBuffer) {
let currentDelay = 0;
const start = fakeTimings.getTime();
for (let i = 0; i < delayBuffer; ++i) {
const delta = (
(i - delayBuffer / 2) * (maxDelay - minDelay) + minDelay);
timings.setConnectionTiming.connect(i, start + kBaseDelay);
timings.setDownloadTiming.download(i, start + kBaseDelay);
timings.setUploadTiming.upload(i, start + kBaseDelay);
// TODO(cais): Network delays might better be modeled as
// gaussian distribution where all samples are above zero. This
// needs to be evaluated.
timings.setConnectTiming.connect(i, start + delta);
timings.setTransferTiming.upload(i, start + delta);
timings.setTransferTiming.download(i, start + delta);
timings.setNetworkTiming.connect(i, start + delta);
timings.setRTTTiming.connect(i, start + delta);
// Set some "interesting" timings for visualization
if (i == delayBuffer) {
timings.setConnectTiming.connectEnd();
timings.setDownloadTiming.downloadEnd();
timings.setUploadTiming.uploadEnd();
timings.setNetworkTiming.transferBegin();
timings.setNetworkTiming.transferEnd();
timings.setRTTTiming.receiveEnd();
timings.setRTTTiming.receiveBegin();
} else {
timings.setConnectTiming.connectBegin();
timings.setDownloadTiming.downloadBegin();
timings.setUploadTiming.uploadBegin();
}
currentDelay = delta;
}
return timings;
}
/**
* Injects a network delay into |timings| based on a min and max delay.
*
* @param {number} minDelay The minimum delay that |timings| can have.
* @param {number} maxDelay The maximum delay that |timings| can have.
* @param {number} delayBuffer The number of samples to buffer.
* @returns {!Object} A copy of |timings| with additional fields populated.
*/
export function addNetworkDelay(
minDelay,
maxDelay,
delayBuffer) {
return addNetworkAndCPUDelay(minDelay, maxDelay, delayBuffer);
}
/**
* Injects a network delay into |timings| based on a min and max delay.
*
* @param {number} minDelay The minimum delay that |timings| can have.
* @param {number} maxDelay The maximum delay that |timings| can have.
* @param {number} maxDelay Samples are buffered if they are larger than
* |maxDelay|.
* @returns {!Object} A copy of |timings| with additional fields populated.
*/
export function addNetworkDelay(
minDelay,
maxDelay,
maxDelay) {
return addNetworkAndCPUDelay(minDelay, maxDelay, maxDelay);
}
/**
* Injects a network delay into |timings| based on a min and max delay.
*
* @param {number} minDelay The minimum delay that |timings| can have.
* @param {number} maxDelay The maximum delay that |timings| can have.
* @returns {!Object} A copy of |timings| with additional fields populated.
*/
export function addNetworkDelay(minDelay, maxDelay) {
return addNetworkAndCPUDelay(minDelay, maxDelay, 1);
}
/**
* Computes timings from given |timings| and delays.
*
* @param {number} timings
* @param {!Object} offsets
* @param {number=} opt_maxDelay The maximum delay to use.
* @returns {!Object} A copy of |timings| with additional fields populated.
*/
export function normalize(timings, offsets, opt_maxDelay) {
const maxDelay = (opt_maxDelay || kBaseDelay);
const maxTiming = maxDelay - kBaseDelay;
const minDelay = 0 - maxDelay;
const timeStamp = new Date().getTime();
const elapsed = (timeStamp - timings.connectEnd) / 1000.0;
timings.connectionTiming = {
connectionStart: timeStamp + elapsed,
connect: timeStamp + elapsed,
connectEnd: timeStamp + elapsed,
domainLookup: timeStamp + elapsed,
domLoading: timeStamp + elapsed,
domInteractive: timeStamp + elapsed,
domContentLoadedEventEnd: timeStamp + elapsed,
requestStart: timeStamp + elapsed,
loadEventStart: timeStamp + elapsed,
styleLoadEventStart: timeStamp + elapsed,
cssRuleLoadEventStart: timeStamp + elapsed,
connectEndDelay: elapsed,
dnsStart: timeSt