index.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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">
  18. <div class="statistic">
  19. <div class="value" id="dl">
  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">
  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">
  36. -
  37. </div>
  38. <div class="label" id="pingl">
  39. Ping
  40. </div>
  41. </div>
  42. </div>
  43. <br>
  44. <button class="ui fluid button" onclick="startBench();">Test</button>
  45. </div>
  46. </body>
  47. <script>
  48. ao_module_setFixedWindowSize();
  49. function startBench() {
  50. downloadBench();
  51. }
  52. function downloadBench() {
  53. $("#dl").text("⏱️");
  54. let socket = new WebSocket(getWSEndpoint() + "/system/ajgi/interface?script=Speedtest/special/wspeedtest.js");
  55. socket.onopen = function(e) {
  56. $("#dl").text("⌛");
  57. socket.send("DWL");
  58. };
  59. socket.onmessage = function(event) {
  60. if (event.data.indexOf("DATA:") == -1) {
  61. if (event.data.indexOf("TTL_BANDWIDTH") != -1) {
  62. var bandwidth = event.data.split("=")[1].split(" ");
  63. $("#dl").text(bandwidth[0]);
  64. $("#dll").text("Download (" + bandwidth[1] + ")");
  65. uploadBench();
  66. }
  67. }
  68. };
  69. socket.onerror = function(error) {
  70. $("#dl").text("❌");
  71. };
  72. }
  73. function uploadBench() {
  74. $("#ul").text("⏱️");
  75. let socket = new WebSocket(getWSEndpoint() + "/system/ajgi/interface?script=Speedtest/special/wspeedtest.js");
  76. socket.onopen = function(e) {
  77. $("#ul").text("⌛");
  78. socket.send("UPL");
  79. var randomStr = rnd(32768);
  80. var filesize = "DATA:".length + randomStr.length;
  81. setTimeout(executeULTest, 1000, socket, 0, 0, randomStr, filesize);
  82. };
  83. socket.onerror = function(error) {
  84. $("#ul").text("❌");
  85. };
  86. }
  87. function executeULTest(socket, CurrentPow, CurrentDif, randomStr, filesize) {
  88. var CurrentMB = Math.pow(2, CurrentPow);
  89. var start = new Date();
  90. for (var i = 0; i < CurrentMB; i++) {
  91. socket.send("DATA:" + randomStr);
  92. }
  93. var end = new Date();
  94. CurrentDif = (end.getTime() - start.getTime()) / 1000;
  95. CurrentPow++;
  96. if (CurrentDif < 5) {
  97. setTimeout(executeULTest, 1000, socket, CurrentPow, CurrentDif, randomStr, filesize);
  98. } else {
  99. socket.send("stop");
  100. var bandwidth = bytesToSpeed(CurrentMB * filesize / CurrentDif).split(" ");
  101. $("#ul").text(bandwidth[0]);
  102. $("#ull").text("Upload (" + bandwidth[1] + ")");
  103. pingTest();
  104. }
  105. }
  106. function pingTest() {
  107. var clientSendTimeStamp = [];
  108. var serverResponseTimeStamp = [];
  109. var clientReceiveTimeStamp = [];
  110. $("#ping").text("⏱️");
  111. let socket = new WebSocket(getWSEndpoint() + "/system/ajgi/interface?script=Speedtest/special/wspeedtest.js");
  112. socket.onopen = function(e) {
  113. $("#ping").text("⌛");
  114. socket.send("PING");
  115. setTimeout(executePingTest, 500, socket, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp, 0);
  116. };
  117. socket.onmessage = function(event) {
  118. clientReceiveTimeStamp.push(new Date().getTime());
  119. serverResponseTimeStamp.push(event.data);
  120. };
  121. socket.onerror = function(error) {
  122. $("#ping").text("❌");
  123. };
  124. }
  125. function executePingTest(socket, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp, count) {
  126. var clientTime = new Date();
  127. socket.send(clientTime);
  128. clientSendTimeStamp.push(clientTime.getTime());
  129. count++;
  130. console.log(count);
  131. if (count < 3) {
  132. setTimeout(executePingTest, 1000, socket, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp, count);
  133. } else {
  134. setTimeout(showPingResult, 2000, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp);
  135. }
  136. }
  137. function showPingResult(clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp) {
  138. console.log(clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp);
  139. serverResponseTimeStamp.shift();
  140. serverResponseTimeStamp.shift();
  141. clientReceiveTimeStamp.shift();
  142. clientReceiveTimeStamp.shift();
  143. var diff = 0;
  144. for (var i = 0; i < clientSendTimeStamp.length; i++) {
  145. diff += (clientReceiveTimeStamp[i] - clientSendTimeStamp[i]) - (serverResponseTimeStamp[i].split(",")[1] - serverResponseTimeStamp[i].split(",")[0]);
  146. }
  147. $("#ping").text(Math.round((diff / clientSendTimeStamp.length) * 100) / 100);
  148. $("#pingl").text("Ping (ms)");
  149. }
  150. function getWSEndpoint() {
  151. //Open opeartion in websocket
  152. let protocol = "wss://";
  153. if (location.protocol !== 'https:') {
  154. protocol = "ws://";
  155. }
  156. wsControlEndpoint = (protocol + window.location.hostname + ":" + window.location.port);
  157. return wsControlEndpoint;
  158. }
  159. //https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript
  160. function rnd(length) {
  161. var result = '';
  162. var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  163. var charactersLength = characters.length;
  164. for (var i = 0; i < length; i++) {
  165. result += characters.charAt(Math.floor(Math.random() *
  166. charactersLength));
  167. }
  168. return result;
  169. }
  170. //https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
  171. function bytesToSize(bytes) {
  172. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  173. if (bytes == 0) return '0 Byte';
  174. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  175. return Math.round((bytes / Math.pow(1024, i)) * 100, 3) / 100 + ' ' + sizes[i];
  176. }
  177. function bytesToSpeed(bytes) {
  178. bytes = bytes * 8;
  179. var sizes = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps'];
  180. if (bytes == 0) return '0 Byte';
  181. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1000)));
  182. return Math.round((bytes / Math.pow(1000, i)) * 100, 3) / 100 + ' ' + sizes[i];
  183. }
  184. </script>
  185. </html>