speedtest_worker.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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-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. dlStatus=((speed*8)/1048576.0).toFixed(2);
  46. xhr=null;
  47. done();
  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-prevLoaded)/((new Date().getTime()-prevT)/1000.0);
  62. if(isNaN(instspd)||!isFinite(instspd)) return;
  63. if(firstTick){
  64. firstTick=false;
  65. }else{
  66. speed=speed*0.7+instspd*0.3;
  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. ulStatus=((speed*8)/1048576.0).toFixed(2);
  75. done();
  76. }.bind(this);
  77. xhr.onerror=function(){
  78. ulStatus="Fail";
  79. done();
  80. }.bind(this);
  81. xhr.open("POST",settings.url_ul+"?r="+Math.random(),true);
  82. xhr.setRequestHeader('Content-Encoding','identity');
  83. var r=new ArrayBuffer(10485760);
  84. try{var w=new Float32Array(r);for(var i=0;i<w.length;i++)w[i]=Math.random();}catch(e){}
  85. xhr.send(r);
  86. }
  87. function pingTest(done){
  88. var prevT=null,ping=0.0,i=0;
  89. var doPing=function(){
  90. prevT=new Date().getTime();
  91. xhr=new XMLHttpRequest();
  92. xhr.onload=function(){
  93. if(i==0){
  94. prevT=new Date().getTime();
  95. }else{
  96. var instspd=new Date().getTime()-prevT;
  97. if(i==1)ping=instspd; else ping=ping*0.9+instspd*0.1;
  98. }
  99. pingStatus=ping.toFixed(2);
  100. i++;
  101. if(i<settings.count_ping) doPing(); else done();
  102. }.bind(this);
  103. xhr.onerror=function(){
  104. pingStatus="Fail";
  105. done();
  106. }.bind(this);
  107. xhr.open("GET",settings.url_ping+"?r="+Math.random(),true);
  108. xhr.send();
  109. }.bind(this);
  110. doPing();
  111. }