Преглед изворни кода

Fixed a bug with Firefox 51; Precision improvements to compensate HTTP overhead

dosse91 пре 9 година
родитељ
комит
f8e5dafa7a
2 измењених фајлова са 16 додато и 8 уклоњено
  1. 1 1
      example4.html
  2. 15 7
      speedtest_worker.js

+ 1 - 1
example4.html

@@ -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 {"time_ul":"5", "time_dl":"5", "count_ping":"10", "url_dl":"garbage.php","url_ul":"empty.dat","url_ping":"empty.dat"}');
+                w.postMessage('start {"time_ul":"10", "time_dl":"10", "count_ping":"20", "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");

+ 15 - 7
speedtest_worker.js

@@ -24,12 +24,14 @@ this.addEventListener('message', function(e){
 		testStatus=5;dlStatus="";ulStatus="";pingStatus="";
 		testStatus=5;dlStatus="";ulStatus="";pingStatus="";
 	}
 	}
 });
 });
+var dlCalled=false;
 function dlTest(done){
 function dlTest(done){
+	if(dlCalled) return; else dlCalled=true;
     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<=0?speed:((event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0));
-        if(isNaN(instspd)||!isFinite(instspd)) return;
+        var instspd=event.loaded<=0?speed:((event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0))*1.25;
+        if(isNaN(instspd)||!isFinite(instspd)||instspd<0) return;
         if(firstTick){
         if(firstTick){
             speed=instspd;
             speed=instspd;
             firstTick=false;
             firstTick=false;
@@ -48,18 +50,21 @@ function dlTest(done){
     }.bind(this);
     }.bind(this);
     xhr.onerror=function(){
     xhr.onerror=function(){
         dlStatus="Fail";
         dlStatus="Fail";
+		try{xhr.abort();}catch(e){}
         xhr=null;
         xhr=null;
         done();
         done();
     }.bind(this);
     }.bind(this);
     xhr.open("GET",settings.url_dl+"?r="+Math.random(),true);
     xhr.open("GET",settings.url_dl+"?r="+Math.random(),true);
     xhr.send();
     xhr.send();
 }
 }
+var ulCalled=false;
 function ulTest(done){
 function ulTest(done){
+	if(ulCalled) return; else ulCalled=true;
     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<=0?speed:((event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0));
-        if(isNaN(instspd)||!isFinite(instspd)) return;
+        var instspd=event.loaded<=0?speed:((event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0))*1.25;
+        if(isNaN(instspd)||!isFinite(instspd)||instspd<0) return;
         if(firstTick){
         if(firstTick){
             firstTick=false;
             firstTick=false;
         }else{
         }else{
@@ -70,13 +75,14 @@ function ulTest(done){
         ulStatus=((speed*8)/1048576.0).toFixed(2);
         ulStatus=((speed*8)/1048576.0).toFixed(2);
         if(((prevT-startT)/1000.0)>settings.time_ul){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.upload.onload=function(){
 		prevT=new Date().getTime(); prevLoaded=0; fistTick=true;
 		prevT=new Date().getTime(); prevLoaded=0; fistTick=true;
         xhr.open("POST",settings.url_ul+"?r="+Math.random(),true);
         xhr.open("POST",settings.url_ul+"?r="+Math.random(),true);
 		xhr.send(r);
 		xhr.send(r);
     }.bind(this);
     }.bind(this);
-    xhr.onerror=function(){
+    xhr.upload.onerror=function(){
         ulStatus="Fail";
         ulStatus="Fail";
+		try{xhr.abort();}catch(e){}
 		xhr=null;
 		xhr=null;
         done();
         done();
     }.bind(this);
     }.bind(this);
@@ -89,7 +95,9 @@ function ulTest(done){
 	req=new Blob(req);
 	req=new Blob(req);
     xhr.send(req);
     xhr.send(req);
 }
 }
+var ptCalled=false;
 function pingTest(done){
 function pingTest(done){
+	if(ptCalled) return; else ptCalled=true;
     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();
@@ -98,7 +106,7 @@ function pingTest(done){
             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)/2;
                 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);