index.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="apple-mobile-web-app-capable" content="yes" />
  5. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1" />
  6. <meta charset="UTF-8">
  7. <meta name="theme-color" content="#4b75ff">
  8. <link rel="stylesheet" href="../script/semantic/semantic.min.css">
  9. <script src="../script/jquery.min.js"></script>
  10. <script src="../script/ao_module.js"></script>
  11. <script src="../script/semantic/semantic.min.js"></script>
  12. <title>WebSocket Test</title>
  13. </head>
  14. <body>
  15. <br><br>
  16. <div class="ui container">
  17. <div class="ui three statistics" id="stat">
  18. <div class="statistic">
  19. <div class="value" id="dl" style="padding: 5px;">
  20. -
  21. </div>
  22. <div class="label" id="dll">
  23. Download
  24. </div>
  25. </div>
  26. <div class="statistic">
  27. <div class="value" id="ul" style="padding: 5px;">
  28. -
  29. </div>
  30. <div class="label" id="ull">
  31. Upload
  32. </div>
  33. </div>
  34. <div class="statistic">
  35. <div class="value" id="ping" style="padding: 5px;">
  36. -
  37. </div>
  38. <div class="label" id="pingl">
  39. Ping
  40. </div>
  41. </div>
  42. </div>
  43. <br>
  44. <div>
  45. <div class="ui top attached teal progress" id="progressbar" style="display: none">
  46. <div class="bar">
  47. <div class="progress"></div>
  48. </div>
  49. </div>
  50. <button class="ui fluid button" onclick="startBench();" id="startbtn">Test</button>
  51. </div>
  52. </div>
  53. </body>
  54. <script>
  55. ao_module_setFixedWindowSize();
  56. autoResize();
  57. $('#progressbar').progress({
  58. label: 'ratio',
  59. text: {
  60. ratio: ''
  61. },
  62. total: 100,
  63. percent: 0
  64. });
  65. $(window).on('resize', function() {
  66. autoResize();
  67. });
  68. function autoResize() {
  69. if ($(window).width() < 550) {
  70. $("#stat").attr("class", "ui three horizontal statistics");
  71. } else {
  72. $("#stat").attr("class", "ui three statistics");
  73. }
  74. }
  75. function startBench() {
  76. $('#progressbar').removeAttr('style');
  77. $('#progressbar').progress('set progress', 0);
  78. $("#startbtn").attr('disabled', 'disabled');
  79. $("#startbtn").html('Testing...');
  80. downloadBench();
  81. }
  82. function downloadBench() {
  83. $("#dl").text("⏱️");
  84. let socket = new WebSocket(getWSEndpoint() + "/system/ajgi/interface?script=Speedtest/special/wspeedtest.js");
  85. socket.onopen = function(e) {
  86. $("#dl").text("⌛");
  87. socket.send("DWL");
  88. $('#progressbar').progress('set progress', 0);
  89. };
  90. socket.onmessage = function(event) {
  91. if (event.data.indexOf("DATA:") == -1) {
  92. if (event.data.indexOf("TTL_BANDWIDTH") != -1) {
  93. let bandwidth = event.data.split("=")[1].split(" ");
  94. $("#dl").text(bandwidth[0]);
  95. $("#dll").text("Download (" + bandwidth[1] + ")");
  96. uploadBench();
  97. }
  98. if (event.data.indexOf("TIME_DIFF") != -1) {
  99. let timediff = parseInt(event.data.split("=")[1]);
  100. $('#progressbar').progress('increment', timediff / 5 * 100);
  101. }
  102. }
  103. };
  104. socket.onerror = function(error) {
  105. if ($("#dl").text() != "⌛") {
  106. $("#dl").text("❌");
  107. }
  108. };
  109. }
  110. function uploadBench() {
  111. $("#ul").text("⏱️");
  112. let socket = new WebSocket(getWSEndpoint() + "/system/ajgi/interface?script=Speedtest/special/wspeedtest.js");
  113. socket.onopen = function(e) {
  114. $("#ul").text("⌛");
  115. socket.send("UPL");
  116. setTimeout(executeULTest, 1000, socket);
  117. };
  118. socket.onerror = function(error) {
  119. if ($("#ul").text() != "⌛") {
  120. $("#ul").text("❌");
  121. }
  122. };
  123. }
  124. function executeULTest(socket) {
  125. $('#progressbar').progress('set progress', 0);
  126. let CurrentMB = 0;
  127. let start = new Date();
  128. while (socket.bufferedAmount < 10485760 * 10) {
  129. socket.send(new ArrayBuffer(32768));
  130. CurrentMB += 32768;
  131. }
  132. let checker = setInterval(function() {
  133. if (socket.bufferedAmount == 0) {
  134. let end = new Date();
  135. CurrentDif = (end.getTime() - start.getTime()) / 1000;
  136. socket.send("stop");
  137. let bandwidth = bytesToSpeed(CurrentMB / CurrentDif).split(" ");
  138. $("#ul").text(bandwidth[0]);
  139. $("#ull").text("Upload (" + bandwidth[1] + ")");
  140. $('#progressbar').progress('set progress', 0);
  141. clearInterval(checker);
  142. pingTest();
  143. } else {
  144. //(1 - (socket.bufferedAmount / (10485760 * 10))) * 100
  145. $('#progressbar').progress('set progress', 100 - (socket.bufferedAmount / 1048576));
  146. }
  147. }, 100);
  148. }
  149. function pingTest() {
  150. $('#progressbar').progress('set progress', 0);
  151. let clientSendTimeStamp = [];
  152. let serverResponseTimeStamp = [];
  153. let clientReceiveTimeStamp = [];
  154. $("#ping").text("⏱️");
  155. let socket = new WebSocket(getWSEndpoint() + "/system/ajgi/interface?script=Speedtest/special/wspeedtest.js");
  156. socket.onopen = function(e) {
  157. $("#ping").text("⌛");
  158. socket.send("PING");
  159. setTimeout(executePingTest, 500, socket, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp, 0);
  160. };
  161. socket.onmessage = function(event) {
  162. clientReceiveTimeStamp.push(new Date().getTime());
  163. serverResponseTimeStamp.push(event.data);
  164. if (clientReceiveTimeStamp.length == 5) {
  165. setTimeout(showPingResult, 2000, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp);
  166. }
  167. $('#progressbar').progress('increment', 16.67);
  168. };
  169. socket.onerror = function(error) {
  170. if ($("#ping").text() != "⌛") {
  171. $("#ping").text("❌");
  172. }
  173. };
  174. }
  175. function executePingTest(socket, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp, count) {
  176. let clientTime = new Date();
  177. socket.send(clientTime);
  178. clientSendTimeStamp.push(clientTime.getTime());
  179. count++;
  180. $('#progressbar').progress('increment', 16.67);
  181. if (count < 3) {
  182. setTimeout(executePingTest, 1000, socket, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp, count);
  183. }
  184. }
  185. function showPingResult(clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp) {
  186. serverResponseTimeStamp.shift();
  187. serverResponseTimeStamp.shift();
  188. clientReceiveTimeStamp.shift();
  189. clientReceiveTimeStamp.shift();
  190. let diff = 0;
  191. for (let i = 0; i < clientSendTimeStamp.length; i++) {
  192. diff += (clientReceiveTimeStamp[i] - clientSendTimeStamp[i]) - (serverResponseTimeStamp[i].split(",")[1] - serverResponseTimeStamp[i].split(",")[0]);
  193. }
  194. $("#ping").text(Math.round((diff / clientSendTimeStamp.length) * 100) / 100);
  195. $("#pingl").text("Ping (ms)");
  196. //restart the test
  197. $('#progressbar').attr('style', 'display: none');
  198. $("#startbtn").html('Re-Test');
  199. $("#startbtn").attr('onclick', 'location.reload();');
  200. $("#startbtn").removeAttr('disabled');
  201. }
  202. function getWSEndpoint() {
  203. //Open opeartion in websocket
  204. let protocol = "wss://";
  205. if (location.protocol !== 'https:') {
  206. protocol = "ws://";
  207. }
  208. wsControlEndpoint = (protocol + window.location.hostname + ":" + window.location.port);
  209. return wsControlEndpoint;
  210. }
  211. //https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
  212. function bytesToSize(bytes) {
  213. let sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  214. if (bytes == 0) return '0 Byte';
  215. let i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  216. return Math.round((bytes / Math.pow(1024, i)) * 100, 3) / 100 + ' ' + sizes[i];
  217. }
  218. function bytesToSpeed(bytes) {
  219. bytes = bytes * 8;
  220. let sizes = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps'];
  221. if (bytes == 0) return '0 Byte';
  222. let i = parseInt(Math.floor(Math.log(bytes) / Math.log(1000)));
  223. return Math.round((bytes / Math.pow(1000, i)) * 100, 3) / 100 + ' ' + sizes[i];
  224. }
  225. </script>
  226. </html>