speedtest_worker.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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"){
  10. if(testStatus==0){
  11. testStatus=1;
  12. try{
  13. var s=JSON.parse(e.data.substring(5));
  14. if(typeof s.url_dl != "undefined") settings.url_dl=s.url_dl;
  15. if(typeof s.url_ul != "undefined") settings.url_ul=s.url_ul;
  16. if(typeof s.url_ping != "undefined") settings.url_ping=s.url_ping;
  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. dlTest(function(){testStatus=2;ulTest(function(){testStatus=3;pingTest(function(){testStatus=4;});});});
  22. }
  23. }
  24. if(params[0]=="abort"){
  25. try{if(xhr)xhr.abort();}catch(e){}
  26. testStatus=5;dlStatus="";ulStatus="";pingStatus="";
  27. }
  28. });
  29. function dlTest(done){
  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-prevLoaded)/((new Date().getTime()-prevT)/1000.0);
  34. if(isNaN(instspd)||!isFinite(instspd)) 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. dlStatus=((speed*8)/1048576.0).toFixed(2);
  48. xhr=null;
  49. done();
  50. }.bind(this);
  51. xhr.onerror=function(){
  52. dlStatus="Fail";
  53. xhr=null;
  54. done();
  55. }.bind(this);
  56. xhr.open("GET", settings.url_dl+"?r="+Math.random(),true);
  57. xhr.send();
  58. }
  59. function ulTest(done){
  60. var firstTick=true,startT=new Date().getTime(), prevT=new Date().getTime(),prevLoaded=0,speed=0.0;
  61. xhr=new XMLHttpRequest();
  62. xhr.upload.onprogress=function(event){
  63. var instspd=(event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0);
  64. if(isNaN(instspd)||!isFinite(instspd)) return;
  65. if(firstTick){
  66. firstTick=false;
  67. }else{
  68. speed=speed*0.7+instspd*0.3;
  69. }
  70. prevLoaded=event.loaded;
  71. prevT=new Date().getTime();
  72. ulStatus=((speed*8)/1048576.0).toFixed(2);
  73. if(((prevT-startT)/1000.0)>settings.time_ul){try{xhr.abort();}catch(e){} xhr=null; done();}
  74. }.bind(this);
  75. xhr.onload=function(){
  76. ulStatus=((speed*8)/1048576.0).toFixed(2);
  77. done();
  78. }.bind(this);
  79. xhr.onerror=function(){
  80. ulStatus="Fail";
  81. done();
  82. }.bind(this);
  83. xhr.open("POST", settings.url_ul+"?r="+Math.random(),true);
  84. xhr.send(new ArrayBuffer(10485760));
  85. }
  86. function pingTest(done){
  87. var prevT=null,ping=0.0,i=0;
  88. var doPing=function(){
  89. prevT=new Date().getTime();
  90. xhr=new XMLHttpRequest();
  91. xhr.onload=function(){
  92. if(i==0){
  93. prevT=new Date().getTime();
  94. }else{
  95. var instspd=new Date().getTime()-prevT;
  96. if(i==1)ping=instspd; else ping=ping*0.9+instspd*0.1;
  97. }
  98. pingStatus=ping.toFixed(2);
  99. i++;
  100. if(i<settings.count_ping) doPing(); else done();
  101. }.bind(this);
  102. xhr.onerror=function(){
  103. pingStatus="Fail";
  104. done();
  105. }.bind(this);
  106. xhr.open("GET",settings.url_ping+"?r="+Math.random(),true);
  107. xhr.send();
  108. }.bind(this);
  109. doPing();
  110. }