Here is my sample code:
var http = require('http'); var options1 = { host: 'www.google.com', port: 80, path: '/', method: 'GET' }; http.createServer(function (req, res) { var start = new Date(); var myCounter = req.query['myCounter'] || 0; var isSent = false; http.request(options1, function(response) { response.setEncoding('utf8'); response.on('data', function (chunk) { var end = new Date(); console.log(myCounter + ' BODY: ' + chunk + " time: " + (end-start) + " Request start time: " + start.getTime()); if (! isSent) { isSent = true; res.writeHead(200, {'Content-Type': 'application/xml'}); res.end(chunk); } }); }).end(); }).listen(3013); console.log('Server running at port 3013');
What I found out is that if I connect to another server (Google or any other), the response will be slower and slower for a few seconds. This does not happen if I connect to another node.js server on the same network.
I am using JMeter for testing. 50 simultaneously per second with a cycle of 1000.
I do not know what the problem is...
===========================
Further research:
I am running the same script on Rackspace as well as on EC2 for testing. And the script will use http.request to connect to: Google, Facebook, as well as my other script, which simply displays data (for example, hello world), which is hosted by another EC2 instance.
Testing Tool I just have jMeter on the desktop.
Pre- node.js test: jMeter β Google Result: fast and consistent. jMeter β Facebook Result: Fast and Consistent. jMeter -> My simple exit script Result: fast and consistent.
Then I create 50 parallel threads / sec with 100 cycles, checking my nodes Rackspace nodejs and then EC2 node.js, which has the same performance king jMeter β node.js β Google Result: from 50 ms to 2000 ms in 200 requests .
jMeter β node.js β Facebook Result: from 200 ms to 3000 ms after 200 requsets.
jMeter β node.js β My simple exit script Result: from 100 ms to 1000 ms after 200 requsets.
The first 10-20 queries are fast and then start to slow down.
Then when I switch to 10 parallel threads everything starts to change. The answer is very consistent, not slowing down.
Something C # related from parallel threads that node.js (http.request) can handle.
------------ More details --------------
Today I did more tests, and here it is: I used http.Agent and increased the maximum socket. However, it is interesting that on one test server (EC2) it improves significantly and does not slow down. But another server (rackspace) only improves a little. It still shows a slowdown. I even set "Connection: close" in the request header, it only improves 100 ms.
if http.request uses a connection pool, how to increase it?
on both servers, if I do "ulimit -a", the file of the open file is 1024.
------------- ** MORE AND MORE ** -------------------
It seems that even I set maxSockets to a larger number, it only works on some limit. Apparently there is a restriction on the socket or an internal limitation of the OS. However, to raise it?
------------- ** AFTER EXTENSIVE TEST ** ---------------
After reading many posts, I find out:
quote from: https://github.com/joyent/node/issues/877
1) If I set headers with connection = 'keep-alive', the performance is good and can go up to maxSocket = 1024 (this is my linux setup).
var options1 = { host: 'www.google.com', port: 80, path: '/', method: 'GET', **headers: { 'Connection':'keep-alive' }** };
If I set it to "Connection": "close", the response time will be 100 times slower.
Funny things happened here:
1) in EC2, when I test Connection: keep-alive first, it will take about 20-30 ms. Then, if I switch to Connection: Close OR set Agent: false, the response time slows down to 300 ms. WIHTOUT will restart the server if I switch to Connection: save-wait again, the response time will slow down even to 4000 ms. Either I need to restart the server, or wait a while to return the response to the lighting speed of 20-30 m.
2) If I ran it with an agent: false, then the response time will slow down to 300 ms. But then it will be faster again and return to "normal".
I assume the connection pool is still valid even if you install the agent: false. However, if you keep in touch: keep-alive, then it will be fast. just don't switch it.
July 25, 2011 Patch
I tried the latest version of node.js V0.4.9 with the fix http.js and https.js from https://github.com/mikeal/node/tree/http2 p>
performance is much better and more stable.