wspeedtest.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. if (!requirelib("appdata")) {
  2. console.log("appdata import failed");
  3. }
  4. /*
  5. WebSocket Test Script
  6. This is a special test script and should not be mixed in with normal
  7. AGI module test scripts. Please test this seperately
  8. Author: tobychui
  9. */
  10. function setup() {
  11. //Require the WebSocket Library
  12. var succ = requirelib("websocket");
  13. if (!succ) {
  14. console.log("WebSocket Open Failed");
  15. return false
  16. }
  17. //Upgrade the current connection to WebSocket, set timeout to 30 seconds
  18. //Timeout value: if after 30 seconds nothing has been send / received, the websocket will be closed
  19. //set this value to 0 to display auto socket closing
  20. succ = websocket.upgrade(30);
  21. if (!succ) {
  22. console.log("WebSocket Upgrade Failed");
  23. return false
  24. }
  25. //console.log("WebSocket Opened!")
  26. return true;
  27. }
  28. function closing() {
  29. //Try to close the WebSocket connection
  30. websocket.close();
  31. }
  32. //Start executing the script
  33. if (setup()) {
  34. websocket.send("DWL/UPL?");
  35. var recv = "";
  36. while (true) {
  37. //Read the websocket input from Client (Web UI)
  38. recv = websocket.read();
  39. if (recv == null) {
  40. console.log("Read Failed!")
  41. break;
  42. }
  43. if (recv == "DWL") {
  44. downloadTest();
  45. break;
  46. } else if (recv == "UPL") {
  47. uploadTest();
  48. break;
  49. } else if (recv == "PING") {
  50. pingTest();
  51. break;
  52. }
  53. }
  54. closing();
  55. } else {
  56. console.log("WebSocket Setup Failed.")
  57. }
  58. function downloadTest() {
  59. var CurrentPow = 0;
  60. var CurrentDif = 0;
  61. randomStr = "";
  62. fileRandomStr = appdata.readFile("Speedtest/special/random64KB.txt");
  63. for(var i = 0; i < 16; i++){
  64. randomStr += fileRandomStr;
  65. }
  66. var filesize = "DATA:".length + randomStr.length;
  67. while (CurrentDif < 5) {
  68. var CurrentMB = Math.pow(2, CurrentPow);
  69. var start = new Date();
  70. for (var i = 0; i < CurrentMB; i++) {
  71. websocket.send("DATA:" + randomStr);
  72. }
  73. var end = new Date();
  74. CurrentDif = (end.getTime() - start.getTime()) / 1000;
  75. websocket.send("TIME_DIFF=" + CurrentDif);
  76. CurrentPow++;
  77. }
  78. websocket.send("TTL_SIZE=" + bytesToSize(CurrentMB * filesize));
  79. websocket.send("TTL_TIME=" + CurrentDif + "s");
  80. websocket.send("TTL_BANDWIDTH=" + bytesToSpeed(CurrentMB * filesize / CurrentDif));
  81. }
  82. function uploadTest() {
  83. websocket.send("UPL");
  84. var recv = "";
  85. while (true) {
  86. //Read the websocket input from Client (Web UI)
  87. recv = websocket.read();
  88. if (recv == null) {
  89. console.log("Read Failed!")
  90. break;
  91. }
  92. if (recv == "stop") {
  93. websocket.send("Stopped.");
  94. break;
  95. }
  96. }
  97. }
  98. function pingTest() {
  99. websocket.send("UPL");
  100. var recv = "";
  101. for (var i = 0; i < 3; i++) {
  102. //Read the websocket input from Client (Web UI)
  103. recv = websocket.read();
  104. if (recv == null) {
  105. console.log("Read Failed!")
  106. break;
  107. } else {
  108. var rcvTime = new Date().getTime();
  109. var sendTime = new Date().getTime();
  110. websocket.send(rcvTime + "," + sendTime);
  111. }
  112. if (recv == "stop") {
  113. websocket.send("Stopped.");
  114. break;
  115. }
  116. }
  117. }
  118. //https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript
  119. function rnd(length) {
  120. var result = '';
  121. var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  122. var charactersLength = characters.length;
  123. for (var i = 0; i < length; i++) {
  124. result += characters.charAt(Math.floor(Math.random() *
  125. charactersLength));
  126. }
  127. return result;
  128. }
  129. //https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
  130. function bytesToSize(bytes) {
  131. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  132. if (bytes == 0) return '0 Byte';
  133. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  134. return Math.round((bytes / Math.pow(1024, i)) * 100, 3) / 100 + ' ' + sizes[i];
  135. }
  136. function bytesToSpeed(bytes) {
  137. bytes = bytes * 8;
  138. var sizes = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps'];
  139. if (bytes == 0) return '0 Byte';
  140. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1000)));
  141. return Math.round((bytes / Math.pow(1000, i)) * 100, 3) / 100 + ' ' + sizes[i];
  142. }