I'm trying to test servers availability with NODE.JS something like server monitoring. I created test CSV file with 5000 domains in it and used the code bellow:
var net = require('net');
function Daemon(address,name, port) {
this.address = address;
this.name = name;
this.port = port;
}
var fs = require('fs');
var array = fs.readFileSync('domains.csv').toString().split("\n");
for(i in array) {
console.log(array[i]);
daemons.push(new Daemon(array[i],'http', 80));
}
// loop through each daemon in the array
daemons.forEach(function(d) {
// create the TCP stream to the server
var stream = net.createConnection(d.port, d.address);
console.log('[' + d.address + ']\t connected');
// listen for connection
stream.on('connect', function(){
// connection success
//console.log('[' + d.name + ']\t connected');
stream.end(); // close the stream
});
// listen for any errors
stream.on('error', function(error){
console.log('[' + d.name + ']\t error: ' + error);
stream.destroy(); // close the stream
});
});
There are many timeouts for particular domains:
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: connect ETIMEDOUT
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: connect ETIMEDOUT
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: connect ETIMEDOUT
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: connect ETIMEDOUT
[http] error: Error: connect ETIMEDOUT
[http] error: Error: connect ETIMEDOUT
[http] error: Error: connect ETIMEDOUT
[http] error: Error: read ETIMEDOUT
[http] error: Error: connect ETIMEDOUT
[http] error: Error: read ETIMEDOUT
[http] error: Error: connect ETIMEDOUT
[http] error: Error: read ETIMEDOUT
[http] error: Error: connect ETIMEDOUT
But this code is slow. It takes almost 30 minutes to finish it. How can I speed up the this code (asynchronous connections) ?
I need the script to be able to test 100.000 domains in every 5 minutes
net.createConnection is non blocking so you already have asynchronous connections.
I tried your code with 500 ips and it took less than 10 secs for accepted or refused connections, only the TIMEOUT ones took a bit longer (30-40 secs)
If you want to do more async, use async module ^^ Your loop will look like :
async.forEach(
daemons,
function(daemon) {
//Your connect stuff
},
function(err){}
);
My guess is that at one point, opening 5000 outbound TCP connections is not good for your system. You should limit concurrent connection attempts to a reasonable number.
I suggest you use async.queue with a concurrency of 100 or 200.