summaryrefslogtreecommitdiff
path: root/tests/load.html
blob: cb550416e0f76ce3cccde934307504f452e0e177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<html>

    <head>
        <title>WebSockets Load Test</title>
    </head>

    <body>

        Host: <input id='host' style='width:100'>&nbsp;
        Port: <input id='port' style='width:50'>&nbsp;
        Encrypt: <input id='encrypt' type='checkbox'>&nbsp;
        Send Delay (ms): <input id='sendDelay' style='width:50' value="100">&nbsp;
        <input id='connectButton' type='button' value='Start' style='width:100px'
            onclick="connect();">&nbsp;

        <br><br>
        <table border=1>
            <tr>
                <th align="right">Packets sent:</th>
                <td align="right"><div id='sent'>0</div></td>
            </tr><tr>
                <th align="right">Good Packets Received:</th>
                <td align="right"><div id='received'>0</div></td>
            </tr><tr>
                <th align="right">Errors (Bad Packets Received:)</th>
                <td align="right"><div id='errors'>0</div></td>
            </tr>
        </table>

        <br>
        Errors:<br>
        <textarea id="error" style="font-size: 9;" cols=80 rows=25></textarea>
    </body>


    <script>

        function error(str) {
            console.error(str);
            cell = document.getElementById('error');
            cell.innerHTML += errors + ": " + str + "\n";
            cell.scrollTop = cell.scrollHeight;
        }

        var host = null, port = null, sendDelay = 0;
        var ws = null, update_ref = null, send_ref = null;
        var sent = 0, received = 0, errors = 0;
        var max_send = 2000;
        var recv_seq = 0, send_seq = 0;

        Array.prototype.pushStr = function (str) {
            var n = str.length;
            for (var i=0; i < n; i++) {
                this.push(str.charCodeAt(i));
            }
        }



        function add (x,y) {
            return parseInt(x,10)+parseInt(y,10);
        }

        function check_respond(data) {
            //console.log(">> check_respond");
            var first, last, str, length, chksum, nums, arr;
            first = String.fromCharCode(data[0]);
            last = String.fromCharCode(data[data.length-1]);

            if (first != "^") {
                errors++;
                error("Packet missing start char '^'");
                return;
            }
            if (last != "$") {
                errors++;
                error("Packet missing end char '$'");
                return;
            }
            text = ''
            for (var i = 1; i < data.length-1; i++) {
                text += String.fromCharCode(data[i]);
            }
            arr = text.split(':');
            seq    = arr[0];
            length = arr[1];
            chksum = arr[2];
            nums   = arr[3];

            //console.log("   length:" + length + " chksum:" + chksum + " nums:" + nums);
            if (seq != recv_seq) {
                errors++;
                error("Expected seq " + recv_seq + " but got " + seq);
                recv_seq = parseInt(seq,10) + 1;   // Back on track
                return;
            }
            recv_seq++;
            if (nums.length != length) {
                errors++;
                error("Expected length " + length + " but got " + nums.length);
                return;
            }
            //real_chksum = nums.reduce(add);
            real_chksum = 0;
            for (var i=0; i < nums.length; i++) {
                real_chksum += parseInt(nums.charAt(i), 10);
            }
            if (real_chksum != chksum) {
                errors++
                error("Expected chksum " + chksum + " but real chksum is " + real_chksum);
                return;
            }
            received++;
            //console.log("   Packet checks out: length:" + length + " chksum:" + chksum);
            //console.log("<< check_respond");
        }

        function send() {
            var length = Math.floor(Math.random()*(max_send-9)) + 10; // 10 - max_send
            var numlist = [], arr = [];
            for (var i=0; i < length; i++) {
                numlist.push( Math.floor(Math.random()*10) );
            }
            //chksum = numlist.reduce(add);
            chksum = 0;
            for (var i=0; i < numlist.length; i++) {
                chksum += parseInt(numlist[i], 10);
            }
            var nums = numlist.join('');
            arr.pushStr("^" + send_seq + ":" + length + ":" + chksum + ":" + nums + "$")
            send_seq ++;
            ws.send(new Uint8Array(arr));
            sent++;
        }

        function update_stats() {
            document.getElementById('sent').innerHTML = sent;
            document.getElementById('received').innerHTML = received;
            document.getElementById('errors').innerHTML = errors;
        }

        function init_ws() {
            console.log(">> init_ws");
            var scheme = "ws://";
            if (document.getElementById('encrypt').checked) {
                scheme = "wss://";
            }
            var uri = scheme + host + ":" + port;
            console.log("connecting to " + uri);
            ws = new WebSocket(uri);
            ws.binaryType = 'arraybuffer';

            ws.addEventListener('message', function(e) {
                //console.log(">> WebSockets.onmessage");
                arr = new Uint8Array(e.data);
                check_respond(arr);
                //console.log("<< WebSockets.onmessage");
            });
            ws.addEventListener('open', function() {
                console.log(">> WebSockets.onopen");
                send_ref = setInterval(send, sendDelay);
                console.log("<< WebSockets.onopen");
            });
            ws.addEventListener('close', function(e) {
                console.log(">> WebSockets.onclose");
                clearInterval(send_ref);
                console.log("<< WebSockets.onclose");
            });
            ws.addEventListener('error', function(e) {
                console.log(">> WebSockets.onerror");
                console.log("   " + e);
                console.log("<< WebSockets.onerror");
            });

            console.log("<< init_ws");
        }

        function connect() {
            console.log(">> connect");
            host = document.getElementById('host').value;
            port = document.getElementById('port').value;
            sendDelay = parseInt(document.getElementById('sendDelay').value, 10);
            if ((!host) || (!port)) {
                console.log("must set host and port");
                return;
            }

            if (ws) {
                ws.close();
            }
            init_ws();
            update_ref = setInterval(update_stats, 1);

            document.getElementById('connectButton').value = "Stop";
            document.getElementById('connectButton').onclick = disconnect;
            console.log("<< connect");
        }

        function disconnect() {
            console.log(">> disconnect");
            if (ws) {
                ws.close();
            }

            clearInterval(update_ref);
            update_stats(); // Final numbers
            recv_seq = 0;
            send_seq = 0;

            document.getElementById('connectButton').value = "Start";
            document.getElementById('connectButton').onclick = connect;
            console.log("<< disconnect");
        }

        window.onload = function() {
            console.log("onload");
            var url = document.location.href;
            document.getElementById('host').value = (url.match(/host=([^&#]*)/) || ['',window.location.hostname])[1];
            document.getElementById('port').value = (url.match(/port=([^&#]*)/) || ['',window.location.port])[1];
        }
    </script>

</html>