我正在尝试使用NODE.JS测试服务器的可用性,例如服务器监控。我创建了包含5000 个域的测试 CSV 文件,并使用了以下代码:
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
});
});
特定域有很多超时:
[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
但是这段代码很慢。完成它需要将近 30 分钟。如何加快此代码(异步连接)?
我需要脚本能够在每 5 分钟内测试 100.000 个域