This repository was archived by the owner on Apr 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtests-utils.js
More file actions
64 lines (46 loc) · 1.49 KB
/
tests-utils.js
File metadata and controls
64 lines (46 loc) · 1.49 KB
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
function makeRequest(url, done, minLatency, maxBandwidth) {
var loaded, total, doneTime;
console.log('url:', url);
var xhr = new XMLHttpRequest();
xhr.shaper.minLatency = minLatency;
xhr.shaper.maxBandwidth = maxBandwidth;
xhr.onreadystatechange = function(e) {
console.log('readyState: ' + xhr.readyState);
if (xhr.readyState === 4) {
doneTime = Date.now();
var duration = doneTime - reqTime;
var bitrate = Math.round(8 * total / duration);
console.log('Loaded ' + total + ' bytes in ' + duration + ' ms, computed bitrate: ' + bitrate + ' kbps');
}
};
xhr.onprogress = function(e) {
loaded = e.loaded;
total = e.total;
console.log('Progress: ' + e.loaded + ' of ' + e.total);
};
xhr.onload = function(e) {
if (xhr.readyState < 4) {
console.warn('onload called with readyState:', xhr.readyState, ' id:', xhr.id);
return;
}
console.log('Loading done');
//console.log(e);
console.log('readyState:', xhr.readyState);
if (xhr.response.byteLength < 1024) {
console.log(new TextDecoder("utf-8").decode(xhr.response));
}
done();
}
xhr.onloadend = function(e) {
console.log('Loading ended');
//console.log(e);
}
xhr.withCredentials = false;
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
console.debug('Max bandwidth: ' + xhr.shaper.maxBandwidth);
console.debug('Min latency: ' + xhr.shaper.minLatency);
var reqTime = Date.now();
xhr.send();
return xhr;
}