MCPcopy
hub / github.com/librespeed/speedtest / dlTest

Function dlTest

speedtest_worker.js:321–425  ·  view source on GitHub ↗
(done)

Source from the content-addressed store, hash-verified

319// download test, calls done function when it's over
320let dlCalled = false; // used to prevent multiple accidental calls to dlTest
321function dlTest(done) {
322 tverb("dlTest");
323 if (dlCalled) return;
324 else dlCalled = true; // dlTest already called?
325 let totLoaded = 0.0, // total number of loaded bytes
326 startT = new Date().getTime(), // timestamp when test was started
327 bonusT = 0, //how many milliseconds the test has been shortened by (higher on faster connections)
328 graceTimeDone = false, //set to true after the grace time is past
329 failed = false; // set to true if a stream fails
330 xhr = [];
331 // function to create a download stream. streams are slightly delayed so that they will not end at the same time
332 const testStream = function(i, delay) {
333 setTimeout(
334 function() {
335 if (testState !== 1) return; // delayed stream ended up starting after the end of the download test
336 tverb("dl test stream started " + i + " " + delay);
337 let prevLoaded = 0; // number of bytes loaded last time onprogress was called
338 let x = new XMLHttpRequest();
339 xhr[i] = x;
340 xhr[i].onprogress = function(event) {
341 tverb("dl stream progress event " + i + " " + event.loaded);
342 if (testState !== 1) {
343 try {
344 x.abort();
345 } catch (e) {}
346 } // just in case this XHR is still running after the download test
347 // progress event, add number of new loaded bytes to totLoaded
348 const loadDiff = event.loaded <= 0 ? 0 : event.loaded - prevLoaded;
349 if (isNaN(loadDiff) || !isFinite(loadDiff) || loadDiff < 0) return; // just in case
350 totLoaded += loadDiff;
351 prevLoaded = event.loaded;
352 }.bind(this);
353 xhr[i].onload = function() {
354 // the large file has been loaded entirely, start again
355 tverb("dl stream finished " + i);
356 try {
357 xhr[i].abort();
358 } catch (e) {} // reset the stream data to empty ram
359 testStream(i, 0);
360 }.bind(this);
361 xhr[i].onerror = function() {
362 // error
363 tverb("dl stream failed " + i);
364 if (settings.xhr_ignoreErrors === 0) failed = true; //abort
365 try {
366 xhr[i].abort();
367 } catch (e) {}
368 delete xhr[i];
369 if (settings.xhr_ignoreErrors === 1) testStream(i, 0); //restart stream
370 }.bind(this);
371 // send xhr
372 try {
373 if (settings.xhr_dlUseBlob) xhr[i].responseType = "blob";
374 else xhr[i].responseType = "arraybuffer";
375 } catch (e) {}
376 xhr[i].open("GET", settings.url_dl + url_sep(settings.url_dl) + (settings.mpot ? "cors=true&" : "") + "r=" + Math.random() + "&ckSize=" + settings.garbagePhp_chunkSize, true); // random string to prevent caching
377 xhr[i].send();
378 }.bind(this),

Callers 1

runNextTestFunction · 0.85

Calls 4

tverbFunction · 0.85
url_sepFunction · 0.85
clearRequestsFunction · 0.85
tlogFunction · 0.85

Tested by

no test coverage detected