Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.JS Response Time

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?

like image 673
Jonathan Avatar asked Mar 14 '13 23:03

Jonathan


People also ask

How do I increase response time in node JS?

Another way to improve Node. js server response time is by updating database indexes. This can be done by adding new indexes, removing unused indexes, or rebuilding existing indexes. Adding new indexes can improve query performance by allowing the database to find the data it needs more quickly.

Is Nodejs fast?

Node. js has proved to be a saviour for many developers and enterprises with its exceptionally fast execution speed. Many big companies have been leveraging this property of Node. js for their benefit.

Why is node so slow?

Node. js programs can be slow due to a CPU/IO-bound operation, such as a database query or slow API call. For most Node. js applications, data fetching is done via an API request and a response is returned.


2 Answers

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?)

like image 51
Andrey Sidorov Avatar answered Oct 05 '22 18:10

Andrey Sidorov


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!

like image 44
Jonathan Avatar answered Oct 05 '22 18:10

Jonathan