Created
July 28, 2014 08:46
-
-
Save nulltask/d71afdcf19bbd4b29baa 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 io = require('socket.io-client'); | |
| var connection = process.env.LATENCY_CONNECTION || 100; | |
| var host = process.env.LATENCY_HOST || 'ws://localhost:3000'; | |
| var options = { | |
| forceNew: true | |
| }; | |
| for (var i = 0; i < connection; i++) { | |
| (function() { | |
| var timerId; | |
| var socket = io.connect(host, options); | |
| socket.on('connect', function() { | |
| socket.emit('ping'); | |
| socket.on('pong', function() { | |
| timerId = setTimeout(function() { | |
| socket.emit('ping'); | |
| }, Math.round(Math.random() * 100) + 100); | |
| }); | |
| socket.on('disconnect', function() { | |
| clearTimeout(timerId); | |
| }); | |
| }); | |
| })(); | |
| } |
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
| <!doctype html> | |
| <html> | |
| <head> | |
| <title>Socket.IO</title> | |
| <script src="/socket.io/socket.io.js"></script> | |
| <script src="//rawgit.com/joewalnes/smoothie/v1.10/smoothie.js"></script> | |
| <script> | |
| window.onload = function () { | |
| var chart = new SmoothieChart({ grid: { fillStyle: "#ffffff"}, labels: { fillStyle: "#000" } }); | |
| var latency = new TimeSeries() | |
| chart.addTimeSeries(latency, { strokeStyle: 'rgba(69, 183, 244, 1)', fillStyle: 'rgba(255, 255, 255, 1)', lineWidth: 4 }); | |
| chart.streamTo(document.getElementById("smoothie"), 500); | |
| // document.getElementById('v').innerHTML = io.version; | |
| var socket = io.connect(); | |
| var lastDate; | |
| socket.on('connect', function () { | |
| document.getElementById('transport').innerHTML = socket.io.engine.transports.join(', '); | |
| var allMs = 0; | |
| var allCount = 0; | |
| var highest = 0; | |
| socket.on('message', function () { | |
| var date = +new Date; | |
| var ms = date - lastDate; | |
| allMs += ms; | |
| allCount++; | |
| if (ms > highest) highest = ms; | |
| var mean = allMs/allCount; | |
| mean = (~~(mean*1000))/1000; | |
| document.getElementById('c').innerHTML = allCount; | |
| document.getElementById('l').innerHTML = mean; | |
| document.getElementById('h').innerHTML = highest; | |
| latency.append(date, ms); | |
| write(); | |
| }); | |
| function write() { | |
| setTimeout(function () { | |
| lastDate = new Date | |
| socket.send('ping'); | |
| }, 100); | |
| } | |
| write(); | |
| }); | |
| } | |
| </script> | |
| <style> | |
| h1 { | |
| font: bold 16px Helvetica; | |
| } | |
| em { | |
| background: #fbec94; | |
| -webkit-border-radius: 4px; | |
| font-style: normal; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>Socket.IO v<span id="v"></span> - <em id="transport"></em> count: <span id="c"></span>, latency (mean): <span id="l"></span> ms, highest: <span id="h"></span> ms</h1> | |
| <canvas id="smoothie"> | |
| </body> | |
| </html> |
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": "latency-io", | |
| "description": "Latency tester for socket.io transports", | |
| "version": "0.0.1", | |
| "dependencies": { | |
| "express": "^4.7.2", | |
| "socket.io": "^1.0.6", | |
| "socket.io-client": "^1.0.6" | |
| } | |
| } |
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 express = require('express'); | |
| var app = express(); | |
| var server = require('http').createServer(app); | |
| var sio = require('socket.io'); | |
| var io = sio.listen(server); | |
| app.use(express.static(__dirname + '/public')); | |
| app.get('/', function (req, res) { | |
| res.sendfile('index.html'); | |
| }); | |
| app.get('/sockets', function(req, res) { | |
| server.getConnections(function(err, count) { | |
| res.send({ | |
| conn: count, | |
| sockets: Object.keys(io.sockets.sockets).length | |
| }); | |
| }); | |
| }); | |
| // socket.io | |
| // io.set('log level', false); | |
| // io.set('transports', [process.env.LATENCY_TRANSPORT || 'xhr-polling']); | |
| io.sockets.on('connection', function (socket) { | |
| var timerId; | |
| socket.on('message', function (msg) { | |
| socket.send(msg); | |
| }); | |
| socket.on('ping', function() { | |
| timerId = setTimeout(function() { | |
| socket.emit('pong'); | |
| }, Math.round(Math.random() * 100) + 100); | |
| }); | |
| socket.on('disconnect', function() { | |
| clearTimeout(timerId); | |
| }); | |
| }); | |
| server.listen(process.env.LATENCY_PORT || 3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment