speedtest_worker.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var testStatus=0,dlStatus="",ulStatus="",pingStatus="";
  2. var xhr=null;
  3. this.addEventListener('message', function(e){
  4. var params=e.data.split(" ");
  5. if(params[0]=="status"){
  6. postMessage(testStatus+";"+dlStatus+";"+ulStatus+";"+pingStatus);
  7. }
  8. if(params[0]=="start"){
  9. if(testStatus==0){
  10. testStatus=1;
  11. var dlUrl=params[1]?params[1]:"garbage.php", ulUrl=params[2]?params[2]:"empty.dat", pingUrl=params[3]?params[3]:"empty.dat";
  12. dlTest(dlUrl,function(){testStatus=2;ulTest(ulUrl,function(){testStatus=3;pingTest(pingUrl,function(){testStatus=4;});});});
  13. }
  14. }
  15. if(params[0]=="abort"){
  16. try{if(xhr)xhr.abort();}catch(e){}
  17. testStatus=5;dlStatus="";ulStatus="";pingStatus="";
  18. }
  19. });
  20. function dlTest(serverURL,done){
  21. var firstTick=true,startT=new Date().getTime(), prevT=new Date().getTime(),prevLoaded=0,speed=0.0;
  22. xhr=new XMLHttpRequest();
  23. xhr.onprogress=function(event){
  24. var instspd=(event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0);
  25. if(isNaN(instspd)||!isFinite(instspd)) return;
  26. if(firstTick){
  27. speed=instspd;
  28. firstTick=false;
  29. }else{
  30. speed=speed*0.9+instspd*0.1;
  31. }
  32. prevLoaded=event.loaded;
  33. prevT=new Date().getTime();
  34. dlStatus=((speed*8)/1048576.0).toFixed(2);
  35. if(((prevT-startT)/1000.0)>15){try{xhr.abort();}catch(e){} xhr=null; done();}
  36. }.bind(this);
  37. xhr.onload=function(){
  38. dlStatus=((speed*8)/1048576.0).toFixed(2);
  39. xhr=null;
  40. done();
  41. }.bind(this);
  42. xhr.onerror=function(){
  43. dlStatus="Fail";
  44. xhr=null;
  45. done();
  46. }.bind(this);
  47. xhr.open("GET", serverURL+"?random="+Math.random(),true);
  48. xhr.send();
  49. }
  50. function ulTest(serverURL,done){
  51. var firstTick=true,startT=new Date().getTime(), prevT=new Date().getTime(),prevLoaded=0,speed=0.0;
  52. xhr=new XMLHttpRequest();
  53. xhr.upload.onprogress=function(event){
  54. var instspd=(event.loaded-prevLoaded)/((new Date().getTime()-prevT)/1000.0);
  55. if(isNaN(instspd)||!isFinite(instspd)) return;
  56. if(firstTick){
  57. firstTick=false;
  58. }else{
  59. speed=speed*0.7+instspd*0.3;
  60. }
  61. prevLoaded=event.loaded;
  62. prevT=new Date().getTime();
  63. ulStatus=((speed*8)/1048576.0).toFixed(2);
  64. if(((prevT-startT)/1000.0)>15){try{xhr.abort();}catch(e){} xhr=null; done();}
  65. }.bind(this);
  66. xhr.onload=function(){
  67. ulStatus=((speed*8)/1048576.0).toFixed(2);
  68. done();
  69. }.bind(this);
  70. xhr.onerror=function(){
  71. ulStatus="Fail";
  72. done();
  73. }.bind(this);
  74. xhr.open("POST", serverURL+"?random="+Math.random(),true);
  75. xhr.send(new ArrayBuffer(10485760));
  76. }
  77. function pingTest(pingUrl,done){
  78. var prevT=null,ping=0.0,i=0;
  79. var doPing=function(){
  80. prevT=new Date().getTime();
  81. xhr=new XMLHttpRequest();
  82. xhr.onload=function(){
  83. if(i==0){
  84. prevT=new Date().getTime();
  85. }else{
  86. var instspd=new Date().getTime()-prevT;
  87. if(i==1) ping=instspd; else ping=ping*0.9+instspd*0.1;
  88. }
  89. pingStatus=ping.toFixed(2);
  90. i++;
  91. if(i<50) doPing(); else done();
  92. }.bind(this);
  93. xhr.onerror=function(){
  94. pingStatus="Fail";
  95. done();
  96. }.bind(this);
  97. xhr.open("GET", pingUrl+"?random="+Math.random(),true);
  98. xhr.send();
  99. }.bind(this);
  100. doPing();
  101. }