load.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <html>
  2. <head>
  3. <title>WebSockets Load Test</title>
  4. </head>
  5. <body>
  6. Host: <input id='host' style='width:100'>&nbsp;
  7. Port: <input id='port' style='width:50'>&nbsp;
  8. Encrypt: <input id='encrypt' type='checkbox'>&nbsp;
  9. Send Delay (ms): <input id='sendDelay' style='width:50' value="100">&nbsp;
  10. <input id='connectButton' type='button' value='Start' style='width:100px'
  11. onclick="connect();">&nbsp;
  12. <br><br>
  13. <table border=1>
  14. <tr>
  15. <th align="right">Packets sent:</th>
  16. <td align="right"><div id='sent'>0</div></td>
  17. </tr><tr>
  18. <th align="right">Good Packets Received:</th>
  19. <td align="right"><div id='received'>0</div></td>
  20. </tr><tr>
  21. <th align="right">Errors (Bad Packets Received:)</th>
  22. <td align="right"><div id='errors'>0</div></td>
  23. </tr>
  24. </table>
  25. <br>
  26. Errors:<br>
  27. <textarea id="error" style="font-size: 9;" cols=80 rows=25></textarea>
  28. </body>
  29. <script>
  30. function error(str) {
  31. console.error(str);
  32. cell = document.getElementById('error');
  33. cell.innerHTML += errors + ": " + str + "\n";
  34. cell.scrollTop = cell.scrollHeight;
  35. }
  36. var host = null, port = null, sendDelay = 0;
  37. var ws = null, update_ref = null, send_ref = null;
  38. var sent = 0, received = 0, errors = 0;
  39. var max_send = 2000;
  40. var recv_seq = 0, send_seq = 0;
  41. Array.prototype.pushStr = function (str) {
  42. var n = str.length;
  43. for (var i=0; i < n; i++) {
  44. this.push(str.charCodeAt(i));
  45. }
  46. }
  47. function add (x,y) {
  48. return parseInt(x,10)+parseInt(y,10);
  49. }
  50. function check_respond(data) {
  51. //console.log(">> check_respond");
  52. var first, last, str, length, chksum, nums, arr;
  53. first = String.fromCharCode(data[0]);
  54. last = String.fromCharCode(data[data.length-1]);
  55. if (first != "^") {
  56. errors++;
  57. error("Packet missing start char '^'");
  58. return;
  59. }
  60. if (last != "$") {
  61. errors++;
  62. error("Packet missing end char '$'");
  63. return;
  64. }
  65. text = ''
  66. for (var i = 1; i < data.length-1; i++) {
  67. text += String.fromCharCode(data[i]);
  68. }
  69. arr = text.split(':');
  70. seq = arr[0];
  71. length = arr[1];
  72. chksum = arr[2];
  73. nums = arr[3];
  74. //console.log(" length:" + length + " chksum:" + chksum + " nums:" + nums);
  75. if (seq != recv_seq) {
  76. errors++;
  77. error("Expected seq " + recv_seq + " but got " + seq);
  78. recv_seq = parseInt(seq,10) + 1; // Back on track
  79. return;
  80. }
  81. recv_seq++;
  82. if (nums.length != length) {
  83. errors++;
  84. error("Expected length " + length + " but got " + nums.length);
  85. return;
  86. }
  87. //real_chksum = nums.reduce(add);
  88. real_chksum = 0;
  89. for (var i=0; i < nums.length; i++) {
  90. real_chksum += parseInt(nums.charAt(i), 10);
  91. }
  92. if (real_chksum != chksum) {
  93. errors++
  94. error("Expected chksum " + chksum + " but real chksum is " + real_chksum);
  95. return;
  96. }
  97. received++;
  98. //console.log(" Packet checks out: length:" + length + " chksum:" + chksum);
  99. //console.log("<< check_respond");
  100. }
  101. function send() {
  102. var length = Math.floor(Math.random()*(max_send-9)) + 10; // 10 - max_send
  103. var numlist = [], arr = [];
  104. for (var i=0; i < length; i++) {
  105. numlist.push( Math.floor(Math.random()*10) );
  106. }
  107. //chksum = numlist.reduce(add);
  108. chksum = 0;
  109. for (var i=0; i < numlist.length; i++) {
  110. chksum += parseInt(numlist[i], 10);
  111. }
  112. var nums = numlist.join('');
  113. arr.pushStr("^" + send_seq + ":" + length + ":" + chksum + ":" + nums + "$")
  114. send_seq ++;
  115. ws.send(new Uint8Array(arr));
  116. sent++;
  117. }
  118. function update_stats() {
  119. document.getElementById('sent').innerHTML = sent;
  120. document.getElementById('received').innerHTML = received;
  121. document.getElementById('errors').innerHTML = errors;
  122. }
  123. function init_ws() {
  124. console.log(">> init_ws");
  125. var scheme = "ws://";
  126. if (document.getElementById('encrypt').checked) {
  127. scheme = "wss://";
  128. }
  129. var uri = scheme + host + ":" + port;
  130. console.log("connecting to " + uri);
  131. ws = new WebSocket(uri);
  132. ws.binaryType = 'arraybuffer';
  133. ws.addEventListener('message', function(e) {
  134. //console.log(">> WebSockets.onmessage");
  135. arr = new Uint8Array(e.data);
  136. check_respond(arr);
  137. //console.log("<< WebSockets.onmessage");
  138. });
  139. ws.addEventListener('open', function() {
  140. console.log(">> WebSockets.onopen");
  141. send_ref = setInterval(send, sendDelay);
  142. console.log("<< WebSockets.onopen");
  143. });
  144. ws.addEventListener('close', function(e) {
  145. console.log(">> WebSockets.onclose");
  146. clearInterval(send_ref);
  147. console.log("<< WebSockets.onclose");
  148. });
  149. ws.addEventListener('error', function(e) {
  150. console.log(">> WebSockets.onerror");
  151. console.log(" " + e);
  152. console.log("<< WebSockets.onerror");
  153. });
  154. console.log("<< init_ws");
  155. }
  156. function connect() {
  157. console.log(">> connect");
  158. host = document.getElementById('host').value;
  159. port = document.getElementById('port').value;
  160. sendDelay = parseInt(document.getElementById('sendDelay').value, 10);
  161. if ((!host) || (!port)) {
  162. console.log("must set host and port");
  163. return;
  164. }
  165. if (ws) {
  166. ws.close();
  167. }
  168. init_ws();
  169. update_ref = setInterval(update_stats, 1);
  170. document.getElementById('connectButton').value = "Stop";
  171. document.getElementById('connectButton').onclick = disconnect;
  172. console.log("<< connect");
  173. }
  174. function disconnect() {
  175. console.log(">> disconnect");
  176. if (ws) {
  177. ws.close();
  178. }
  179. clearInterval(update_ref);
  180. update_stats(); // Final numbers
  181. recv_seq = 0;
  182. send_seq = 0;
  183. document.getElementById('connectButton').value = "Start";
  184. document.getElementById('connectButton').onclick = connect;
  185. console.log("<< disconnect");
  186. }
  187. window.onload = function() {
  188. console.log("onload");
  189. var url = document.location.href;
  190. document.getElementById('host').value = (url.match(/host=([^&#]*)/) || ['',window.location.hostname])[1];
  191. document.getElementById('port').value = (url.match(/port=([^&#]*)/) || ['',window.location.port])[1];
  192. }
  193. </script>
  194. </html>