From a7011b2e990e0fc9cf4641ee0abcbf298a38757c Mon Sep 17 00:00:00 2001 From: dosse91 Date: Thu, 22 Jun 2017 08:33:39 +0200 Subject: [PATCH] Removed unused Fetch API support (may come back in future); Removed quirk for Safari, now treated as chromium-based browser --- doc.md | 11 +----- speedtest_worker.js | 85 ++++++++++++++--------------------------- speedtest_worker.min.js | 2 +- 3 files changed, 30 insertions(+), 68 deletions(-) diff --git a/doc.md b/doc.md index 0ea708f..7f8de9e 100644 --- a/doc.md +++ b/doc.md @@ -1,7 +1,7 @@ # HTML5 Speedtest > by Federico Dossena -> Version 4.2.6, June 19 2017 +> Version 4.2.7, June 22 2017 > [https://github.com/adolfintel/speedtest/](https://github.com/adolfintel/speedtest/) @@ -168,7 +168,6 @@ w.postMessage('start {"param1": "value1", "param2": "value2", ...}') * Default: `3` * Recommended: `>=1` * Default override: 1 on Firefox if enable_quirks is true - * Default override: 10 on Safari if enable_quirks is true * __xhr_ignoreErrors__: how to react to errors in download/upload streams and the ping test * `0`: Fail test on error (behaviour of previous versions of this test) * `1`: Restart a stream/ping when it fails @@ -181,14 +180,6 @@ w.postMessage('start {"param1": "value1", "param2": "value2", ...}') * __time_ulGraceTime__: How long to wait (in seconds) before actually measuring the upload speed. This is a good idea because we want to wait for the buffers to be full (avoids the peak at the beginning of the test) * Default: `3` * Recommended: `>=1` -* __allow_fetchAPI__: allow the use of Fetch API for the download test instead of regular XHR. Experimental, not recommended. - * Default: `false` -* __force_fetchAPI__: forces the use of Fetch API on all browsers that support it - * Default: `false` -Fetch API are used if the following conditions are met: - * allow_fetchAPI is true - * Chromium-based browser with support for Fetch API and enable_quirks is true -OR force_fetchAPI is true and the browser supports Fetch API * __overheadCompensationFactor__: compensation for HTTP and network overhead. Default value assumes typical MTUs used over the Internet. You might want to change this if you're using this in your internal network with different MTUs, or if you're using IPv6 instead of IPv4. * Default: `1.13359567567567567568` (1048576/925000) assumes HTTP+TCP+IPv4+ETH with typical MTUs used over the Internet * `1.0513`: HTTP+TCP+IPv6+ETH, over the Internet (empirically tested, not calculated) diff --git a/speedtest_worker.js b/speedtest_worker.js index 5183caf..86bb043 100644 --- a/speedtest_worker.js +++ b/speedtest_worker.js @@ -1,5 +1,5 @@ /* - HTML5 Speedtest v4.2.5 + HTML5 Speedtest v4.2.7 by Federico Dossena https://github.com/adolfintel/speedtest/ GNU LGPLv3 License @@ -30,8 +30,6 @@ var settings = { xhr_dlUseBlob: false, // if set to true, it reduces ram usage but uses the hard drive (useful with large garbagePhp_chunkSize and/or high xhr_dlMultistream) garbagePhp_chunkSize: 20, // size of chunks sent by garbage.php (can be different if enable_quirks is active) enable_quirks: true, // enable quirks for specific browsers. currently it overrides settings to optimize for specific browsers, unless they are already being overridden with the start command - allow_fetchAPI: false, // enables Fetch API. currently disabled because it leaks memory like no tomorrow - force_fetchAPI: false, // when Fetch API is enabled, it will force usage on every browser that supports it overheadCompensationFactor: 1048576/925000 //compensation for HTTP+TCP+IP+ETH overhead. 925000 is how much data is actually carried over 1048576 (1mb) bytes downloaded/uploaded. This default value assumes HTTP+TCP+IPv4+ETH with typical MTUs over the Internet. You may want to change this if you're going through your local network with a different MTU or if you're going over IPv6 (see doc.md for some other values) } @@ -80,7 +78,6 @@ this.addEventListener('message', function (e) { if (typeof s.time_dl !== 'undefined') settings.time_dl = s.time_dl // duration of download test if (typeof s.time_ul !== 'undefined') settings.time_ul = s.time_ul // duration of upload test if (typeof s.enable_quirks !== 'undefined') settings.enable_quirks = s.enable_quirks // enable quirks or not - if (typeof s.allow_fetchAPI !== 'undefined') settings.allow_fetchAPI = s.allow_fetchAPI // allows fetch api to be used if supported // quirks for specific browsers. more may be added in future releases if (settings.enable_quirks) { var ua = navigator.userAgent @@ -92,14 +89,7 @@ this.addEventListener('message', function (e) { // edge more precise with 3 download streams settings.xhr_dlMultistream = 3 } - if ((/Safari.(\d+)/i.test(ua)) && !(/Chrome.(\d+)/i.test(ua))) { - // safari more precise with 10 upload streams and 5mb chunks for download test - settings.xhr_ulMultistream = 10 - settings.garbagePhp_chunkSize = 5 - } if (/Chrome.(\d+)/i.test(ua) && (!!self.fetch)) { - // chrome can't handle large xhr very well, use fetch api if available and allowed - if (settings.allow_fetchAPI) useFetchAPI = true // chrome more precise with 5 streams settings.xhr_dlMultistream = 5 } @@ -110,15 +100,12 @@ this.addEventListener('message', function (e) { if (typeof s.xhr_ignoreErrors !== 'undefined') settings.xhr_ignoreErrors = s.xhr_ignoreErrors // what to do in case of errors during the test if (typeof s.xhr_dlUseBlob !== 'undefined') settings.xhr_dlUseBlob = s.xhr_dlUseBlob // use blob for download test if (typeof s.garbagePhp_chunkSize !== 'undefined') settings.garbagePhp_chunkSize = s.garbagePhp_chunkSize // size of garbage.php chunks - if (typeof s.force_fetchAPI !== 'undefined') settings.force_fetchAPI = s.force_fetchAPI // use fetch api on all browsers that support it if enabled if (typeof s.time_dlGraceTime !== 'undefined') settings.time_dlGraceTime = s.time_dlGraceTime // dl test grace time before measuring if (typeof s.time_ulGraceTime !== 'undefined') settings.time_ulGraceTime = s.time_ulGraceTime // ul test grace time before measuring if (typeof s.overheadCompensationFactor !== 'undefined') settings.overheadCompensationFactor = s.overheadCompensationFactor //custom overhead compensation factor (default assumes HTTP+TCP+IP+ETH with typical MTUs) - if (settings.allow_fetchAPI && settings.force_fetchAPI && (!!self.fetch)) useFetchAPI = true } catch (e) { console.warn("Possible error in custom test settings. Some settings may not be applied. Exception: "+e) } // run the tests console.log(settings) - console.log('Fetch API: ' + useFetchAPI) getIp(function () { dlTest(function () { testStatus = 2; pingTest(function () { testStatus = 3; ulTest(function () { testStatus = 4 }) }) }) }) } if (params[0] === 'abort') { // abort command @@ -166,49 +153,33 @@ function dlTest (done) { var testStream = function (i, delay) { setTimeout(function () { if (testStatus !== 1) return // delayed stream ended up starting after the end of the download test - if (useFetchAPI) { - xhr[i] = fetch(settings.url_dl + url_sep(settings.url_dl) + 'r=' + Math.random() + '&ckSize=' + settings.garbagePhp_chunkSize).then(function (response) { - var reader = response.body.getReader() - var consume = function () { - return reader.read().then(function (result) { - if (result.done) testStream(i); else { - totLoaded += result.value.length - if (xhr[i].cancelRequested) reader.cancel() - } - return consume() - }.bind(this)) - }.bind(this) - return consume() - }.bind(this)) - } else { - var prevLoaded = 0 // number of bytes loaded last time onprogress was called - var x = new XMLHttpRequest() - xhr[i] = x - xhr[i].onprogress = function (event) { - if (testStatus !== 1) { try { x.abort() } catch (e) { } } // just in case this XHR is still running after the download test - // progress event, add number of new loaded bytes to totLoaded - var loadDiff = event.loaded <= 0 ? 0 : (event.loaded - prevLoaded) - if (isNaN(loadDiff) || !isFinite(loadDiff) || loadDiff < 0) return // just in case - totLoaded += loadDiff - prevLoaded = event.loaded - }.bind(this) - xhr[i].onload = function () { - // the large file has been loaded entirely, start again - try { xhr[i].abort() } catch (e) { } // reset the stream data to empty ram - testStream(i, 0) - }.bind(this) - xhr[i].onerror = function () { - // error - if (settings.xhr_ignoreErrors === 0) failed=true //abort - try { xhr[i].abort() } catch (e) { } - delete (xhr[i]) - if (settings.xhr_ignoreErrors === 1) testStream(i, 100) //restart stream after 100ms - }.bind(this) - // send xhr - try { if (settings.xhr_dlUseBlob) xhr[i].responseType = 'blob'; else xhr[i].responseType = 'arraybuffer' } catch (e) { } - xhr[i].open('GET', settings.url_dl + url_sep(settings.url_dl) + 'r=' + Math.random() + '&ckSize=' + settings.garbagePhp_chunkSize, true) // random string to prevent caching - xhr[i].send() - } + var prevLoaded = 0 // number of bytes loaded last time onprogress was called + var x = new XMLHttpRequest() + xhr[i] = x + xhr[i].onprogress = function (event) { + if (testStatus !== 1) { try { x.abort() } catch (e) { } } // just in case this XHR is still running after the download test + // progress event, add number of new loaded bytes to totLoaded + var loadDiff = event.loaded <= 0 ? 0 : (event.loaded - prevLoaded) + if (isNaN(loadDiff) || !isFinite(loadDiff) || loadDiff < 0) return // just in case + totLoaded += loadDiff + prevLoaded = event.loaded + }.bind(this) + xhr[i].onload = function () { + // the large file has been loaded entirely, start again + try { xhr[i].abort() } catch (e) { } // reset the stream data to empty ram + testStream(i, 0) + }.bind(this) + xhr[i].onerror = function () { + // error + if (settings.xhr_ignoreErrors === 0) failed=true //abort + try { xhr[i].abort() } catch (e) { } + delete (xhr[i]) + if (settings.xhr_ignoreErrors === 1) testStream(i, 100) //restart stream after 100ms + }.bind(this) + // send xhr + try { if (settings.xhr_dlUseBlob) xhr[i].responseType = 'blob'; else xhr[i].responseType = 'arraybuffer' } catch (e) { } + xhr[i].open('GET', settings.url_dl + url_sep(settings.url_dl) + 'r=' + Math.random() + '&ckSize=' + settings.garbagePhp_chunkSize, true) // random string to prevent caching + xhr[i].send() }.bind(this), 1 + delay) }.bind(this) // open streams diff --git a/speedtest_worker.min.js b/speedtest_worker.min.js index 5fccd3d..7223949 100644 --- a/speedtest_worker.min.js +++ b/speedtest_worker.min.js @@ -1 +1 @@ -function url_sep(url){return url.match(/\?/)?"&":"?"}function clearRequests(){if(xhr){for(var i=0;iloadDiff||(totLoaded+=loadDiff,prevLoaded=event.loaded)}.bind(this),xhr[i].onload=function(){try{xhr[i].abort()}catch(e){}testStream(i,0)}.bind(this),xhr[i].onerror=function(){0===settings.xhr_ignoreErrors&&(failed=!0);try{xhr[i].abort()}catch(e){}delete xhr[i],1===settings.xhr_ignoreErrors&&testStream(i,100)}.bind(this);try{settings.xhr_dlUseBlob?xhr[i].responseType="blob":xhr[i].responseType="arraybuffer"}catch(e){}xhr[i].open("GET",settings.url_dl+url_sep(settings.url_dl)+"r="+Math.random()+"&ckSize="+settings.garbagePhp_chunkSize,!0),xhr[i].send()}}.bind(this),1+delay)}.bind(this),i=0;it))if(graceTimeDone){var speed=totLoaded/(t/1e3);dlStatus=(8*speed*settings.overheadCompensationFactor/1048576).toFixed(2),(t/1e3>settings.time_dl||failed)&&((failed||isNaN(dlStatus))&&(dlStatus="Fail"),clearRequests(),clearInterval(interval),done())}else t>1e3*settings.time_dlGraceTime&&(totLoaded>0&&(startT=(new Date).getTime(),totLoaded=0),graceTimeDone=!0)}.bind(this),200)}}function ulTest(done){if(!ulCalled){ulCalled=!0;var totLoaded=0,startT=(new Date).getTime(),graceTimeDone=!1,failed=!1;xhr=[];for(var testStream=function(i,delay){setTimeout(function(){if(3===testStatus){var prevLoaded=0,x=new XMLHttpRequest;xhr[i]=x;var ie11workaround;try{xhr[i].upload.onprogress,ie11workaround=!1}catch(e){ie11workaround=!0}ie11workaround?(xhr[i].onload=function(){totLoaded+=262144,testStream(i,0)},xhr[i].onerror=function(){0===settings.xhr_ignoreErrors&&(failed=!0);try{xhr[i].abort()}catch(e){}delete xhr[i],1===settings.xhr_ignoreErrors&&testStream(i,100)},xhr[i].open("POST",settings.url_ul+url_sep(settings.url_ul)+"r="+Math.random(),!0),xhr[i].setRequestHeader("Content-Encoding","identity"),xhr[i].send(reqsmall)):(xhr[i].upload.onprogress=function(event){if(3!==testStatus)try{x.abort()}catch(e){}var loadDiff=event.loaded<=0?0:event.loaded-prevLoaded;isNaN(loadDiff)||!isFinite(loadDiff)||0>loadDiff||(totLoaded+=loadDiff,prevLoaded=event.loaded)}.bind(this),xhr[i].upload.onload=function(){testStream(i,0)}.bind(this),xhr[i].upload.onerror=function(){0===settings.xhr_ignoreErrors&&(failed=!0);try{xhr[i].abort()}catch(e){}delete xhr[i],1===settings.xhr_ignoreErrors&&testStream(i,100)}.bind(this),xhr[i].open("POST",settings.url_ul+url_sep(settings.url_ul)+"r="+Math.random(),!0),xhr[i].setRequestHeader("Content-Encoding","identity"),xhr[i].send(req))}}.bind(this),1)}.bind(this),i=0;it))if(graceTimeDone){var speed=totLoaded/(t/1e3);ulStatus=(8*speed*settings.overheadCompensationFactor/1048576).toFixed(2),(t/1e3>settings.time_ul||failed)&&((failed||isNaN(ulStatus))&&(ulStatus="Fail"),clearRequests(),clearInterval(interval),done())}else t>1e3*settings.time_ulGraceTime&&(totLoaded>0&&(startT=(new Date).getTime(),totLoaded=0),graceTimeDone=!0)}.bind(this),200)}}function pingTest(done){if(!ptCalled){ptCalled=!0;var prevT=null,ping=0,jitter=0,i=0,prevInstspd=0;xhr=[];var doPing=function(){prevT=(new Date).getTime(),xhr[0]=new XMLHttpRequest,xhr[0].onload=function(){if(0===i)prevT=(new Date).getTime();else{var instspd=(new Date).getTime()-prevT,instjitter=Math.abs(instspd-prevInstspd);1===i?ping=instspd:(ping=.9*ping+.1*instspd,jitter=instjitter>jitter?.2*jitter+.8*instjitter:.9*jitter+.1*instjitter),prevInstspd=instspd}pingStatus=ping.toFixed(2),jitterStatus=jitter.toFixed(2),i++,ii;i++)req.push(r);req=new Blob(req),r=new ArrayBuffer(262144);try{r=new Float32Array(r);for(var i=0;iloadDiff||(totLoaded+=loadDiff,prevLoaded=event.loaded)}.bind(this),xhr[i].onload=function(){try{xhr[i].abort()}catch(e){}testStream(i,0)}.bind(this),xhr[i].onerror=function(){0===settings.xhr_ignoreErrors&&(failed=!0);try{xhr[i].abort()}catch(e){}delete xhr[i],1===settings.xhr_ignoreErrors&&testStream(i,100)}.bind(this);try{settings.xhr_dlUseBlob?xhr[i].responseType="blob":xhr[i].responseType="arraybuffer"}catch(e){}xhr[i].open("GET",settings.url_dl+url_sep(settings.url_dl)+"r="+Math.random()+"&ckSize="+settings.garbagePhp_chunkSize,!0),xhr[i].send()}}.bind(this),1+delay)}.bind(this),i=0;it))if(graceTimeDone){var speed=totLoaded/(t/1e3);dlStatus=(8*speed*settings.overheadCompensationFactor/1048576).toFixed(2),(t/1e3>settings.time_dl||failed)&&((failed||isNaN(dlStatus))&&(dlStatus="Fail"),clearRequests(),clearInterval(interval),done())}else t>1e3*settings.time_dlGraceTime&&(totLoaded>0&&(startT=(new Date).getTime(),totLoaded=0),graceTimeDone=!0)}.bind(this),200)}}function ulTest(done){if(!ulCalled){ulCalled=!0;var totLoaded=0,startT=(new Date).getTime(),graceTimeDone=!1,failed=!1;xhr=[];for(var testStream=function(i,delay){setTimeout(function(){if(3===testStatus){var prevLoaded=0,x=new XMLHttpRequest;xhr[i]=x;var ie11workaround;try{xhr[i].upload.onprogress,ie11workaround=!1}catch(e){ie11workaround=!0}ie11workaround?(xhr[i].onload=function(){totLoaded+=262144,testStream(i,0)},xhr[i].onerror=function(){0===settings.xhr_ignoreErrors&&(failed=!0);try{xhr[i].abort()}catch(e){}delete xhr[i],1===settings.xhr_ignoreErrors&&testStream(i,100)},xhr[i].open("POST",settings.url_ul+url_sep(settings.url_ul)+"r="+Math.random(),!0),xhr[i].setRequestHeader("Content-Encoding","identity"),xhr[i].send(reqsmall)):(xhr[i].upload.onprogress=function(event){if(3!==testStatus)try{x.abort()}catch(e){}var loadDiff=event.loaded<=0?0:event.loaded-prevLoaded;isNaN(loadDiff)||!isFinite(loadDiff)||0>loadDiff||(totLoaded+=loadDiff,prevLoaded=event.loaded)}.bind(this),xhr[i].upload.onload=function(){testStream(i,0)}.bind(this),xhr[i].upload.onerror=function(){0===settings.xhr_ignoreErrors&&(failed=!0);try{xhr[i].abort()}catch(e){}delete xhr[i],1===settings.xhr_ignoreErrors&&testStream(i,100)}.bind(this),xhr[i].open("POST",settings.url_ul+url_sep(settings.url_ul)+"r="+Math.random(),!0),xhr[i].setRequestHeader("Content-Encoding","identity"),xhr[i].send(req))}}.bind(this),1)}.bind(this),i=0;it))if(graceTimeDone){var speed=totLoaded/(t/1e3);ulStatus=(8*speed*settings.overheadCompensationFactor/1048576).toFixed(2),(t/1e3>settings.time_ul||failed)&&((failed||isNaN(ulStatus))&&(ulStatus="Fail"),clearRequests(),clearInterval(interval),done())}else t>1e3*settings.time_ulGraceTime&&(totLoaded>0&&(startT=(new Date).getTime(),totLoaded=0),graceTimeDone=!0)}.bind(this),200)}}function pingTest(done){if(!ptCalled){ptCalled=!0;var prevT=null,ping=0,jitter=0,i=0,prevInstspd=0;xhr=[];var doPing=function(){prevT=(new Date).getTime(),xhr[0]=new XMLHttpRequest,xhr[0].onload=function(){if(0===i)prevT=(new Date).getTime();else{var instspd=(new Date).getTime()-prevT,instjitter=Math.abs(instspd-prevInstspd);1===i?ping=instspd:(ping=.9*ping+.1*instspd,jitter=instjitter>jitter?.2*jitter+.8*instjitter:.9*jitter+.1*instjitter),prevInstspd=instspd}pingStatus=ping.toFixed(2),jitterStatus=jitter.toFixed(2),i++,ii;i++)req.push(r);req=new Blob(req),r=new ArrayBuffer(262144);try{r=new Float32Array(r);for(var i=0;i