From f1bf2ce2aea1d26638ae9c05483af145758cafc7 Mon Sep 17 00:00:00 2001 From: dosse91 Date: Mon, 2 Jan 2017 07:10:44 +0100 Subject: [PATCH] Improved accuracy; Minor changes and fixes --- speedtest_worker.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/speedtest_worker.js b/speedtest_worker.js index f6f30f5..50509fe 100644 --- a/speedtest_worker.js +++ b/speedtest_worker.js @@ -28,7 +28,7 @@ function dlTest(done){ var firstTick=true,startT=new Date().getTime(), prevT=new Date().getTime(),prevLoaded=0,speed=0.0; xhr=new XMLHttpRequest(); xhr.onprogress=function(event){ - var instspd=(event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0); + var instspd=event.loaded<=0?prevLoaded:((event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0)); if(isNaN(instspd)||!isFinite(instspd)) return; if(firstTick){ speed=instspd; @@ -42,9 +42,9 @@ function dlTest(done){ if(((prevT-startT)/1000.0)>settings.time_dl){try{xhr.abort();}catch(e){} xhr=null; done();} }.bind(this); xhr.onload=function(){ - dlStatus=((speed*8)/1048576.0).toFixed(2); - xhr=null; - done(); + prevT=new Date().getTime(); prevLoaded=0; fistTick=true; + xhr.open("GET",settings.url_dl+"?r="+Math.random(),true); + xhr.send(); }.bind(this); xhr.onerror=function(){ dlStatus="Fail"; @@ -58,12 +58,12 @@ function ulTest(done){ var firstTick=true,startT=new Date().getTime(), prevT=new Date().getTime(),prevLoaded=0,speed=0.0; xhr=new XMLHttpRequest(); xhr.upload.onprogress=function(event){ - var instspd=(event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0); + var instspd=event.loaded<=0?prevLoaded:((event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0)); if(isNaN(instspd)||!isFinite(instspd)) return; if(firstTick){ firstTick=false; }else{ - speed=speed*0.7+instspd*0.3; + speed=instspdsettings.time_ul){try{xhr.abort();}catch(e){} xhr=null; done();} }.bind(this); xhr.onload=function(){ - ulStatus=((speed*8)/1048576.0).toFixed(2); - done(); + prevT=new Date().getTime(); prevLoaded=0; fistTick=true; + xhr.open("POST",settings.url_ul+"?r="+Math.random(),true); + xhr.send(r); }.bind(this); xhr.onerror=function(){ ulStatus="Fail"; + xhr=null; done(); }.bind(this); xhr.open("POST",settings.url_ul+"?r="+Math.random(),true);