changed handling of settings

- changed worker: allowing direct message to set options, also timeouts and pingcounts
- examples changed with new set message
This commit is contained in:
Martin 2016-10-22 15:39:16 +02:00 committed by GitHub
parent d8b1f0d085
commit 2facf7f096
3 changed files with 269 additions and 254 deletions

View file

@ -1,70 +1,73 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Speedtest</title> <title>Speedtest</title>
<style type="text/css"> <style type="text/css">
html,body{ html,body{
margin:0; margin:0;
padding:0; padding:0;
border:none; border:none;
text-align:center; text-align:center;
} }
div.test{ div.test{
display:inline-block; display:inline-block;
width:30vw; width:30vw;
text-align:center; text-align:center;
} }
div.testName,div.meterUnit{ div.testName,div.meterUnit{
font-size:3vw; font-size:3vw;
} }
div.meter{ div.meter{
font-size:6vw; font-size:6vw;
line-height:1.5em; line-height:1.5em;
height:1.5em !important; height:1.5em !important;
} }
.flash{ .flash{
animation:flash 0.6s linear infinite; animation:flash 0.6s linear infinite;
} }
@keyframes flash{ @keyframes flash{
0%{opacity:0.6;} 0%{opacity:0.6;}
50%{opacity:1;} 50%{opacity:1;}
} }
</style> </style>
</head> </head>
<body> <body>
<h1>Speedtest</h1> <h1>Speedtest</h1>
<div class="test"> <div class="test">
<div class="testName">Download</div> <div class="testName">Download</div>
<div class="meter">&nbsp;<span id="download"></span>&nbsp;</div> <div class="meter">&nbsp;<span id="download"></span>&nbsp;</div>
<div class="meterUnit">Mbit/s</div> <div class="meterUnit">Mbit/s</div>
</div> </div>
<div class="test"> <div class="test">
<div class="testName">Upload</div> <div class="testName">Upload</div>
<div class="meter">&nbsp;<span id="upload"></span>&nbsp;</div> <div class="meter">&nbsp;<span id="upload"></span>&nbsp;</div>
<div class="meterUnit">Mbit/s</div> <div class="meterUnit">Mbit/s</div>
</div> </div>
<div class="test"> <div class="test">
<div class="testName">Latency</div> <div class="testName">Latency</div>
<div class="meter">&nbsp;<span id="ping"></span>&nbsp;</div> <div class="meter">&nbsp;<span id="ping"></span>&nbsp;</div>
<div class="meterUnit">ms</div> <div class="meterUnit">ms</div>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
var w=new Worker("speedtest_worker.js"); var w=new Worker("speedtest_worker.js");
var interval=setInterval(function(){w.postMessage("status");}.bind(this),100); var interval=setInterval(function(){w.postMessage("status");}.bind(this),100);
w.onmessage=function(event){ w.onmessage=function(event){
var data=event.data.split(";"); var data=event.data.split(";");
var status=Number(data[0]); var status=Number(data[0]);
var dl=document.getElementById("download"),ul=document.getElementById("upload"),ping=document.getElementById("ping"); var dl=document.getElementById("download"),ul=document.getElementById("upload"),ping=document.getElementById("ping");
dl.className=status==1?"flash":"";ul.className=status==2?"flash":"";ping.className=status==3?"flash":""; dl.className=status==1?"flash":"";ul.className=status==2?"flash":"";ping.className=status==3?"flash":"";
if(status>=4){ if(status>=4){
clearInterval(interval); clearInterval(interval);
} }
dl.innerHTML=data[1]; dl.innerHTML=data[1];
ul.innerHTML=data[2]; ul.innerHTML=data[2];
ping.innerHTML=data[3]; ping.innerHTML=data[3];
}.bind(this); }.bind(this);
w.postMessage("start garbage.php empty.dat empty.dat"); w.postMessage("set url_dl garbage.php");
</script> w.postMessage("set url_ul empty.dat");
</body> w.postMessage("set url_ping empty.dat");
w.postMessage("start");
</script>
</body>
</html> </html>

View file

@ -1,97 +1,101 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Speedtest</title> <title>Speedtest</title>
<style type="text/css"> <style type="text/css">
html,body{ html,body{
margin:0; margin:0;
padding:0; padding:0;
border:none; border:none;
text-align:center; text-align:center;
} }
div.test{ div.test{
display:inline-block; display:inline-block;
width:30vw; width:30vw;
text-align:center; text-align:center;
} }
div.testName,div.meterUnit{ div.testName,div.meterUnit{
font-size:3vw; font-size:3vw;
} }
div.meter{ div.meter{
font-size:6vw; font-size:6vw;
line-height:1.5em; line-height:1.5em;
height:1.5em !important; height:1.5em !important;
} }
.flash{ .flash{
animation:flash 0.6s linear infinite; animation:flash 0.6s linear infinite;
} }
@keyframes flash{ @keyframes flash{
0%{opacity:0.6;} 0%{opacity:0.6;}
50%{opacity:1;} 50%{opacity:1;}
} }
a{ a{
display:inline-block; display:inline-block;
border:0.15em solid #000000; border:0.15em solid #000000;
padding:0.3em 0.5em; padding:0.3em 0.5em;
margin:0.6em; margin:0.6em;
color:#000000; color:#000000;
text-decoration:none; text-decoration:none;
} }
</style> </style>
<script type="text/javascript"> <script type="text/javascript">
var w=null; var w=null;
function runTest(){ function runTest(){
document.getElementById("startBtn").style.display="none"; document.getElementById("startBtn").style.display="none";
document.getElementById("testArea").style.display=""; document.getElementById("testArea").style.display="";
document.getElementById("abortBtn").style.display=""; document.getElementById("abortBtn").style.display="";
w=new Worker("speedtest_worker.js"); w=new Worker("speedtest_worker.js");
var interval=setInterval(function(){w.postMessage("status");}.bind(this),100); var interval=setInterval(function(){w.postMessage("status");}.bind(this),100);
w.onmessage=function(event){ w.onmessage=function(event){
var data=event.data.split(";"); var data=event.data.split(";");
var status=Number(data[0]); var status=Number(data[0]);
var dl=document.getElementById("download"),ul=document.getElementById("upload"),ping=document.getElementById("ping"); var dl=document.getElementById("download"),ul=document.getElementById("upload"),ping=document.getElementById("ping");
dl.className=status==1?"flash":"";ul.className=status==2?"flash":"";ping.className=status==3?"flash":""; // dl.className=status==1?"flash":"";ul.className=status==2?"flash":"";ping.className=status==3?"flash":"";
if(status>=4){
clearInterval(interval); if(status>=4){
document.getElementById("abortBtn").style.display="none"; clearInterval(interval);
document.getElementById("startBtn").style.display=""; document.getElementById("abortBtn").style.display="none";
w=null; document.getElementById("startBtn").style.display="";
} w=null;
if(status==5){ }
document.getElementById("testArea").style.display="none"; if(status==5){
} document.getElementById("testArea").style.display="none";
dl.innerHTML=data[1]; }
ul.innerHTML=data[2]; dl.innerHTML=data[1];
ping.innerHTML=data[3]; ul.innerHTML=data[2];
}.bind(this); ping.innerHTML=data[3];
w.postMessage("start garbage.php empty.dat empty.dat"); }.bind(this);
} w.postMessage("set time_dl 10"); // Timeout download
function abortTest(){ w.postMessage("set time_ul 20"); // Timeout upload
if(w)w.postMessage("abort"); w.postMessage("set count_ping 30"); // Count of pings
} w.postMessage("start");
</script> }
</head> function abortTest(){
<body> if(w)w.postMessage("abort");
<h1>Speedtest</h1> }
<div id="testArea" style="display:none"> </script>
<div class="test"> </head>
<div class="testName">Download</div> <body>
<div class="meter">&nbsp;<span id="download"></span>&nbsp;</div> <h1>Speedtest</h1>
<div class="meterUnit">Mbit/s</div> <div id="testArea" style="display:none">
</div> <div class="test">
<div class="test"> <div class="testName">Download</div>
<div class="testName">Upload</div> <div class="meter">&nbsp;<span id="download"></span>&nbsp;</div>
<div class="meter">&nbsp;<span id="upload"></span>&nbsp;</div> <div class="meterUnit">Mbit/s</div>
<div class="meterUnit">Mbit/s</div> </div>
</div> <div class="test">
<div class="test"> <div class="testName">Upload</div>
<div class="testName">Latency</div> <div class="meter">&nbsp;<span id="upload"></span>&nbsp;</div>
<div class="meter">&nbsp;<span id="ping"></span>&nbsp;</div> <div class="meterUnit">Mbit/s</div>
<div class="meterUnit">ms</div> </div>
</div> <div class="test">
<br/> <div class="testName">Latency</div>
<a href="javascript:abortTest()" id="abortBtn">Abort</a> <div class="meter">&nbsp;<span id="ping"></span>&nbsp;</div>
</div> <div class="meterUnit">ms</div>
<a href="javascript:runTest()" id="startBtn">Run speedtest</a> </div>
</body> <br/>
<a href="javascript:abortTest()" id="abortBtn">Abort</a>
</div>
<a href="javascript:runTest()" id="startBtn">Run speedtest</a>
</body>
</html> </html>

View file

@ -1,104 +1,112 @@
var testStatus=0,dlStatus="",ulStatus="",pingStatus=""; 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; var xhr=null;
this.addEventListener('message', function(e){ this.addEventListener('message', function(e){
var params=e.data.split(" "); var params=e.data.split(" ");
if(params[0]=="status"){ if(params[0]=="set")
postMessage(testStatus+";"+dlStatus+";"+ulStatus+";"+pingStatus); {
} if(params[1] && settings.hasOwnProperty(params[1]) && params[2])
if(params[0]=="start"){ {
if(testStatus==0){ settings[params[1]]=params[2];
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]=="status"){
} postMessage(testStatus+";"+dlStatus+";"+ulStatus+";"+pingStatus);
} }
if(params[0]=="abort"){ if(params[0]=="start"){
try{if(xhr)xhr.abort();}catch(e){} if(testStatus==0){
testStatus=5;dlStatus="";ulStatus="";pingStatus=""; 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){ function dlTest(done){
var firstTick=true,startT=new Date().getTime(), prevT=new Date().getTime(),prevLoaded=0,speed=0.0; var firstTick=true,startT=new Date().getTime(), prevT=new Date().getTime(),prevLoaded=0,speed=0.0;
xhr=new XMLHttpRequest(); xhr=new XMLHttpRequest();
xhr.onprogress=function(event){ xhr.onprogress=function(event){
var instspd=(event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0); var instspd=(event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0);
if(isNaN(instspd)||!isFinite(instspd)) return; if(isNaN(instspd)||!isFinite(instspd)) return;
if(firstTick){ if(firstTick){
speed=instspd; speed=instspd;
firstTick=false; firstTick=false;
}else{ }else{
speed=speed*0.9+instspd*0.1; speed=speed*0.9+instspd*0.1;
} }
prevLoaded=event.loaded; prevLoaded=event.loaded;
prevT=new Date().getTime(); prevT=new Date().getTime();
dlStatus=((speed*8)/1048576.0).toFixed(2); dlStatus=((speed*8)/1048576.0).toFixed(2);
if(((prevT-startT)/1000.0)>15){try{xhr.abort();}catch(e){} xhr=null; done();} if(((prevT-startT)/1000.0)>settings.time_dl){;try{xhr.abort();}catch(e){} xhr=null; done();}
}.bind(this); }.bind(this);
xhr.onload=function(){ xhr.onload=function(){
dlStatus=((speed*8)/1048576.0).toFixed(2); dlStatus=((speed*8)/1048576.0).toFixed(2);
xhr=null; xhr=null;
done(); done();
}.bind(this); }.bind(this);
xhr.onerror=function(){ xhr.onerror=function(){
dlStatus="Fail"; dlStatus="Fail";
xhr=null; xhr=null;
done(); done();
}.bind(this); }.bind(this);
xhr.open("GET", serverURL+"?random="+Math.random(),true); xhr.open("GET", settings.url_dl+"?r="+Math.random(),true);
xhr.send(); xhr.send();
} }
function ulTest(serverURL,done){ function ulTest(done){
var firstTick=true,startT=new Date().getTime(), prevT=new Date().getTime(),prevLoaded=0,speed=0.0; var firstTick=true,startT=new Date().getTime(), prevT=new Date().getTime(),prevLoaded=0,speed=0.0;
xhr=new XMLHttpRequest(); xhr=new XMLHttpRequest();
xhr.upload.onprogress=function(event){ xhr.upload.onprogress=function(event){
var instspd=(event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0); var instspd=(event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0);
if(isNaN(instspd)||!isFinite(instspd)) return; if(isNaN(instspd)||!isFinite(instspd)) return;
if(firstTick){ if(firstTick){
firstTick=false; firstTick=false;
}else{ }else{
speed=speed*0.7+instspd*0.3; speed=speed*0.7+instspd*0.3;
} }
prevLoaded=event.loaded; prevLoaded=event.loaded;
prevT=new Date().getTime(); prevT=new Date().getTime();
ulStatus=((speed*8)/1048576.0).toFixed(2); ulStatus=((speed*8)/1048576.0).toFixed(2);
if(((prevT-startT)/1000.0)>15){try{xhr.abort();}catch(e){} xhr=null; done();} if(((prevT-startT)/1000.0)>settings.time_ul){try{xhr.abort();}catch(e){} xhr=null; done();}
}.bind(this); }.bind(this);
xhr.onload=function(){ xhr.onload=function(){
ulStatus=((speed*8)/1048576.0).toFixed(2); ulStatus=((speed*8)/1048576.0).toFixed(2);
done(); done();
}.bind(this); }.bind(this);
xhr.onerror=function(){ xhr.onerror=function(){
ulStatus="Fail"; ulStatus="Fail";
done(); done();
}.bind(this); }.bind(this);
xhr.open("POST", serverURL+"?random="+Math.random(),true); xhr.open("POST", settings.url_ul+"?r="+Math.random(),true);
xhr.send(new ArrayBuffer(10485760)); xhr.send(new ArrayBuffer(10485760));
} }
function pingTest(pingUrl,done){ function pingTest(done){
var prevT=null,ping=0.0,i=0; var prevT=null,ping=0.0,i=0;
var doPing=function(){ var doPing=function(){
prevT=new Date().getTime(); prevT=new Date().getTime();
xhr=new XMLHttpRequest(); xhr=new XMLHttpRequest();
xhr.onload=function(){ xhr.onload=function(){
if(i==0){ if(i==0){
prevT=new Date().getTime(); prevT=new Date().getTime();
}else{ }else{
var instspd=new Date().getTime()-prevT; var instspd=new Date().getTime()-prevT;
if(i==1) ping=instspd; else ping=ping*0.9+instspd*0.1; if(i==1) ping=instspd; else ping=ping*0.9+instspd*0.1;
} }
pingStatus=ping.toFixed(2); pingStatus=ping.toFixed(2);
i++; i++;
if(i<50) doPing(); else done(); if(i<settings.count_ping) doPing(); else done();
}.bind(this); }.bind(this);
xhr.onerror=function(){ xhr.onerror=function(){
pingStatus="Fail"; pingStatus="Fail";
done(); done();
}.bind(this); }.bind(this);
xhr.open("GET", pingUrl+"?random="+Math.random(),true); xhr.open("GET", settings.url_ping+"?r="+Math.random(),true);
xhr.send(); xhr.send();
}.bind(this); }.bind(this);
doPing(); doPing();
} }