| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- 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){}}
- }
|