Node.js的比Apache慢 [英] Node.js slower than Apache

查看:163
本文介绍了Node.js的比Apache慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个很简单的场景比较的Node.js的性能(0.5.1- pre)VS阿帕奇(2.2.17) - 提供一个文本文件

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.

这里的code我使用节点服务器:

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')
    }
)

对于Apache,我只是使用任何默认配置它与Ubuntu 11.04的推移

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

在与对以下参数运行的Apache台的阿帕奇

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)

在运行Apache替补反对的节点实例,这些都是运行时:

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)

这是比Apache显然慢。如果你考虑的是,Apache是​​做了很多其他的东西,如登录等,这是特别令人惊讶。

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.

我是不是做错了?或者是Node.js的在这个场景真的慢?

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

修改1 :我注意到,节点的并发性更好 - 增加数量的并发请求,到1000的时候,阿帕奇开始下降几个人,而节点工作正常,无连接的下降

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.

推荐答案

Node.js的是在很多小的动态处理的请求(可吊挂/长轮询)非常好。但它并不擅长处理大的缓冲区。瑞恩·达尔(作者Node.js的)解释他的presentations 的。我建议你​​研究这些幻灯片。我还看了这个网上的某个地方。

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.

你可以从幻灯片(13 45)看到它是坏的大缓冲区。

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

45幻灯片15:

V8拥有代垃圾
  集电极。移动物体周围
  随机。节点无法得到一个指向
  原始字符串数据写入套接字。

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

幻灯片16 45

使用节点的新的缓冲区对象,则
  业绩变动。

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

还没好作为例如nginx的,但好了很多。此外,这些幻灯片是pretty老所以大概瑞恩甚至改善这一点。

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.

不过,我不认为你应该使用Node.js的托管静态文件。你可能收留他们在其上托管静态文件优化的CDN的更好。 的一些流行的CDN (有的甚至免费),通过WIKI。

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.

如果你不希望使用CDN承载静态文件,我建议你使用的 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.

这篇关于Node.js的比Apache慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆