speedtest_worker.js 4.6 KB

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