I have an express (node.js v0.10.29) proxy server on a Linux machine (Ubuntu 12.04 64 bit, memory 3.75 GB, 1 core), which makes some outgoing HTTP requests for each incoming request. While testing the load, I found that the response time becomes very slow (about 30 seconds for a request that makes 4 outgoing requests when starting 1000). After some investigation, I made sure that outgoing requests are a bottleneck and eliminate the restriction on the machine (the processor and memory do not get higher than 20%, and I increased the number of open files to 10,000). First, I used the request module for the outgoing request, tried to change it to an http module, and for both of them I tried to increase globalAgent.maxSocketsusing agent = falseusing my own agent with any quantity maxSockets, settingrequest.setNoDelay(true)using clusterbut nothing changed the results of my load testing.
what could be the problem?
Here is my last code for the HTTP request:
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: 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);
});
}
the "next" method will call the "doPost" function several times (in this case, 4 times).