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

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

问题描述

我正在比较 Node.js (0.5.1-pre) 与 Apache (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.

这是我用于节点服务器的代码:

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

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

我得到以下运行时:

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)

当针对 node 实例运行 Apache bench 时,这些是运行时:

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 时,Apache 开始丢弃其中的几个,而节点工作正常,没有断开连接.>

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 非常擅长处理大量小的动态请求(可以是挂起/长轮询).但它不擅长处理大缓冲区.Ryan Dahl(作者 node.js)解释了这一他的演讲.我建议你研究这些幻灯片.我也在某处网上看过这个.

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 开始

使用 Node 的新 Buffer 对象,结果发生了变化.

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

仍然不如 nginx 好,但好多了.这些幻灯片也很旧,所以可能瑞安甚至改进了这一点.

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 with 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天全站免登陆