node/benchmark/idle_clients.js

50 lines
920 B
JavaScript
Raw Normal View History

2010-10-16 01:05:22 +08:00
net = require('net');
var errors = 0, connections = 0;
2010-10-27 17:52:49 +08:00
var lastClose = 0;
2010-10-16 01:05:22 +08:00
function connect () {
process.nextTick(function () {
var s = net.Stream();
var gotConnected = false;
s.connect(9000);
2010-10-27 17:52:49 +08:00
2010-10-16 01:05:22 +08:00
s.on('connect', function () {
gotConnected = true;
connections++;
2010-10-28 03:08:47 +08:00
connect();
2010-10-16 01:05:22 +08:00
});
s.on('close', function () {
if (gotConnected) connections--;
2010-10-27 17:52:49 +08:00
lastClose = new Date();
2010-10-16 01:05:22 +08:00
});
s.on('error', function () {
errors++;
});
});
}
connect();
var oldConnections, oldErrors;
// Try to start new connections every so often
setInterval(connect, 5000);
2010-10-16 01:05:22 +08:00
setInterval(function () {
if (oldConnections != connections) {
oldConnections = connections;
console.log("CLIENT %d connections: %d", process.pid, connections);
}
if (oldErrors != errors) {
oldErrors = errors;
console.log("CLIENT %d errors: %d", process.pid, errors);
}
}, 1000);