Last active
December 20, 2015 07:09
-
-
Save nulltask/6090978 to your computer and use it in GitHub Desktop.
This file contains 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 || 'localhost'; | |
var options = { | |
port: process.env.LATENCY_PORT || 3000, | |
'force new connection': 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 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="/smoothie.js"></script> | |
<script> | |
window.onload = function () { | |
var chart = new SmoothieChart({ grid: { fillStyle: "#ffffff"}, labels: { fillStyle: "#000" } }) | |
, 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() | |
, lastDate | |
socket.on('connect', function () { | |
document.getElementById('transport').innerHTML = socket.socket.transport.name; | |
var allMs = 0; | |
var allCount = 0; | |
var highest = 0; | |
socket.on('message', function () { | |
var date = +new Date | |
, 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 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": "3", | |
"socket.io": "0.9", | |
"socket.io-client": "0.9" | |
} | |
} |
This file contains 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