-
-
Save ihilt/b0aaaadd45a7ad8774be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var WebSocket = require('ws'); | |
| var ws = new WebSocket('ws://localhost:8001'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "ws-speed-test", | |
| "version": "0.1.1", | |
| "dependencies": { | |
| "ws": "0.4.31" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var http = require('http'); | |
| var ws = require('ws'); | |
| var server = http.createServer(function(req, res) { | |
| console.log(req.method + ' ' + req.url); | |
| res.writeHead(200, { 'Content-Type': 'text/html' }); | |
| res.end('\ | |
| <script>\ | |
| window.onload = function() {\ | |
| var ws = new WebSocket("ws://" + location.hostname + ":8001/");\ | |
| function log(text) {\ | |
| document.body.appendChild(document.createTextNode(text));\ | |
| document.body.appendChild(document.createElement("br"));\ | |
| }\ | |
| log("connecting to " + ws.URL);\ | |
| ws.onopen = function() {\ | |
| log("connected");\ | |
| };\ | |
| ws.onclose = function(e) {\ | |
| log("disconnected");\ | |
| };\ | |
| ws.onmessage = function(e) {\ | |
| var text = "got " + (e.data.length / (1024 * 1024)).toFixed(3) + " MB";\ | |
| ws.send(text);\ | |
| log(text);\ | |
| };\ | |
| };\ | |
| </script>\ | |
| '); | |
| }); | |
| console.log('listening on port 8000'); | |
| server.listen(8000); | |
| var wss = new ws.Server({ port: 8001 }); | |
| wss.on('connection', function(ws) { | |
| var onmessage = null; | |
| ws.on('message', function(data) { | |
| onmessage(data); | |
| }); | |
| function test(sizeInMB, callback) { | |
| var data = new Uint8Array(sizeInMB * 1024 * 1024); | |
| for (var j = 0; j < data.length; j++) { | |
| data[j] = 32 + Math.random() * 95; | |
| } | |
| var sendStart = Date.now(); | |
| var sendTime; | |
| console.log('sending ' + sizeInMB.toFixed(3) + ' MB'); | |
| onmessage = function(data) { | |
| var totalTime = (Date.now() - sendStart) / 1000; | |
| console.log(data + ', took ' + totalTime.toFixed(3) + ' seconds'); | |
| times.push({ sizeInMB: sizeInMB, sendTime: sendTime, totalTime: totalTime }); | |
| setTimeout(callback, 500); | |
| }; | |
| ws.send(data, function() { | |
| sendTime = (Date.now() - sendStart) / 1000; | |
| console.log('sent ' + sizeInMB.toFixed(3) + ' MB, took ' + sendTime.toFixed(3) + ' seconds'); | |
| }); | |
| } | |
| var times = []; | |
| var i = 0; | |
| function next() { | |
| i += 0.5; | |
| if (i > 5) done(); | |
| else test(i, next); | |
| } | |
| function done() { | |
| console.log('sizeInMB\tsendTime\ttotalTime'); | |
| for (var i = 0; i < times.length; i++) { | |
| console.log(times[i].sizeInMB + '\t' + times[i].sendTime + '\t' + times[i].totalTime); | |
| } | |
| } | |
| setTimeout(next, 500); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
