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

« back to all changes in this revision

Viewing changes to tests/latency.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 Latency Test</title>
 
5
        <script src="include/base64.js"></script>
 
6
        <script src="include/util.js"></script>
 
7
        <script src="include/webutil.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'>
 
23
        <br>
 
24
        Payload Size: <input id='payload_size' style='width:50'>&nbsp;
 
25
        Send Delay (ms): <input id='sendDelay' style='width:50' value="10">&nbsp;
 
26
        <input id='connectButton' type='button' value='Start' style='width:100px'
 
27
            onclick="connect();">&nbsp;
 
28
 
 
29
        <br><br>
 
30
        <table border=1>
 
31
            <tr>
 
32
                <th align="right">Packets sent:</th>
 
33
                <td align="right"><div id='sent'></div></td>
 
34
            </tr><tr>
 
35
                <th align="right">Packets Received:</th>
 
36
                <td align="right"><div id='received'></div></td>
 
37
            </tr><tr>
 
38
                <th align="right">Average Latency:</th>
 
39
                <td align="right"><div id='laverage'></div></td>
 
40
            </tr><tr>
 
41
                <th align="right">40 Frame Running Average Latency:</th>
 
42
                <td align="right"><div id='lrunning'></div></td>
 
43
            </tr><tr>
 
44
                <th align="right">Minimum Latency:</th>
 
45
                <td align="right"><div id='lmin'></div></td>
 
46
            </tr><tr>
 
47
                <th align="right">Maximum Latency:</th>
 
48
                <td align="right"><div id='lmax'></div></td>
 
49
            </tr>
 
50
        </table>
 
51
 
 
52
        <br>
 
53
        Messages:<br>
 
54
        <textarea id="messages" style="font-size: 9;" cols=80 rows=10></textarea>
 
55
    </body>
 
56
 
 
57
 
 
58
    <script>
 
59
 
 
60
        var host = null, port = null, sendDelay = 0, actualSendDelay,
 
61
            ws = null, send_ref = null,
 
62
            sent, received, latencies, ltotal, laverage, lrunning, lmin, lmax,
 
63
            run_length = 40,
 
64
            payload_size = 2000, payload,
 
65
            msg_cnt = 0, recv_seq = 0, send_seq = 0;
 
66
 
 
67
        Array.prototype.pushStr = function (str) {
 
68
            var n = str.length;
 
69
            for (var i=0; i < n; i++) {
 
70
                this.push(str.charCodeAt(i));
 
71
            }
 
72
        }
 
73
 
 
74
 
 
75
        function message(str) {
 
76
            console.log(str);
 
77
            cell = $D('messages');
 
78
            msg_cnt++;
 
79
            cell.innerHTML += msg_cnt + ": " + str + "\n";
 
80
            cell.scrollTop = cell.scrollHeight;
 
81
        }
 
82
 
 
83
 
 
84
        function add (x,y) {
 
85
            return parseInt(x,10)+parseInt(y,10);
 
86
        }
 
87
 
 
88
        function recvMsg(data) {
 
89
            //console.log(">> check_respond");
 
90
            var i, now, arr, first, last, arr, latency;
 
91
 
 
92
            now = (new Date()).getTime(); // Early as possible
 
93
 
 
94
            arr = ws.rQshiftBytes(ws.rQlen());
 
95
            first = String.fromCharCode(arr.shift());
 
96
            last = String.fromCharCode(arr.pop());
 
97
 
 
98
            if (first != "^") {
 
99
                message("Error: packet missing start char '^'");
 
100
                disconnect();
 
101
                return;
 
102
            }
 
103
            if (last != "$") {
 
104
                message("Error: packet missing end char '$'");
 
105
                disconnect();
 
106
                return;
 
107
            }
 
108
            arr = arr.map(function(num) {
 
109
                    return String.fromCharCode(num); 
 
110
                } ).join('').split(':');
 
111
            seq       = arr[0];
 
112
            timestamp = parseInt(arr[1],10);
 
113
            rpayload  = arr[2];
 
114
 
 
115
            if (seq != recv_seq) {
 
116
                message("Error: expected seq " + recv_seq + " but got " + seq);
 
117
                disconnect();
 
118
                return;
 
119
            }
 
120
            recv_seq++;
 
121
            if (payload !== rpayload) {
 
122
                message("Payload corrupt");
 
123
                disconnect();
 
124
                return;
 
125
            }
 
126
 
 
127
            received++;
 
128
 
 
129
            latency = now - timestamp;
 
130
            latencies.push(latency);
 
131
            if (latencies.length > run_length) {
 
132
                latencies.shift();
 
133
            }
 
134
            ltotal   += latency;
 
135
            laverage  = ltotal / received;
 
136
            lrunning  = 0;
 
137
            for (var i=0; i < latencies.length; i++) {
 
138
                lrunning += latencies[i];
 
139
            }
 
140
            lrunning  = lrunning / latencies.length;
 
141
 
 
142
            if (latency < lmin) {
 
143
                lmin  = latency;
 
144
            }
 
145
            if (latency > lmax) {
 
146
                lmax  = latency;
 
147
            }
 
148
 
 
149
            showStats();
 
150
            //console.log("<< check_respond");
 
151
        }
 
152
 
 
153
        function sendMsg() {
 
154
            var arr = [];
 
155
            if (! ws.flush() ) {
 
156
                message("WebSocket not ready, backing off");
 
157
                actualSendDelay = actualSendDelay * 2;
 
158
                send_ref = setTimeout(sendMsg, actualSendDelay);
 
159
                return false;
 
160
            } else {
 
161
                // Scale the delay down to the requested minimum
 
162
                if (actualSendDelay > sendDelay) {
 
163
                    message("WebSocket ready, increasing presure");
 
164
                    actualSendDelay = Math.max(actualSendDelay / 2, sendDelay);
 
165
                }
 
166
            }
 
167
 
 
168
            timestamp = (new Date()).getTime();
 
169
            arr.pushStr("^" + send_seq + ":" + timestamp + ":" + payload + "$");
 
170
            send_seq ++;
 
171
            ws.send(arr);
 
172
            sent++;
 
173
 
 
174
            showStats();
 
175
            send_ref = setTimeout(sendMsg, actualSendDelay);
 
176
        }
 
177
 
 
178
        function showStats() {
 
179
            $D('sent').innerHTML     = sent;
 
180
            $D('received').innerHTML = received;
 
181
            $D('laverage').innerHTML = laverage.toFixed(2);
 
182
            $D('lrunning').innerHTML = lrunning.toFixed(2);
 
183
            $D('lmin').innerHTML     = lmin.toFixed(2);
 
184
            $D('lmax').innerHTML     = lmax.toFixed(2);
 
185
        }
 
186
 
 
187
        function init_ws() {
 
188
            console.log(">> init_ws");
 
189
            var scheme = "ws://";
 
190
            if ($D('encrypt').checked) {
 
191
                scheme = "wss://";
 
192
            }
 
193
            var uri = scheme + host + ":" + port;
 
194
            console.log("connecting to " + uri);
 
195
            ws = new Websock();
 
196
            ws.maxBufferedAmount = 5000;
 
197
            ws.open(uri);
 
198
 
 
199
            ws.on('message', function() {
 
200
                recvMsg();
 
201
            });
 
202
            ws.on('open', function() {
 
203
                send_ref = setTimeout(sendMsg, sendDelay);
 
204
            });
 
205
            ws.on('close', function(e) {
 
206
                disconnect();
 
207
            });
 
208
            ws.on('error', function(e) {
 
209
                message("Websock error: " + e);
 
210
                disconnect();
 
211
            });
 
212
 
 
213
            console.log("<< init_ws");
 
214
        }
 
215
 
 
216
        function connect() {
 
217
            console.log(">> connect");
 
218
            host = $D('host').value;
 
219
            port = $D('port').value;
 
220
            payload_size = parseInt($D('payload_size').value, 10);
 
221
            sendDelay = parseInt($D('sendDelay').value, 10);
 
222
 
 
223
            if ((!host) || (!port)) {
 
224
                console.log("must set host and port");
 
225
                return;
 
226
            }
 
227
 
 
228
            if (ws) {
 
229
                ws.close();
 
230
            }
 
231
            init_ws();
 
232
 
 
233
            // Populate payload data
 
234
            var numlist = []
 
235
            for (var i=0; i < payload_size; i++) {
 
236
                numlist.push( Math.floor(Math.random()*10) );
 
237
            }
 
238
            payload = numlist.join('');
 
239
 
 
240
            // Initialize stats
 
241
            sent      = 0;
 
242
            received  = 0;
 
243
            latencies = [];
 
244
            ltotal    = 0;
 
245
            laverage  = 0;
 
246
            lrunning  = 0;
 
247
            lmin      = 999999999;
 
248
            lmax      = 0;
 
249
            actualSendDelay = sendDelay;
 
250
 
 
251
            $D('connectButton').value = "Stop";
 
252
            $D('connectButton').onclick = disconnect;
 
253
            console.log("<< connect");
 
254
        }
 
255
 
 
256
        function disconnect() {
 
257
            console.log(">> disconnect");
 
258
            if (ws) {
 
259
                ws.close();
 
260
            }
 
261
 
 
262
            if (send_ref) {
 
263
                clearInterval(send_ref);
 
264
                send_ref = null;
 
265
            }
 
266
            showStats(); // Final numbers
 
267
            recv_seq = 0;
 
268
            send_seq = 0;
 
269
 
 
270
            $D('connectButton').value = "Start";
 
271
            $D('connectButton').onclick = connect;
 
272
            console.log("<< disconnect");
 
273
        }
 
274
 
 
275
 
 
276
        window.onload = function() {
 
277
            console.log("onload");
 
278
            if (Websock_native) {
 
279
                message("Using native WebSockets");
 
280
            } else {
 
281
                message("initializing web-socket-js flash bridge");
 
282
            }
 
283
            var url = document.location.href;
 
284
            $D('host').value = (url.match(/host=([^&#]*)/) || ['',window.location.hostname])[1];
 
285
            $D('port').value = (url.match(/port=([^&#]*)/) || ['',window.location.port])[1];
 
286
            $D('payload_size').value = payload_size;
 
287
        }
 
288
    </script>
 
289
 
 
290
</html>