Now using JSON for handling settings; Minor changes

This commit is contained in:
dosse91 2016-10-23 19:47:55 +02:00
parent 7051405fdc
commit c4dd6d89b8
6 changed files with 35 additions and 41 deletions

View file

@ -2,23 +2,23 @@
No Flash, No Java, No Websocket, No Bullshit. No Flash, No Java, No Websocket, No Bullshit.
This is a very small (4k) Speedtest implemented in Javascript, using XMLHttpRequest and Web Workers. This is a very small Speedtest implemented in Javascript, using XMLHttpRequest and Web Workers.
## Try it ## Try it
[Take a Speedtest](http://speedtest.adolfintel.com) [Take a Speedtest](http://speedtest.adolfintel.com)
## Compatibility ## Compatibility
Only modern browsers are supported (Edge 12+) Microsoft Edge, Firefox 10+, Chrome 10+, Opera 15+, Safari 7 (not tested)
## Requirements ## Requirements
- A reasonably fast web server - A reasonably fast web serve
- Some way to generate garbage data using either the included PHP script, a [big file of random data](http://downloads.adolfintel.com/geth.php?r=speedtest-bigfile), or a symlink to /dev/urandom - Some way to generate garbage data using either the included PHP script, a [big file of random data](http://downloads.adolfintel.com/geth.php?r=speedtest-bigfile), or a symlink to /dev/urandom
- Your server must not compress the data it sends - Your server must not compress the data it sends
- Your server must accept large POST requests (up to 10 Megabytes), otherwise the upload test will fail - Your server must accept large POST requests (up to 10 Megabytes), otherwise the upload test will fail
- Client side, there must not be any type of buffering (such as a proxy), or you may get incorrect results - Client side, there must not be any type of buffering (such as a proxy), or you may get incorrect results
## How to use ## How to use
See the examples, it's really simple. See example.html, it's not rocket science.
## License ## License
Copyright (C) 2016 Federico Dossena Copyright (C) 2016 Federico Dossena

View file

@ -20,7 +20,7 @@
document.getElementById("upload").innerHTML=data[2]+" Mbit/s"; document.getElementById("upload").innerHTML=data[2]+" Mbit/s";
document.getElementById("ping").innerHTML=data[3]+" ms"; document.getElementById("ping").innerHTML=data[3]+" ms";
} }
w.postMessage("start"); //start the speedtest (default params: garbage.php empty.dat empty.dat) w.postMessage("start"); //start the speedtest (default params. keep garbage.php and empty.dat in the same directory as the js file)
</script> </script>
</body> </body>
</html> </html>

View file

@ -64,10 +64,7 @@
ul.innerHTML=data[2]; ul.innerHTML=data[2];
ping.innerHTML=data[3]; ping.innerHTML=data[3];
}.bind(this); }.bind(this);
w.postMessage("set url_dl garbage.php"); w.postMessage('start {"url_dl":"garbage.php","url_ul":"empty.dat","url_ping":"empty.dat","time_dl":"10","time_ul":"15","count_ping":"30"}'); //start with custom parameters. paths are relative to js file. you can omit parameters that you don't want to change
w.postMessage("set url_ul empty.dat");
w.postMessage("set url_ping empty.dat");
w.postMessage("start");
</script> </script>
</body> </body>
</html> </html>

View file

@ -50,8 +50,7 @@
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);
document.getElementById("abortBtn").style.display="none"; document.getElementById("abortBtn").style.display="none";
@ -65,9 +64,6 @@
ul.innerHTML=data[2]; ul.innerHTML=data[2];
ping.innerHTML=data[3]; ping.innerHTML=data[3];
}.bind(this); }.bind(this);
w.postMessage("set time_dl 10"); // Timeout download
w.postMessage("set time_ul 20"); // Timeout upload
w.postMessage("set count_ping 30"); // Count of pings
w.postMessage("start"); w.postMessage("start");
} }
function abortTest(){ function abortTest(){

View file

@ -77,7 +77,7 @@
updateGauge(ggul, data[2]); updateGauge(ggul, data[2]);
updateGauge(ggping, data[3]); updateGauge(ggping, data[3]);
}.bind(this); }.bind(this);
w.postMessage("start"); w.postMessage('start {"time_ul":"5", "time_dl":"5", "count_ping":"10", "url_dl":"garbage.php","url_ul":"empty.dat","url_ping":"empty.dat"}');
} }
function abortTest(){ function abortTest(){
if(w)w.postMessage("abort"); if(w)w.postMessage("abort");

View file

@ -1,29 +1,30 @@
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 settings={time_ul:15, time_dl:15, count_ping:35, 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]=="set") if(params[0]=="status"){
{ postMessage(testStatus+";"+dlStatus+";"+ulStatus+";"+pingStatus);
if(params[1] && settings.hasOwnProperty(params[1]) && params[2]) }
{ if(params[0]=="start"){
settings[params[1]]=params[2]; if(testStatus==0){
} testStatus=1;
} try{
if(params[0]=="status"){ var s=JSON.parse(e.data.substring(5));
postMessage(testStatus+";"+dlStatus+";"+ulStatus+";"+pingStatus); if(typeof s.url_dl != "undefined") settings.url_dl=s.url_dl;
} if(typeof s.url_ul != "undefined") settings.url_ul=s.url_ul;
if(params[0]=="start"){ if(typeof s.url_ping != "undefined") settings.url_ping=s.url_ping;
if(testStatus==0){ if(typeof s.time_dl != "undefined") settings.time_dl=s.time_dl;
testStatus=1; if(typeof s.time_ul != "undefined") settings.time_ul=s.time_ul;
dlTest(function(){testStatus=2;ulTest(function(){testStatus=3;pingTest(function(){testStatus=4;});});}); if(typeof s.count_ping != "undefined") settings.count_ping=s.count_ping;
} }catch(e){}
} 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){} if(params[0]=="abort"){
testStatus=5;dlStatus="";ulStatus="";pingStatus=""; try{if(xhr)xhr.abort();}catch(e){}
} testStatus=5;dlStatus="";ulStatus="";pingStatus="";
}
}); });
function dlTest(done){ function dlTest(done){
@ -41,7 +42,7 @@ function dlTest(done){
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)>settings.time_dl){;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);
@ -95,7 +96,7 @@ function pingTest(done){
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++;
@ -105,7 +106,7 @@ function pingTest(done){
pingStatus="Fail"; pingStatus="Fail";
done(); done();
}.bind(this); }.bind(this);
xhr.open("GET", settings.url_ping+"?r="+Math.random(),true); xhr.open("GET",settings.url_ping+"?r="+Math.random(),true);
xhr.send(); xhr.send();
}.bind(this); }.bind(this);
doPing(); doPing();