-
Notifications
You must be signed in to change notification settings - Fork 34
/
BandwidthEngine.js
370 lines (314 loc) · 10.5 KB
/
BandwidthEngine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import 'isomorphic-fetch';
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
const serverTiming = r.headers.get(`server-timing`);
if (serverTiming) {
const re = serverTiming.match(/dur=([0-9.]+)/);
if (re) return +re[1];
}
};
const getTtfb = perf => perf.responseStart - perf.requestStart;
const gePayloadDownload = perf => perf.responseEnd - perf.responseStart; // min 1ms
const calcDownloadDuration = ({ ping, payloadDownloadTime }) =>
ping + payloadDownloadTime; // request duration excluding server time
const calcUploadDuration = ({ ttfb }) => ttfb;
const calcDownloadSpeed = ({ duration, transferSize }, numBytes) => {
// use transferSize if available. if estimating from numBytes, add ~0.5% of headers.
const bits =
8 * (transferSize || +numBytes * (1 + ESTIMATED_HEADER_FRACTION));
const secs = duration / 1000;
return !secs ? undefined : bits / secs;
};
const calcUploadSpeed = ({ duration }, numBytes) => {
const bits = 8 * numBytes * (1 + ESTIMATED_HEADER_FRACTION); // take into account estimated packet headers
const secs = duration / 1000; // subtract estimated server time
return !secs ? undefined : bits / secs;
};
const genContent = memoize(numBytes => '0'.repeat(numBytes));
//
class BandwidthMeasurementEngine {
constructor(
measurements,
{ downloadApiUrl, uploadApiUrl, throttleMs = 0 } = {}
) {
if (!measurements) throw new Error('Missing measurements argument');
if (!downloadApiUrl) throw new Error('Missing downloadApiUrl argument');
if (!uploadApiUrl) throw new Error('Missing uploadApiUrl argument');
this.#measurements = measurements;
this.#downloadApi = downloadApiUrl;
this.#uploadApi = uploadApiUrl;
this.#throttleMs = throttleMs;
}
// Public attributes
get results() {
// read access to results
return this.#results;
}
#qsParams = {}; // additional query string params to include in the requests
get qsParams() {
return this.#qsParams;
}
set qsParams(v) {
this.#qsParams = v;
}
#fetchOptions = {}; // additional options included in the requests
get fetchOptions() {
return this.#fetchOptions;
}
set fetchOptions(v) {
this.#fetchOptions = v;
}
finishRequestDuration = 1000; // download/upload duration (ms) to reach for stopping further measurements
getServerTime = cfGetServerTime; // method to extract server time from response
#responseHook = r => r; // pipe-through of response objects
set responseHook(f) {
this.#responseHook = f;
}
#onRunningChange = () => {}; // callback invoked when engine starts/stops
set onRunningChange(f) {
this.#onRunningChange = f;
}
#onNewMeasurementStarted = () => {}; // callback invoked when a new item in the measurement list is started
set onNewMeasurementStarted(f) {
this.#onNewMeasurementStarted = f;
}
#onMeasurementResult = () => {}; // callback invoked when a new measurement result arrives
set onMeasurementResult(f) {
this.#onMeasurementResult = f;
}
#onFinished = () => {}; // callback invoked when all the measurements are finished
set onFinished(f) {
this.#onFinished = f;
}
#onConnectionError = () => {}; // Invoked when unable to get a response from the API
set onConnectionError(f) {
this.#onConnectionError = f;
}
// Public methods
pause() {
clearTimeout(this.#currentNextMsmTimeoutId);
this.#cancelCurrentMeasurement();
this.#setRunning(false);
}
play() {
if (!this.#running) {
this.#setRunning(true);
this.#nextMeasurement();
}
}
// Internal state
#measurements;
#downloadApi;
#uploadApi;
#running = false;
#finished = { down: false, up: false };
#results = { down: {}, up: {} };
#measIdx = 0;
#counter = 0;
#retries = 0;
#minDuration = -Infinity; // of current measurement
#throttleMs = 0;
#currentFetchPromise = undefined;
#currentNextMsmTimeoutId = undefined;
// Internal methods
#setRunning(running) {
if (running !== this.#running) {
this.#running = running;
setTimeout(() => this.#onRunningChange(this.#running));
}
}
#saveMeasurementResults(measIdx, measTiming) {
const { bytes, dir } = this.#measurements[measIdx];
const results = this.#results;
const bytesResult = results[dir].hasOwnProperty(bytes)
? results[dir][bytes]
: {
timings: [],
// count all measurements with same bytes and direction
numMeasurements: this.#measurements
.filter(({ bytes: b, dir: d }) => bytes === b && dir === d)
.map(m => m.count)
.reduce((agg, cnt) => agg + cnt, 0)
};
!!measTiming && bytesResult.timings.push(measTiming);
bytesResult.timings = bytesResult.timings.slice(
-bytesResult.numMeasurements
);
results[dir][bytes] = bytesResult;
if (measTiming) {
setTimeout(() => {
this.#onMeasurementResult(
{
type: dir,
bytes,
...measTiming
},
results
);
});
} else {
this.#onNewMeasurementStarted(this.#measurements[measIdx], results);
}
}
#nextMeasurement() {
const measurements = this.#measurements;
let meas = measurements[this.#measIdx];
if (this.#counter >= meas.count) {
// Finished current measurement
const finished = this.#finished;
if (
this.#minDuration > this.finishRequestDuration &&
!meas.bypassMinDuration
) {
// mark direction as finished
const dir = meas.dir;
this.#finished[dir] = true;
Object.values(this.#finished).every(finished => finished) &&
this.#onFinished(this.#results);
}
// clear settings
this.#counter = 0;
this.#minDuration = -Infinity;
performance.clearResourceTimings();
do {
this.#measIdx += 1; // skip through finished measurements
} while (
this.#measIdx < measurements.length &&
finished[measurements[this.#measIdx].dir]
);
if (this.#measIdx >= measurements.length) {
// reached the end: halt further measurements
this.#finished = { down: true, up: true };
this.#setRunning(false);
this.#onFinished(this.#results);
return;
}
meas = measurements[this.#measIdx];
}
const measIdx = this.#measIdx;
if (this.#counter === 0) {
this.#saveMeasurementResults(measIdx); // register measurement start
}
const { bytes: numBytes, dir } = meas;
const isDown = dir === 'down';
const apiUrl = isDown ? this.#downloadApi : this.#uploadApi;
const qsParams = Object.assign({}, this.#qsParams);
isDown && (qsParams.bytes = `${numBytes}`);
const url = `${
apiUrl.startsWith('http') || apiUrl.startsWith('//')
? ''
: window.location.origin // use abs to match perf timing urls
}${apiUrl}?${Object.entries(qsParams)
.map(([k, v]) => `${k}=${v}`)
.join('&')}`;
const fetchOpt = Object.assign(
{},
isDown
? {}
: {
method: 'POST',
body: genContent(numBytes)
},
this.#fetchOptions
);
let serverTime;
const curPromise = (this.#currentFetchPromise = fetch(url, fetchOpt) // eslint-disable-line compat/compat
.then(r => {
if (r.ok) return r;
throw Error(r.statusText);
})
.then(r => {
this.getServerTime && (serverTime = this.getServerTime(r));
return r;
})
.then(r =>
r.text().then(body => {
this.#responseHook &&
this.#responseHook({
url,
headers: r.headers,
body
});
return body;
})
)
.then((_, reject) => {
if (curPromise._cancel) {
reject('cancelled');
return;
}
const perf = performance.getEntriesByName(url).slice(-1)[0]; // get latest perf timing
const timing = {
transferSize: perf.transferSize,
ttfb: getTtfb(perf),
payloadDownloadTime: gePayloadDownload(perf),
serverTime: serverTime || -1,
measTime: new Date()
};
timing.ping = Math.max(
1e-2,
timing.ttfb - (serverTime || ESTIMATED_SERVER_TIME)
); // ttfb = network latency + server time
timing.duration = (isDown ? calcDownloadDuration : calcUploadDuration)(
timing
);
timing.bps = (isDown ? calcDownloadSpeed : calcUploadSpeed)(
timing,
numBytes
);
if (isDown && numBytes) {
const reqSize = +numBytes;
if (
timing.transferSize &&
(timing.transferSize < reqSize ||
timing.transferSize / reqSize > 1.05)
) {
// log if transferSize is too different from requested size
console.warn(
`Requested ${reqSize}B but received ${timing.transferSize}B (${
Math.round((timing.transferSize / reqSize) * 1e4) / 1e2
}%).`
);
}
}
this.#saveMeasurementResults(measIdx, timing);
const requestDuration = timing.duration;
this.#minDuration =
this.#minDuration < 0
? requestDuration
: Math.min(this.#minDuration, requestDuration); // carry minimum request duration
this.#counter += 1;
this.#retries = 0;
if (this.#throttleMs) {
this.#currentNextMsmTimeoutId = setTimeout(
() => this.#nextMeasurement(),
this.#throttleMs
);
} else {
this.#nextMeasurement();
}
})
.catch(error => {
if (curPromise._cancel) return;
console.warn(`Error fetching ${url}: ${error}`);
if (this.#retries++ < MAX_RETRIES) {
this.#nextMeasurement(); // keep trying
} else {
this.#retries = 0;
this.#setRunning(false);
this.#onConnectionError(
`Connection failed to ${url}. Gave up after ${MAX_RETRIES} retries.`
);
}
}));
}
#cancelCurrentMeasurement() {
const curPromise = this.#currentFetchPromise;
curPromise && (curPromise._cancel = true);
}
}
export default BandwidthMeasurementEngine;