From 2facf7f0965e108bedf58eb23e49c785e5acb83a Mon Sep 17 00:00:00 2001 From: Martin Date: Sat, 22 Oct 2016 15:39:16 +0200 Subject: [PATCH] changed handling of settings - changed worker: allowing direct message to set options, also timeouts and pingcounts - examples changed with new set message --- example2.html | 135 +++++++++++++++--------------- example3.html | 192 ++++++++++++++++++++++--------------------- speedtest_worker.js | 196 +++++++++++++++++++++++--------------------- 3 files changed, 269 insertions(+), 254 deletions(-) diff --git a/example2.html b/example2.html index be1a907..951ac4f 100644 --- a/example2.html +++ b/example2.html @@ -1,70 +1,73 @@ - -Speedtest - - - -

Speedtest

-
-
Download
-
  
-
Mbit/s
-
-
-
Upload
-
  
-
Mbit/s
-
-
-
Latency
-
  
-
ms
-
- - + + + +

Speedtest

+
+
Download
+
  
+
Mbit/s
+
+
+
Upload
+
  
+
Mbit/s
+
+
+
Latency
+
  
+
ms
+
+ + \ No newline at end of file diff --git a/example3.html b/example3.html index 9ee0a41..0440f8a 100644 --- a/example3.html +++ b/example3.html @@ -1,97 +1,101 @@ - -Speedtest - - - - -

Speedtest

- -Run speedtest - + + Speedtest + + + + +

Speedtest

+ + Run speedtest + \ No newline at end of file diff --git a/speedtest_worker.js b/speedtest_worker.js index 3ccea6e..4cbe8e4 100644 --- a/speedtest_worker.js +++ b/speedtest_worker.js @@ -1,104 +1,112 @@ var testStatus=0,dlStatus="",ulStatus="",pingStatus=""; +var settings={time_ul : 15, time_dl:15, count_ping: 15, url_dl:"garbage.php",url_ul:"empty.dat",url_ping:"empty.dat"}; var xhr=null; this.addEventListener('message', function(e){ - var params=e.data.split(" "); - if(params[0]=="status"){ - postMessage(testStatus+";"+dlStatus+";"+ulStatus+";"+pingStatus); - } - if(params[0]=="start"){ - if(testStatus==0){ - testStatus=1; - var dlUrl=params[1]?params[1]:"garbage.php", ulUrl=params[2]?params[2]:"empty.dat", pingUrl=params[3]?params[3]:"empty.dat"; - dlTest(dlUrl,function(){testStatus=2;ulTest(ulUrl,function(){testStatus=3;pingTest(pingUrl,function(){testStatus=4;});});}); - } - } - if(params[0]=="abort"){ - try{if(xhr)xhr.abort();}catch(e){} - testStatus=5;dlStatus="";ulStatus="";pingStatus=""; - } + var params=e.data.split(" "); + if(params[0]=="set") + { + if(params[1] && settings.hasOwnProperty(params[1]) && params[2]) + { + settings[params[1]]=params[2]; + } + } + if(params[0]=="status"){ + postMessage(testStatus+";"+dlStatus+";"+ulStatus+";"+pingStatus); + } + if(params[0]=="start"){ + if(testStatus==0){ + testStatus=1; + dlTest(function(){testStatus=2;ulTest(function(){testStatus=3;pingTest(function(){testStatus=4;});});}); + } + } + if(params[0]=="abort"){ + console.warn("Aborting test...") + try{if(xhr)xhr.abort();}catch(e){} + testStatus=5;dlStatus="";ulStatus="";pingStatus=""; + } }); -function dlTest(serverURL,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); - if(isNaN(instspd)||!isFinite(instspd)) return; - if(firstTick){ - speed=instspd; - firstTick=false; - }else{ - speed=speed*0.9+instspd*0.1; - } - prevLoaded=event.loaded; - prevT=new Date().getTime(); - dlStatus=((speed*8)/1048576.0).toFixed(2); - if(((prevT-startT)/1000.0)>15){try{xhr.abort();}catch(e){} xhr=null; done();} - }.bind(this); - xhr.onload=function(){ - dlStatus=((speed*8)/1048576.0).toFixed(2); - xhr=null; - done(); - }.bind(this); - xhr.onerror=function(){ - dlStatus="Fail"; - xhr=null; - done(); - }.bind(this); - xhr.open("GET", serverURL+"?random="+Math.random(),true); - xhr.send(); +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); + if(isNaN(instspd)||!isFinite(instspd)) return; + if(firstTick){ + speed=instspd; + firstTick=false; + }else{ + speed=speed*0.9+instspd*0.1; + } + prevLoaded=event.loaded; + prevT=new Date().getTime(); + dlStatus=((speed*8)/1048576.0).toFixed(2); + 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(); + }.bind(this); + xhr.onerror=function(){ + dlStatus="Fail"; + xhr=null; + done(); + }.bind(this); + xhr.open("GET", settings.url_dl+"?r="+Math.random(),true); + xhr.send(); } -function ulTest(serverURL,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); - if(isNaN(instspd)||!isFinite(instspd)) return; - if(firstTick){ - firstTick=false; - }else{ - speed=speed*0.7+instspd*0.3; - } - prevLoaded=event.loaded; - prevT=new Date().getTime(); - ulStatus=((speed*8)/1048576.0).toFixed(2); - if(((prevT-startT)/1000.0)>15){try{xhr.abort();}catch(e){} xhr=null; done();} - }.bind(this); - xhr.onload=function(){ - ulStatus=((speed*8)/1048576.0).toFixed(2); - done(); - }.bind(this); - xhr.onerror=function(){ - ulStatus="Fail"; - done(); - }.bind(this); - xhr.open("POST", serverURL+"?random="+Math.random(),true); - xhr.send(new ArrayBuffer(10485760)); +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); + if(isNaN(instspd)||!isFinite(instspd)) return; + if(firstTick){ + firstTick=false; + }else{ + speed=speed*0.7+instspd*0.3; + } + prevLoaded=event.loaded; + prevT=new Date().getTime(); + ulStatus=((speed*8)/1048576.0).toFixed(2); + if(((prevT-startT)/1000.0)>settings.time_ul){try{xhr.abort();}catch(e){} xhr=null; done();} + }.bind(this); + xhr.onload=function(){ + ulStatus=((speed*8)/1048576.0).toFixed(2); + done(); + }.bind(this); + xhr.onerror=function(){ + ulStatus="Fail"; + done(); + }.bind(this); + xhr.open("POST", settings.url_ul+"?r="+Math.random(),true); + xhr.send(new ArrayBuffer(10485760)); } -function pingTest(pingUrl,done){ - var prevT=null,ping=0.0,i=0; - var doPing=function(){ - prevT=new Date().getTime(); - xhr=new XMLHttpRequest(); - xhr.onload=function(){ - if(i==0){ - prevT=new Date().getTime(); - }else{ - var instspd=new Date().getTime()-prevT; - if(i==1) ping=instspd; else ping=ping*0.9+instspd*0.1; - } - pingStatus=ping.toFixed(2); - i++; - if(i<50) doPing(); else done(); - }.bind(this); - xhr.onerror=function(){ - pingStatus="Fail"; - done(); - }.bind(this); - xhr.open("GET", pingUrl+"?random="+Math.random(),true); - xhr.send(); - }.bind(this); - doPing(); +function pingTest(done){ + var prevT=null,ping=0.0,i=0; + var doPing=function(){ + prevT=new Date().getTime(); + xhr=new XMLHttpRequest(); + xhr.onload=function(){ + if(i==0){ + prevT=new Date().getTime(); + }else{ + var instspd=new Date().getTime()-prevT; + if(i==1) ping=instspd; else ping=ping*0.9+instspd*0.1; + } + pingStatus=ping.toFixed(2); + i++; + if(i