commit bec767c9ed9bd1fbc6108e50e744a9aee314a5be Author: dosse91 Date: Fri Mar 4 12:48:09 2016 +0100 First commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..7a154d4 --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# Speedtest in 4k + +No Flash, No Java, No Websocket, No Bullshit. + +This is a very small (4k) Speedtest implemented in Javascript, that relies only on XMLHttpRequest. + +## Try it +[Take a Speedtest](http://speedtest.adolfintel.com) + +## Compatibility +Microsoft Edge, Firefox 10+, Chrome 10+, Opera 15+, Safari 7 (not tested) + +## Requirements + - 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 + - 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 + - Client side, there must not be any type of buffering (such as a proxy), or you may get incorrect results + +## How to use +See example.html, it's not rocket science. + +## License +Copyright (C) 2016 Federico Dossena + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program. If not, see . diff --git a/empty.dat b/empty.dat new file mode 100644 index 0000000..e69de29 diff --git a/example.html b/example.html new file mode 100644 index 0000000..84b78f9 --- /dev/null +++ b/example.html @@ -0,0 +1,46 @@ + + + +Speedtest + + + +

Speedtest

+

Download

+
Wait...
+

Upload

+
Wait...
+

Latency

+
Wait...
+ + + \ No newline at end of file diff --git a/garbage.php b/garbage.php new file mode 100644 index 0000000..be7c0f6 --- /dev/null +++ b/garbage.php @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/speedtest.js b/speedtest.js new file mode 100644 index 0000000..ce93259 --- /dev/null +++ b/speedtest.js @@ -0,0 +1,123 @@ +function DownloadTester(serverURL,done,update,err){ + this.xhr=new XMLHttpRequest(); + this.firstTick=true; + this.prevLoaded=0; + this.startT=new Date().getTime(); + this.prevT=new Date().getTime(); + this.speed=0.0; + if(done)this.onDone=done; if(update)this.onUpdate=update; if(err)this.onFail=err; + this.xhr.onprogress=function(event){ + var instspd=(event.loaded-this.prevLoaded)/((new Date().getTime()-this.prevT)/1000.0); + if(isNaN(instspd)||!isFinite(instspd)) return; + if(this.firstTick){ + this.speed=instspd; + this.firstTick=false; + }else{ + this.speed=this.speed*0.9+instspd*0.1; + } + this.prevLoaded=event.loaded; + this.prevT=new Date().getTime(); + this.onUpdate(); + if(((this.prevT-this.startT)/1000.0)>15){try{this.xhr.abort();}catch(e){} this.onDone();} + }.bind(this); + this.xhr.onload=function(){ + this.onUpdate(); + this.onDone(); + }.bind(this); + this.xhr.onerror=function(){ + this.onUpdate(); + this.onFail(); + }.bind(this); + this.xhr.open("GET", serverURL+"?random="+Math.random(),true); + this.xhr.send(); +} +DownloadTester.prototype={ + constructor:DownloadTester, + onDone:function(){}, + onFail:function(){}, + onUpdate:function(){}, + getValue:function(){return ((this.speed*8)/1048576.0).toFixed(2);}, + cancel:function(){try{this.xhr.abort();}catch(e){}} +} + +function UploadTester(serverURL,done,update,err){ + this.xhr=new XMLHttpRequest(); + this.firstTick=true; + this.prevLoaded=0; + this.startT=new Date().getTime(); + this.prevT=new Date().getTime(); + this.speed=0.0; + if(done)this.onDone=done; if(update)this.onUpdate=update; if(err)this.onFail=err; + this.xhr.upload.onprogress=function(event){ + var instspd=(event.loaded-this.prevLoaded)/((new Date().getTime()-this.prevT)/1000.0); + if(isNaN(instspd)||!isFinite(instspd)) return; + if(this.firstTick){ + this.firstTick=false; + }else{ + this.speed=this.speed*0.7+instspd*0.3; + } + this.prevLoaded=event.loaded; + this.prevT=new Date().getTime(); + this.onUpdate(); + if(((this.prevT-this.startT)/1000.0)>15){try{this.xhr.abort();}catch(e){} this.onDone();} + }.bind(this); + this.xhr.onload=function(){ + this.onUpdate(); + this.onDone(); + }.bind(this); + this.xhr.onerror=function(){ + this.onUpdate(); + this.onFail(); + }.bind(this); + this.xhr.open("POST", serverURL+"?random="+Math.random(),true); + this.xhr.send(new ArrayBuffer(10485760)); +} +UploadTester.prototype={ + constructor:UploadTester, + onDone:function(){}, + onFail:function(){}, + onUpdate:function(){}, + getValue:function(){return ((this.speed*8)/1048576.0).toFixed(2);}, + cancel:function(){try{this.xhr.abort();}catch(e){}} +} + +function PingTester(serverURL,done,update,err){ + this.xhr=null; + this.prevT=null; + this.ping=0.0; + this.i=0; + this.pingURL=serverURL; + if(done)this.onDone=done; + if(update)this.onUpdate=update; + if(err)this.onFail=err; + this.doPing=function(){ + this.prevT=new Date().getTime(); + this.xhr=new XMLHttpRequest(); + this.xhr.onload=function(){ + if(this.i==0){ + this.prevT=new Date().getTime(); + }else{ + var instspd=new Date().getTime()-this.prevT; + if(this.i==1) this.ping=instspd; else this.ping=this.ping*0.9+instspd*0.1; + } + this.onUpdate(); + this.i++; + if(this.i<50) this.doPing(); else this.onDone(); + }.bind(this); + this.xhr.onerror=function(){ + this.onUpdate(); + this.onFail(); + }.bind(this); + this.xhr.open("GET", this.pingURL+"?random="+Math.random(),true); + this.xhr.send(); + }.bind(this); + this.doPing(); +} +PingTester.prototype={ + constructor:PingTester, + onDone:function(){}, + onFail:function(){}, + onUpdate:function(){}, + getValue:function(){return this.ping.toFixed(2);}, + cancel:function(){this.i=9999; if(this.xhr) try{xhr.abort();}catch(e){}} +} \ No newline at end of file