-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathstability_worker.js
More file actions
245 lines (221 loc) · 6.73 KB
/
stability_worker.js
File metadata and controls
245 lines (221 loc) · 6.73 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
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
/*
LibreSpeed - Stability Test Worker
https://github.com/librespeed/speedtest/
GNU LGPLv3 License
*/
// data reported to main thread
let testState = -1; // -1=idle, 0=starting, 1=running, 4=finished, 5=aborted
let currentPing = 0;
let avgPing = 0;
let minPing = -1;
let maxPing = 0;
let jitter = 0;
let packetLoss = 0; // failed request percentage
let elapsed = 0;
let progress = 0;
let pingData = []; // all ping data points {t: elapsedMs, ping: ms, lost: bool}
let lastReportedIndex = 0; // for delta delivery
let totalSamples = 0;
let failedSamples = 0;
let pingSum = 0;
let settings = {
url_ping: "backend/empty.php",
url_ping_external: "", // external URL to ping (uses fetch no-cors, e.g. "https://www.google.com/generate_204")
duration: 60, // seconds
ping_interval: 200, // minimum ms between pings to limit sample rate
ping_timeout: 5000,
ping_allowPerformanceApi: true,
mpot: false
};
let xhr = null;
let startTime = 0;
let prevInstspd = 0;
let aborted = false;
function url_sep(url) {
return url.match(/\?/) ? "&" : "?";
}
this.addEventListener("message", function (e) {
const params = e.data.split(" ");
if (params[0] === "status") {
// return current state with delta ping data
const newData = pingData.slice(lastReportedIndex);
lastReportedIndex = pingData.length;
postMessage(
JSON.stringify({
testState: testState,
currentPing: currentPing,
avgPing: avgPing,
minPing: minPing,
maxPing: maxPing,
jitter: jitter,
packetLoss: packetLoss,
elapsed: elapsed,
duration: settings.duration,
progress: progress,
pingData: newData,
totalSamples: totalSamples,
failedSamples: failedSamples
})
);
}
if (params[0] === "start" && testState === -1) {
testState = 0;
try {
let s = {};
try {
const ss = e.data.substring(6);
if (ss) s = JSON.parse(ss);
} catch (e) {
console.warn("Error parsing settings JSON");
}
for (let key in s) {
if (typeof settings[key] !== "undefined") settings[key] = s[key];
}
} catch (e) {
console.warn("Error applying settings: " + e);
}
// start the stability test
aborted = false;
startTime = new Date().getTime();
testState = 1;
doPing();
}
if (params[0] === "abort") {
if (testState >= 4) return;
aborted = true;
testState = 5;
if (xhr) {
try {
xhr.abort();
} catch (e) {}
}
}
});
function recordPing(instspd) {
// guard against 0ms pings
if (instspd < 1) instspd = prevInstspd;
if (instspd < 1) instspd = 1;
totalSamples++;
currentPing = instspd;
pingSum += instspd;
avgPing = parseFloat((pingSum / (totalSamples - failedSamples)).toFixed(2));
if (minPing === -1 || instspd < minPing) minPing = instspd;
if (instspd > maxPing) maxPing = instspd;
// jitter calculation (same weighted formula as speedtest_worker.js)
if (totalSamples > 1 && prevInstspd > 0) {
const instjitter = Math.abs(instspd - prevInstspd);
if (totalSamples === 2) {
jitter = instjitter;
} else {
jitter = instjitter > jitter ? jitter * 0.3 + instjitter * 0.7 : jitter * 0.8 + instjitter * 0.2;
}
}
prevInstspd = instspd;
// failed request percentage
packetLoss = totalSamples > 0 ? parseFloat(((failedSamples / totalSamples) * 100).toFixed(2)) : 0;
// record data point
const now = new Date().getTime();
elapsed = (now - startTime) / 1000;
pingData.push({ t: elapsed, ping: parseFloat(instspd.toFixed(2)), lost: false });
}
function recordLoss() {
const now = new Date().getTime();
totalSamples++;
failedSamples++;
packetLoss = parseFloat(((failedSamples / totalSamples) * 100).toFixed(2));
elapsed = (now - startTime) / 1000;
pingData.push({ t: elapsed, ping: 0, lost: true });
}
// pace pings to avoid excessive sample rates on low-latency links
function schedulePing(rtt) {
const delay = Math.max(0, settings.ping_interval - rtt);
if (delay > 0) {
setTimeout(doPing, delay);
} else {
doPing();
}
}
function doPing() {
if (aborted || testState >= 4) return;
// check if duration exceeded
const now = new Date().getTime();
elapsed = (now - startTime) / 1000;
progress = Math.min(1, elapsed / settings.duration);
if (elapsed >= settings.duration) {
testState = 4;
progress = 1;
return;
}
// external ping mode: use fetch with no-cors
if (settings.url_ping_external) {
doPingExternal();
return;
}
const prevT = new Date().getTime();
xhr = new XMLHttpRequest();
xhr.onload = function () {
if (aborted || testState >= 4) return;
const now = new Date().getTime();
let instspd = now - prevT;
if (settings.ping_allowPerformanceApi) {
try {
let p = performance.getEntries();
p = p[p.length - 1];
let d = p.responseStart - p.requestStart;
if (d <= 0) d = p.duration;
if (d > 0 && d < instspd) instspd = d;
} catch (e) {
// Performance API not available, use estimate
}
}
recordPing(instspd);
schedulePing(instspd);
};
xhr.onerror = function () {
if (aborted || testState >= 4) return;
recordLoss();
schedulePing(0);
};
xhr.ontimeout = xhr.onerror;
xhr.open(
"GET",
settings.url_ping + url_sep(settings.url_ping) + (settings.mpot ? "cors=true&" : "") + "r=" + Math.random(),
true
);
try {
xhr.timeout = settings.ping_timeout;
} catch (e) {}
xhr.send();
}
// ping an external host using fetch with no-cors (opaque response, but timing still works)
function doPingExternal() {
const prevT = new Date().getTime();
const remainingMs = Math.max(1, settings.duration * 1000 - (prevT - startTime));
const timeoutMs = Math.min(settings.ping_timeout, remainingMs);
const url =
settings.url_ping_external + (settings.url_ping_external.indexOf("?") >= 0 ? "&" : "?") + "r=" + Math.random();
let timeoutId = null;
const controller = typeof AbortController !== "undefined" ? new AbortController() : null;
const fetchOptions = { mode: "no-cors", cache: "no-store" };
if (controller) fetchOptions.signal = controller.signal;
const timeout = new Promise(function (_, reject) {
timeoutId = setTimeout(function () {
if (controller) controller.abort();
reject(new Error("timeout"));
}, timeoutMs);
});
Promise.race([fetch(url, fetchOptions), timeout])
.then(function () {
if (timeoutId) clearTimeout(timeoutId);
if (aborted || testState >= 4) return;
const instspd = new Date().getTime() - prevT;
recordPing(instspd);
schedulePing(instspd);
})
.catch(function () {
if (timeoutId) clearTimeout(timeoutId);
if (aborted || testState >= 4) return;
recordLoss();
schedulePing(0);
});
}