Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js slower than Apache

I am comparing performance of Node.js (0.5.1-pre) vs Apache (2.2.17) for a very simple scenario - serving a text file.

Here's the code I use for node server:

var http = require('http')   , fs = require('fs')  fs.readFile('/var/www/README.txt',     function(err, data) {         http.createServer(function(req, res) {             res.writeHead(200, {'Content-Type': 'text/plain'})             res.end(data)         }).listen(8080, '127.0.0.1')     } ) 

For Apache I am just using whatever default configuration which goes with Ubuntu 11.04

When running Apache Bench with the following parameters against Apache

ab -n10000 -c100 http://127.0.0.1/README.txt 

I get the following runtimes:

Time taken for tests:   1.083 seconds Complete requests:      10000 Failed requests:        0 Write errors:           0 Total transferred:      27630000 bytes HTML transferred:       24830000 bytes Requests per second:    9229.38 [#/sec] (mean) Time per request:       10.835 [ms] (mean) Time per request:       0.108 [ms] (mean, across all concurrent requests) Transfer rate:          24903.11 [Kbytes/sec] received  Connection Times (ms)               min  mean[+/-sd] median   max Connect:        0    0   0.8      0       9 Processing:     5   10   2.0     10      23 Waiting:        4   10   1.9     10      21 Total:          6   11   2.1     10      23  Percentage of the requests served within a certain time (ms)   50%     10   66%     11   75%     11   80%     11   90%     14   95%     15   98%     18   99%     19  100%     23 (longest request) 

When running Apache bench against node instance, these are the runtimes:

Time taken for tests:   1.712 seconds Complete requests:      10000 Failed requests:        0 Write errors:           0 Total transferred:      25470000 bytes HTML transferred:       24830000 bytes Requests per second:    5840.83 [#/sec] (mean) Time per request:       17.121 [ms] (mean) Time per request:       0.171 [ms] (mean, across all concurrent requests) Transfer rate:          14527.94 [Kbytes/sec] received  Connection Times (ms)               min  mean[+/-sd] median   max Connect:        0    0   0.9      0       8 Processing:     0   17   8.8     16      53 Waiting:        0   17   8.6     16      48 Total:          1   17   8.7     17      53  Percentage of the requests served within a certain time (ms)   50%     17   66%     21   75%     23   80%     25   90%     28   95%     31   98%     35   99%     38  100%     53 (longest request) 

Which is clearly slower than Apache. This is especially surprising if you consider the fact that Apache is doing a lot of other stuff, like logging etc.

Am I doing it wrong? Or is Node.js really slower in this scenario?

Edit 1: I do notice that node's concurrency is better - when increasing a number of simultaneous request to 1000, Apache starts dropping few of them, while node works fine with no connections dropped.

like image 543
Art Avatar asked Jul 09 '11 10:07

Art


People also ask

Why is NodeJS so slow?

require() — the nodejs module loader is really slow and may be to blame. require() is synchronous, which means blocking when loading each module. The quick and dirty technique is to use as fewer require() statements as possible. Alternatively, adding a caching module caching mechanism can solve the issue.

Is NodeJS slower than Java?

js is faster than Java because it uses on non-blocking calls (not just non-blocking I/O) while Java web apps usually rely on multi-threading. Some frameworks are good at some things, some others.

Is NodeJS slower than PHP?

Winner: Node. js wins the Node. js vs PHP performance battle as it offers better speed and a seamless and concurrent experience both for the developer and end-user.

CAN NodeJS replace Apache?

If you're prepared to re-write your PHP in JavaScript, then yes, Node. js can replace your Apache. If you place an Apache or NGINX instance running in reverse-proxy mode between your servers and your clients, you could handle some requests in JavaScript on Node.


1 Answers

Dynamic requests

node.js is very good at handling at lot small dynamic requests(which can be hanging/long-polling). But it is not good at handling large buffers. Ryan Dahl(Author node.js) explained this one of his presentations. I recommend you to study these slides. I also watched this online somewhere.

Garbage Collector

As you can see from slide(13 from 45) it is bad at big buffers.

Slide 15 from 45:

V8 has a generational garbage collector. Moves objects around randomly. Node can’t get a pointer to raw string data to write to socket.

Use Buffer

Slide 16 from 45

Using Node’s new Buffer object, the results change.

Still not that good as for example nginx, but a lot better. Also these slides are pretty old so probably Ryan has even improved this.

CDN

Still I don't think you should be using node.js to host static files. You are probably better of hosting them on a CDN which is optimized for hosting static files. Some popular CDN's(some even free for) via WIKI.

NGinx(+Memcached)

If you don't want to use CDN to host your static files I recommend you to use Nginx with memcached instead which is very fast.

like image 132
Alfred Avatar answered Sep 20 '22 08:09

Alfred