Created
September 20, 2012 03:13
-
-
Save evanw/3753757 to your computer and use it in GitHub Desktop.
Revisions
-
evanw revised this gist
Sep 20, 2012 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
LoadingSorry, something went wrong. Reload?Sorry, we cannot display this file.Sorry, this file is invalid so it cannot be displayed. -
evanw created this gist
Sep 20, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ { "name": "ws-speed-test", "version": "0.1.0", "dependencies": { "ws": "0.4.21" } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,74 @@ 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); });