Skip to content

Commit

Permalink
Expose estimatedServerTime as a configurable parameter (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasturiano committed Jul 4, 2023
1 parent e2bef2b commit c6a90a1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ new SpeedTest({ configOptions })
| <b>measureUploadLoadedLatency</b>: <i>boolean</i> | Whether to perform additional latency measurements simultaneously with upload requests, to measure loaded latency (during upload). | `true` |
| <b>loadedLatencyThrottle</b>: <i>number</i> | Time interval to wait in between loaded latency requests (in milliseconds). | 400 |
| <b>bandwidthFinishRequestDuration</b>: <i>number</i> | The minimum duration (in milliseconds) to reach in download/upload measurement sets for halting further measurements with larger file sizes in the same direction. | 1000 |
| <b>estimatedServerTime</b>: <i>number</i> | If the download/upload APIs do not return a server-timing response header containing the time spent in the server, this fixed value (in milliseconds) will be subtracted from all time-to-first-byte calculations. | 10 |
| <b>latencyPercentile</b>: <i>number</i> | The percentile (between 0 and 1) used to calculate latency from a set of measurements. | 0.5 |
| <b>bandwidthPercentile</b>: <i>number</i> | The percentile (between 0 and 1) used to calculate bandwidth from a set of measurements. | 0.9 |
| <b>bandwidthMinRequestDuration</b>: <i>number</i> | The minimum duration (in milliseconds) of a request to consider a measurement good enough to use in the bandwidth calculation. | 10 |
Expand Down
1 change: 1 addition & 0 deletions src/config/defaultConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default {
measureUploadLoadedLatency: true,
loadedLatencyThrottle: 400, // ms in between loaded latency requests
bandwidthFinishRequestDuration: 1000, // download/upload duration (ms) to reach for stopping further measurements
estimatedServerTime: 10, // ms to discount from latency calculation (if not present in response headers)

// Result interpretation
latencyPercentile: 0.5, // Percentile used to calculate latency from a set of measurements
Expand Down
12 changes: 9 additions & 3 deletions src/engines/BandwidthEngine/BandwidthEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import memoize from 'lodash.memoize';
const MAX_RETRIES = 20;

const ESTIMATED_HEADER_FRACTION = 0.005; // ~.5% of packet header / payload size. used when transferSize is not available.
const ESTIMATED_SERVER_TIME = 15; // ms to discount from latency calculation (if not present in response headers)

const cfGetServerTime = r => {
// extract server-timing from headers: server-timing: cfRequestDuration;dur=15.999794
Expand Down Expand Up @@ -48,7 +47,12 @@ const genContent = memoize(numBytes => '0'.repeat(numBytes));
class BandwidthMeasurementEngine {
constructor(
measurements,
{ downloadApiUrl, uploadApiUrl, throttleMs = 0 } = {}
{
downloadApiUrl,
uploadApiUrl,
throttleMs = 0,
estimatedServerTime = 0
} = {}
) {
if (!measurements) throw new Error('Missing measurements argument');
if (!downloadApiUrl) throw new Error('Missing downloadApiUrl argument');
Expand All @@ -58,6 +62,7 @@ class BandwidthMeasurementEngine {
this.#downloadApi = downloadApiUrl;
this.#uploadApi = uploadApiUrl;
this.#throttleMs = throttleMs;
this.#estimatedServerTime = Math.max(0, estimatedServerTime);
}

// Public attributes
Expand Down Expand Up @@ -138,6 +143,7 @@ class BandwidthMeasurementEngine {
#retries = 0;
#minDuration = -Infinity; // of current measurement
#throttleMs = 0;
#estimatedServerTime = 0;
#currentFetchPromise = undefined;
#currentNextMsmTimeoutId = undefined;

Expand Down Expand Up @@ -299,7 +305,7 @@ class BandwidthMeasurementEngine {
};
timing.ping = Math.max(
1e-2,
timing.ttfb - (serverTime || ESTIMATED_SERVER_TIME)
timing.ttfb - (serverTime || this.#estimatedServerTime)
); // ttfb = network latency + server time

timing.duration = (isDown ? calcDownloadDuration : calcUploadDuration)(
Expand Down
9 changes: 8 additions & 1 deletion src/engines/BandwidthEngine/ParallelLatency.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ class BandwidthWithParallelLatencyEngine extends BandwidthEngine {
parallelLatencyThrottleMs = 100,
downloadApiUrl,
uploadApiUrl,
estimatedServerTime = 0,
...ptProps
} = {}
) {
super(measurements, {
downloadApiUrl,
uploadApiUrl,
estimatedServerTime,
...ptProps
});

Expand All @@ -27,7 +29,12 @@ class BandwidthWithParallelLatencyEngine extends BandwidthEngine {
bypassMinDuration: true
}
],
{ downloadApiUrl, uploadApiUrl, throttleMs: parallelLatencyThrottleMs }
{
downloadApiUrl,
uploadApiUrl,
estimatedServerTime,
throttleMs: parallelLatencyThrottleMs
}
);
this.#latencyEngine.qsParams = {
during: `${measurements[0].dir}load`
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface ConfigOptions {
measureUploadLoadedLatency?: boolean,
loadedLatencyThrottle?: number,
bandwidthFinishRequestDuration?: number,
estimatedServerTime?: number;

// Result interpretation
latencyPercentile?: number,
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class MeasurementEngine {
const { type, ...msmConfig } = this.#config.measurements[this.#curMsmIdx];
const msmResults = this.#curTypeResults();

const { downloadApiUrl, uploadApiUrl } = this.#config;
const { downloadApiUrl, uploadApiUrl, estimatedServerTime } = this.#config;

let engine;
switch (type) {
Expand Down Expand Up @@ -292,6 +292,7 @@ class MeasurementEngine {
{
downloadApiUrl,
uploadApiUrl,
estimatedServerTime,
logApiUrl: this.#config.logMeasurementApiUrl,
measurementId: this.#measurementId,

Expand Down Expand Up @@ -347,6 +348,7 @@ class MeasurementEngine {
{
downloadApiUrl,
uploadApiUrl,
estimatedServerTime,
logApiUrl: this.#config.logMeasurementApiUrl,
measurementId: this.#measurementId,
measureParallelLatency,
Expand Down

0 comments on commit c6a90a1

Please sign in to comment.