speedtest_worker.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var testStatus=0,dlStatus="",ulStatus="",pingStatus="",clientIp="";
  2. var settings={time_ul:15,time_dl:15,count_ping:35,url_dl:"garbage.php",url_ul:"empty.dat",url_ping:"empty.dat",url_getIp:"getIP.php"};
  3. var xhr=null;
  4. this.addEventListener('message', function(e){
  5. var params=e.data.split(" ");
  6. if(params[0]=="status"){
  7. postMessage(testStatus+";"+dlStatus+";"+ulStatus+";"+pingStatus+";"+clientIp);
  8. }
  9. if(params[0]=="start"&&testStatus==0){
  10. testStatus=1;
  11. try{
  12. var s=JSON.parse(e.data.substring(5));
  13. if(typeof s.url_dl != "undefined") settings.url_dl=s.url_dl;
  14. if(typeof s.url_ul != "undefined") settings.url_ul=s.url_ul;
  15. if(typeof s.url_ping != "undefined") settings.url_ping=s.url_ping;
  16. if(typeof s.url_getIp != "undefined") settings.url_getIp=s.url_getIp;
  17. if(typeof s.time_dl != "undefined") settings.time_dl=s.time_dl;
  18. if(typeof s.time_ul != "undefined") settings.time_ul=s.time_ul;
  19. if(typeof s.count_ping != "undefined") settings.count_ping=s.count_ping;
  20. }catch(e){}
  21. getIp(function(){dlTest(function(){testStatus=2;ulTest(function(){testStatus=3;pingTest(function(){testStatus=4;});});})});
  22. }
  23. if(params[0]=="abort"){
  24. try{if(xhr)xhr.abort();}catch(e){}
  25. testStatus=5;dlStatus="";ulStatus="";pingStatus="";
  26. }
  27. });
  28. function getIp(done){
  29. xhr=new XMLHttpRequest();
  30. xhr.onload=function(){
  31. clientIp=xhr.responseText;
  32. done();
  33. }
  34. xhr.onerror=function(){
  35. done();
  36. }
  37. xhr.open("GET",settings.url_getIp+"?r="+Math.random(),true);
  38. xhr.send();
  39. }
  40. var dlCalled=false;
  41. function dlTest(done){
  42. if(dlCalled) return; else dlCalled=true;
  43. var firstTick=true,startT=new Date().getTime(), prevT=new Date().getTime(),prevLoaded=0,speed=0.0;
  44. xhr=new XMLHttpRequest();
  45. xhr.onprogress=function(event){
  46. var instspd=event.loaded<=0?speed:((event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0))*1.25;
  47. if(isNaN(instspd)||!isFinite(instspd)||instspd<0) return;
  48. if(firstTick){
  49. speed=instspd;
  50. firstTick=false;
  51. }else{
  52. speed=speed*0.9+instspd*0.1;
  53. }
  54. prevLoaded=event.loaded;
  55. prevT=new Date().getTime();
  56. dlStatus=((speed*8)/1048576.0).toFixed(2);
  57. if(((prevT-startT)/1000.0)>settings.time_dl){xhr.onprogress=null; xhr.onload=null; xhr.onerror=null; try{xhr.abort();}catch(e){} xhr=null; done();}
  58. }.bind(this);
  59. xhr.onload=function(){
  60. prevT=new Date().getTime(); prevLoaded=0; fistTick=true;
  61. xhr.open("GET",settings.url_dl+"?r="+Math.random(),true);
  62. xhr.send();
  63. }.bind(this);
  64. xhr.onerror=function(){
  65. dlStatus="Fail";
  66. try{xhr.abort();}catch(e){}
  67. xhr=null;
  68. done();
  69. }.bind(this);
  70. xhr.open("GET",settings.url_dl+"?r="+Math.random(),true);
  71. xhr.send();
  72. }
  73. var ulCalled=false;
  74. function ulTest(done){
  75. if(ulCalled) return; else ulCalled=true;
  76. var firstTick=true,startT=new Date().getTime(), prevT=new Date().getTime(),prevLoaded=0,speed=0.0;
  77. xhr=new XMLHttpRequest();
  78. xhr.upload.onprogress=function(event){
  79. var instspd=event.loaded<=0?speed:((event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0))*1.25;
  80. if(isNaN(instspd)||!isFinite(instspd)||instspd<0) return;
  81. if(firstTick){
  82. firstTick=false;
  83. }else{
  84. speed=instspd<speed?(speed*0.4+instspd*0.6):(speed*0.8+instspd*0.2);
  85. }
  86. prevLoaded=event.loaded;
  87. prevT=new Date().getTime();
  88. ulStatus=((speed*8)/1048576.0).toFixed(2);
  89. if(((prevT-startT)/1000.0)>settings.time_ul){xhr.upload.onprogress=null; xhr.upload.onload=null; xhr.upload.onerror=null; try{xhr.abort();}catch(e){} xhr=null; done();}
  90. }.bind(this);
  91. xhr.upload.onload=function(){
  92. prevT=new Date().getTime(); prevLoaded=0; fistTick=true;
  93. xhr.open("POST",settings.url_ul+"?r="+Math.random(),true);
  94. xhr.send(r);
  95. }.bind(this);
  96. xhr.upload.onerror=function(){
  97. ulStatus="Fail";
  98. try{xhr.abort();}catch(e){}
  99. xhr=null;
  100. done();
  101. }.bind(this);
  102. xhr.open("POST",settings.url_ul+"?r="+Math.random(),true);
  103. xhr.setRequestHeader('Content-Encoding','identity');
  104. var r=new ArrayBuffer(1048576);
  105. try{r=new Float32Array(r);for(var i=0;i<r.length;i++)r[i]=Math.random();}catch(e){}
  106. var req=[];
  107. for(var i=0;i<20;i++) req.push(r);
  108. req=new Blob(req);
  109. xhr.send(req);
  110. }
  111. var ptCalled=false;
  112. function pingTest(done){
  113. if(ptCalled) return; else ptCalled=true;
  114. var prevT=null,ping=0.0,i=0;
  115. var doPing=function(){
  116. prevT=new Date().getTime();
  117. xhr=new XMLHttpRequest();
  118. xhr.onload=function(){
  119. if(i==0){
  120. prevT=new Date().getTime();
  121. }else{
  122. var instspd=(new Date().getTime()-prevT)/2;
  123. if(i==1)ping=instspd; else ping=ping*0.9+instspd*0.1;
  124. }
  125. pingStatus=ping.toFixed(2);
  126. i++;
  127. if(i<settings.count_ping) doPing(); else done();
  128. }.bind(this);
  129. xhr.onerror=function(){
  130. pingStatus="Fail";
  131. done();
  132. }.bind(this);
  133. xhr.open("GET",settings.url_ping+"?r="+Math.random(),true);
  134. xhr.send();
  135. }.bind(this);
  136. doPing();
  137. }