13

Threw Node.JS on an AWS instance and was testing the request times, got some interesting results.

I used the following for the server:

var http = require('http');

http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write('Hello World');
  res.end();
}).listen(8080);

I have an average 90ms delay to this server, but the total request takes ~350+ms. Obviously a lot of time is wasted on the box. I made sure the DNS was cached prior to the test.

I did an Apache bench on the server with a cocurrency of 1000 - it finished 10,000 requests in 4.3 seconds... which means an average of 4.3 milliseconds.

UPDATE: Just for grins, I installed Apache + PHP on the same machine and did a simple "Hello World" echo and got a 92ms response time on average (two over ping).

Is there a setting somewhere that I am missing?

Jonathan
  • 996
  • 1
  • 7
  • 27
  • 1
    how did you measure total request time? how did you benchmark 10000 requests time? – Andrey Sidorov Mar 15 '13 at 01:01
  • @AndreySidorov Just used Chrome Developer Tools locally for request time, I did an apache bench for the server. – Jonathan Mar 15 '13 at 01:13
  • hey Jonathan, so the conclusion is?... response time with node is slow? would be interesting to investigate further – George Katsanos Mar 22 '13 at 06:58
  • @GeorgeKatsanos Actually, the contrary. See my answer for some more details, but through process of elimination, it seems that calling ``res.write()`` kills performance. So using packages like express.js which don't use that function will help a lot. When done correctly, I was getting a response of 1-2ms over ping with node.js – Jonathan Mar 22 '13 at 17:54

2 Answers2

15

While Chrome Developer Tools is a good way to investigate front end performance, it gives you very rough estimate of actual server timings / cpu load. If you have ~350 ms total request time in dev tools, subtract from this number DNS lookup + Connecting + Sending + Receiving, then subtract roundtrip time (90 ms?) and after that you have first estimate. In your case I expect actual request time to be sub-millisecond. Try to run this code on server:

var http = require('http');
function hrdiff(t1, t2) {
    var s = t2[0] - t1[0];
    var mms = t2[1] - t1[1];
    return s*1e9 + mms;
}
http.createServer(function(req, res) {
  var t1 = process.hrtime();
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write('Hello World');
  res.end();
  var t2 = process.hrtime();
  console.log(hrdiff(t1, t2));
}).listen(8080);

Based on ab result you should estimate average send+request+receive time to be at most 4.2 ms ( 4200 ms / 10000 req) (did you run it on server? what concurrency?)

Andrey Sidorov
  • 24,905
  • 4
  • 62
  • 75
  • 1
    +1 For an intelligent answer. All very interesting. In my local test, DNS was cached, so without ping, the request took 260ms - according to the snippet your provided, the average request for this is around 0.6 milliseconds. On the ab, I did it on server with concurrency at 1000. **Those numbers make sense, but I'm still at a loss when it comes to that 260ms** – Jonathan Mar 15 '13 at 16:59
  • 2
    Further interesting numbers, testing Apache/PHP on the same box, I'm getting a response time of 92ms (two over ping). While i'm still getting roughly 350ms on the NodeJS script. I feel like i'm missing something... – Jonathan Mar 15 '13 at 17:32
  • I have very similar results with Apache/PHP and with node.js when measuring from the client. Can you do more detailed measurements using https://github.com/mnot/htracr or just use tcpdump and post your results? – Andrey Sidorov Mar 22 '13 at 00:38
  • I actually figured it out - see my answer. I'll go ahead and award you the bounty for the help, however! Thank you sir. – Jonathan Mar 22 '13 at 06:48
13

I absolutely hate answering my own questions, but I want to pass along what I have discovered with future readers.

tl;dr: There is something wrong with res.write(). Use express.js or res.end()

I just got through conducting a bunch of tests. I setup multiple types of Node server and mixed in things like PHP and Nginx. Here are my findings.

As stated previously, with the snippet I included above, I was loosing around 250ms/request, but the Apache benchmarks did not replicate that issues. I then proceeded to do a PHP test and got results ranging from 2ms - 20ms over ping... a big difference.

This prompted some more research, I started a Nginx server and proxied the node through it, and somehow, that magically changed the response from 250ms to 15ms over ping. I was on par with that PHP script, but that is a really confusing result. Usually additional hops would slow things down.

Intrigued, I made an express.js server as well - and something even more interesting happened, the ping was 2ms over on its own. I dug around in the source for quite a while and noticed that it lacked a res.write() command, rather, it went straight to the res.end(). I started another server removing the "Hello World" from the res.write and added it to the res.end and amazingly, the ping was 0ms over ping.

I did some searching on this, wanted to see if it was a well-known issue and came across this SO question, who had the exact same problem. nodejs response speed and nginx

Overall, intresting stuff. Make sure you optimize your responses and send it all at once.

Best of luck to everyone!

Community
  • 1
  • 1
Jonathan
  • 996
  • 1
  • 7
  • 27