speedtest_worker.js 4.3 KB

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