我在 Linux 机器(Ubuntu 12.04 64 位,3.75GB 内存,1 个核心)上有一个 express(node.js v0.10.29)代理,它为每个传入请求发出一些出站 HTTP 请求。在负载测试时,我发现响应时间变得非常慢(在触发 1000 时发出 4 个出站请求的请求大约需要 30 秒)。经过一番调查,我确定出站请求是瓶颈并消除了机器限制(cpu 和内存没有超过 20%,我将打开文件的数量增加到 10000)。首先,我对出站请求使用 request 模块,尝试将其更改为 http 模块,并为他们两个尝试增加globalAgent.maxSockets
, using agent = false
, using 我自己的代理和任意数量的maxSockets
, setting request.setNoDelay(true)
, usingcluster
,但我的负载测试结果没有任何改变。
可能是什么问题?
这是我最新的 HTTP 请求代码:
var http = require('http');
var agent = new http.Agent();
agent.maxSockets = 100;
var doPost = function(reqUrl, body, next, onError) {
var stringBody = JSON.stringify(body);
var headers = {
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip',
'Content-Length': stringBody.length
};
var parsedUrl = url.parse(reqUrl);
var options = {
host: parsedUrl.host,
path: parsedUrl.path,
method: 'POST',
headers: headers,
//agent: false
agent: agent
};
doHttpRequest(options, stringBody, next, onError);
};
function doHttpRequest(options, body, next, onError, HttpContext){
var req = http.request(options, function(res) {
var chunks = [];
res.on('data', function (chunk) {
chunks.push(chunk);
});
res.on('end', function(){
var buffer = Buffer.concat(chunks);
var encoding = res.headers['content-encoding'];
if (encoding == 'gzip') {
zlib.gunzip(buffer, function(error, decoded) {
var jsonRes = JSON.parse(decoded && decoded.toString());
next(jsonRes);
});
} else if (encoding == 'deflate') {
zlib.inflate(buffer, function(error, decoded) {
var jsonRes = JSON.parse(decoded && decoded.toString());
next(jsonRes);
});
} else {
next(null, buffer.toString());
}
});
});
req.setNoDelay(true);
req.write(body);
req.end();
req.on('error', function(e) {
log(e);
});
}
“next”方法将调用“doPost”函数几次(在本例中为 4 次)。