speedtest.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. function DownloadTester(serverURL,done,update,err){
  2. this.xhr=new XMLHttpRequest();
  3. this.firstTick=true;
  4. this.prevLoaded=0;
  5. this.startT=new Date().getTime();
  6. this.prevT=new Date().getTime();
  7. this.speed=0.0;
  8. if(done)this.onDone=done; if(update)this.onUpdate=update; if(err)this.onFail=err;
  9. this.xhr.onprogress=function(event){
  10. var instspd=(event.loaded-this.prevLoaded)/((new Date().getTime()-this.prevT)/1000.0);
  11. if(isNaN(instspd)||!isFinite(instspd)) return;
  12. if(this.firstTick){
  13. this.speed=instspd;
  14. this.firstTick=false;
  15. }else{
  16. this.speed=this.speed*0.9+instspd*0.1;
  17. }
  18. this.prevLoaded=event.loaded;
  19. this.prevT=new Date().getTime();
  20. this.onUpdate();
  21. if(((this.prevT-this.startT)/1000.0)>15){try{this.xhr.abort();}catch(e){} this.onDone();}
  22. }.bind(this);
  23. this.xhr.onload=function(){
  24. this.onUpdate();
  25. this.onDone();
  26. }.bind(this);
  27. this.xhr.onerror=function(){
  28. this.onUpdate();
  29. this.onFail();
  30. }.bind(this);
  31. this.xhr.open("GET", serverURL+"?random="+Math.random(),true);
  32. this.xhr.send();
  33. }
  34. DownloadTester.prototype={
  35. constructor:DownloadTester,
  36. onDone:function(){},
  37. onFail:function(){},
  38. onUpdate:function(){},
  39. getValue:function(){return ((this.speed*8)/1048576.0).toFixed(2);},
  40. cancel:function(){try{this.xhr.abort();}catch(e){}}
  41. }
  42. function UploadTester(serverURL,done,update,err){
  43. this.xhr=new XMLHttpRequest();
  44. this.firstTick=true;
  45. this.prevLoaded=0;
  46. this.startT=new Date().getTime();
  47. this.prevT=new Date().getTime();
  48. this.speed=0.0;
  49. if(done)this.onDone=done; if(update)this.onUpdate=update; if(err)this.onFail=err;
  50. this.xhr.upload.onprogress=function(event){
  51. var instspd=(event.loaded-this.prevLoaded)/((new Date().getTime()-this.prevT)/1000.0);
  52. if(isNaN(instspd)||!isFinite(instspd)) return;
  53. if(this.firstTick){
  54. this.firstTick=false;
  55. }else{
  56. this.speed=this.speed*0.7+instspd*0.3;
  57. }
  58. this.prevLoaded=event.loaded;
  59. this.prevT=new Date().getTime();
  60. this.onUpdate();
  61. if(((this.prevT-this.startT)/1000.0)>15){try{this.xhr.abort();}catch(e){} this.onDone();}
  62. }.bind(this);
  63. this.xhr.onload=function(){
  64. this.onUpdate();
  65. this.onDone();
  66. }.bind(this);
  67. this.xhr.onerror=function(){
  68. this.onUpdate();
  69. this.onFail();
  70. }.bind(this);
  71. this.xhr.open("POST", serverURL+"?random="+Math.random(),true);
  72. this.xhr.send(new ArrayBuffer(10485760));
  73. }
  74. UploadTester.prototype={
  75. constructor:UploadTester,
  76. onDone:function(){},
  77. onFail:function(){},
  78. onUpdate:function(){},
  79. getValue:function(){return ((this.speed*8)/1048576.0).toFixed(2);},
  80. cancel:function(){try{this.xhr.abort();}catch(e){}}
  81. }
  82. function PingTester(serverURL,done,update,err){
  83. this.xhr=null;
  84. this.prevT=null;
  85. this.ping=0.0;
  86. this.i=0;
  87. this.pingURL=serverURL;
  88. if(done)this.onDone=done;
  89. if(update)this.onUpdate=update;
  90. if(err)this.onFail=err;
  91. this.doPing=function(){
  92. this.prevT=new Date().getTime();
  93. this.xhr=new XMLHttpRequest();
  94. this.xhr.onload=function(){
  95. if(this.i==0){
  96. this.prevT=new Date().getTime();
  97. }else{
  98. var instspd=new Date().getTime()-this.prevT;
  99. if(this.i==1) this.ping=instspd; else this.ping=this.ping*0.9+instspd*0.1;
  100. }
  101. this.onUpdate();
  102. this.i++;
  103. if(this.i<50) this.doPing(); else this.onDone();
  104. }.bind(this);
  105. this.xhr.onerror=function(){
  106. this.onUpdate();
  107. this.onFail();
  108. }.bind(this);
  109. this.xhr.open("GET", this.pingURL+"?random="+Math.random(),true);
  110. this.xhr.send();
  111. }.bind(this);
  112. this.doPing();
  113. }
  114. PingTester.prototype={
  115. constructor:PingTester,
  116. onDone:function(){},
  117. onFail:function(){},
  118. onUpdate:function(){},
  119. getValue:function(){return this.ping.toFixed(2);},
  120. cancel:function(){this.i=9999; if(this.xhr) try{xhr.abort();}catch(e){}}
  121. }