~ubuntu-branches/ubuntu/trusty/websockify/trusty-updates

« back to all changes in this revision

Viewing changes to tests/load.html

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2013-02-23 01:22:51 UTC
  • Revision ID: package-import@ubuntu.com-20130223012251-3qkk1n1p93kb3j87
Tags: upstream-0.3.0+dfsg1
ImportĀ upstreamĀ versionĀ 0.3.0+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html>
 
2
 
 
3
    <head>
 
4
        <title>WebSockets Load Test</title>
 
5
        <script src="include/util.js"></script>
 
6
        <script src="include/webutil.js"></script> 
 
7
        <script src="include/base64.js"></script>
 
8
        <script src="include/websock.js"></script> 
 
9
        <!-- Uncomment to activate firebug lite -->
 
10
        <!--
 
11
        <script type='text/javascript' 
 
12
            src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
 
13
        -->
 
14
 
 
15
 
 
16
    </head>
 
17
 
 
18
    <body>
 
19
 
 
20
        Host: <input id='host' style='width:100'>&nbsp;
 
21
        Port: <input id='port' style='width:50'>&nbsp;
 
22
        Encrypt: <input id='encrypt' type='checkbox'>&nbsp;
 
23
        Send Delay (ms): <input id='sendDelay' style='width:50' value="100">&nbsp;
 
24
        <input id='connectButton' type='button' value='Start' style='width:100px'
 
25
            onclick="connect();">&nbsp;
 
26
 
 
27
        <br><br>
 
28
        <table border=1>
 
29
            <tr>
 
30
                <th align="right">Packets sent:</th>
 
31
                <td align="right"><div id='sent'>0</div></td>
 
32
            </tr><tr>
 
33
                <th align="right">Good Packets Received:</th>
 
34
                <td align="right"><div id='received'>0</div></td>
 
35
            </tr><tr>
 
36
                <th align="right">Errors (Bad Packets Received:)</th>
 
37
                <td align="right"><div id='errors'>0</div></td>
 
38
            </tr>
 
39
        </table>
 
40
 
 
41
        <br>
 
42
        Errors:<br>
 
43
        <textarea id="error" style="font-size: 9;" cols=80 rows=25></textarea>
 
44
    </body>
 
45
 
 
46
 
 
47
    <script>
 
48
 
 
49
        function error(str) {
 
50
            console.error(str);
 
51
            cell = $D('error');
 
52
            cell.innerHTML += errors + ": " + str + "\n";
 
53
            cell.scrollTop = cell.scrollHeight;
 
54
        }
 
55
 
 
56
        var host = null, port = null, sendDelay = 0;
 
57
        var ws = null, update_ref = null, send_ref = null;
 
58
        var sent = 0, received = 0, errors = 0;
 
59
        var max_send = 2000;
 
60
        var recv_seq = 0, send_seq = 0;
 
61
 
 
62
        Array.prototype.pushStr = function (str) {
 
63
            var n = str.length;
 
64
            for (var i=0; i < n; i++) {
 
65
                this.push(str.charCodeAt(i));
 
66
            }
 
67
        }
 
68
 
 
69
 
 
70
 
 
71
        function add (x,y) {
 
72
            return parseInt(x,10)+parseInt(y,10);
 
73
        }
 
74
 
 
75
        function check_respond(data) {
 
76
            //console.log(">> check_respond");
 
77
            var first, last, str, length, chksum, nums, arr;
 
78
            first = String.fromCharCode(data.shift());
 
79
            last = String.fromCharCode(data.pop());
 
80
 
 
81
            if (first != "^") {
 
82
                errors++;
 
83
                error("Packet missing start char '^'");
 
84
                return;
 
85
            }
 
86
            if (last != "$") {
 
87
                errors++;
 
88
                error("Packet missing end char '$'");
 
89
                return;
 
90
            }
 
91
            arr = data.map(function(num) {
 
92
                    return String.fromCharCode(num); 
 
93
                } ).join('').split(':');
 
94
            seq    = arr[0];
 
95
            length = arr[1];
 
96
            chksum = arr[2];
 
97
            nums   = arr[3];
 
98
 
 
99
            //console.log("   length:" + length + " chksum:" + chksum + " nums:" + nums);
 
100
            if (seq != recv_seq) {
 
101
                errors++;
 
102
                error("Expected seq " + recv_seq + " but got " + seq);
 
103
                recv_seq = parseInt(seq,10) + 1;   // Back on track
 
104
                return;
 
105
            }
 
106
            recv_seq++;
 
107
            if (nums.length != length) {
 
108
                errors++;
 
109
                error("Expected length " + length + " but got " + nums.length);
 
110
                return;
 
111
            }
 
112
            //real_chksum = nums.reduce(add);
 
113
            real_chksum = 0;
 
114
            for (var i=0; i < nums.length; i++) {
 
115
                real_chksum += parseInt(nums.charAt(i), 10);
 
116
            }
 
117
            if (real_chksum != chksum) {
 
118
                errors++
 
119
                error("Expected chksum " + chksum + " but real chksum is " + real_chksum);
 
120
                return;
 
121
            }
 
122
            received++;
 
123
            //console.log("   Packet checks out: length:" + length + " chksum:" + chksum);
 
124
            //console.log("<< check_respond");
 
125
        }
 
126
 
 
127
        function send() {
 
128
            var length = Math.floor(Math.random()*(max_send-9)) + 10; // 10 - max_send
 
129
            var numlist = [], arr = [];
 
130
            for (var i=0; i < length; i++) {
 
131
                numlist.push( Math.floor(Math.random()*10) );
 
132
            }
 
133
            //chksum = numlist.reduce(add);
 
134
            chksum = 0;
 
135
            for (var i=0; i < numlist.length; i++) {
 
136
                chksum += parseInt(numlist[i], 10);
 
137
            }
 
138
            var nums = numlist.join('');
 
139
            arr.pushStr("^" + send_seq + ":" + length + ":" + chksum + ":" + nums + "$")
 
140
            send_seq ++;
 
141
            ws.send(arr);
 
142
            sent++;
 
143
        }
 
144
 
 
145
        function update_stats() {
 
146
            $D('sent').innerHTML = sent;
 
147
            $D('received').innerHTML = received;
 
148
            $D('errors').innerHTML = errors;
 
149
        }
 
150
 
 
151
        function init_ws() {
 
152
            console.log(">> init_ws");
 
153
            var scheme = "ws://";
 
154
            if ($D('encrypt').checked) {
 
155
                scheme = "wss://";
 
156
            }
 
157
            var uri = scheme + host + ":" + port;
 
158
            console.log("connecting to " + uri);
 
159
            ws = new Websock();
 
160
            ws.open(uri);
 
161
 
 
162
            ws.on('message', function() {
 
163
                //console.log(">> WebSockets.onmessage");
 
164
                arr = ws.rQshiftBytes(ws.rQlen());
 
165
                check_respond(arr);
 
166
                //console.log("<< WebSockets.onmessage");
 
167
            });
 
168
            ws.on('open', function() {
 
169
                console.log(">> WebSockets.onopen");
 
170
                send_ref = setInterval(send, sendDelay);
 
171
                console.log("<< WebSockets.onopen");
 
172
            });
 
173
            ws.on('close', function(e) {
 
174
                console.log(">> WebSockets.onclose");
 
175
                clearInterval(send_ref);
 
176
                console.log("<< WebSockets.onclose");
 
177
            });
 
178
            ws.on('error', function(e) {
 
179
                console.log(">> WebSockets.onerror");
 
180
                console.log("   " + e);
 
181
                console.log("<< WebSockets.onerror");
 
182
            });
 
183
 
 
184
            console.log("<< init_ws");
 
185
        }
 
186
 
 
187
        function connect() {
 
188
            console.log(">> connect");
 
189
            host = $D('host').value;
 
190
            port = $D('port').value;
 
191
            sendDelay = parseInt($D('sendDelay').value, 10);
 
192
            if ((!host) || (!port)) {
 
193
                console.log("must set host and port");
 
194
                return;
 
195
            }
 
196
 
 
197
            if (ws) {
 
198
                ws.close();
 
199
            }
 
200
            init_ws();
 
201
            update_ref = setInterval(update_stats, 1);
 
202
 
 
203
            $D('connectButton').value = "Stop";
 
204
            $D('connectButton').onclick = disconnect;
 
205
            console.log("<< connect");
 
206
        }
 
207
 
 
208
        function disconnect() {
 
209
            console.log(">> disconnect");
 
210
            if (ws) {
 
211
                ws.close();
 
212
            }
 
213
 
 
214
            clearInterval(update_ref);
 
215
            update_stats(); // Final numbers
 
216
            recv_seq = 0;
 
217
            send_seq = 0;
 
218
 
 
219
            $D('connectButton').value = "Start";
 
220
            $D('connectButton').onclick = connect;
 
221
            console.log("<< disconnect");
 
222
        }
 
223
 
 
224
 
 
225
        /* If no builtin websockets then load web_socket.js */
 
226
        if (window.WebSocket) {
 
227
            VNC_native_ws = true;
 
228
        } else {
 
229
            VNC_native_ws = false;
 
230
            console.log("Loading web-socket-js flash bridge");
 
231
            var extra = "<script src='include/web-socket-js/swfobject.js'><\/script>";
 
232
            extra += "<script src='include/web-socket-js/FABridge.js'><\/script>";
 
233
            extra += "<script src='include/web-socket-js/web_socket.js'><\/script>";
 
234
            document.write(extra);
 
235
        }
 
236
 
 
237
        window.onload = function() {
 
238
            console.log("onload");
 
239
            if (!VNC_native_ws) {
 
240
                console.log("initializing web-socket-js flash bridge");
 
241
                WebSocket.__swfLocation = "include/web-socket-js/WebSocketMain.swf";
 
242
                WebSocket.__initialize();
 
243
            }
 
244
            var url = document.location.href;
 
245
            $D('host').value = (url.match(/host=([^&#]*)/) || ['',''])[1];
 
246
            $D('port').value = (url.match(/port=([^&#]*)/) || ['',''])[1];
 
247
        }
 
248
    </script>
 
249
 
 
250
</html>