【问题标题】:Why is node.js so much slower than apache 2.4为什么 node.js 比 apache 2.4 慢这么多
【发布时间】:2016-10-24 22:38:04
【问题描述】:

几年前,我测试了 node.js 与 apache 2。结果令人印象深刻。 node.js 非常快,尤其是高并发。

昨天我想向某人展示,然后.... outch apache 2.4 更快。

设置:

Node.js(Express.js,节点 6.2.2)

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Apache 2.4(提供 php 文件)

<?php
  $foo = "Hello";
  $bar = "World";
  echo "$foo $bar";
?>

我用 80 端口启动了 apache 然后我在 3000 端口上启动了 node.js 应用程序并使用 Apache Benchmark 测试了所有内容

ab -r -n 10000 -c 10 http://127.0.0.1/

结果:

Server Software:        Apache/2.4.18
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /
Document Length:        11 bytes

Concurrency Level:      10
Time taken for tests:   4.439 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      1980000 bytes
HTML transferred:       110000 bytes
Requests per second:    2252.97 [#/sec] (mean)
Time per request:       4.439 [ms] (mean)
Time per request:       0.444 [ms] (mean, across all concurrent requests)
Transfer rate:          435.63 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.3      0      12
Processing:     1    3   1.8      3      88
Waiting:        0    3   1.5      3      38
Total:          1    4   1.8      4      91

Percentage of the requests served within a certain time (ms)
  50%      4
  66%      4
  75%      5
  80%      5
  90%      6
  95%      7
  98%      9
  99%     10
 100%     91 (longest request)

Node.js

ab -r -n 10000 -c 10 http://127.0.0.1/

结果:

Server Software:
Server Hostname:        127.0.0.1
Server Port:            3000

Document Path:          /
Document Length:        19 bytes

Concurrency Level:      10
Time taken for tests:   8.513 seconds
Complete requests:      10000
Failed requests:        0
Non-2xx responses:      10000
Total transferred:      4020000 bytes
HTML transferred:       190000 bytes
Requests per second:    1174.64 [#/sec] (mean)
Time per request:       8.513 [ms] (mean)
Time per request:       0.851 [ms] (mean, across all concurrent requests)
Transfer rate:          461.14 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       7
Processing:     1    8   4.4      8      69
Waiting:        0    8   4.3      7      69
Total:          2    8   4.4      8      69

Percentage of the requests served within a certain time (ms)
  50%      8
  66%      9
  75%     10
  80%     10
  90%     12
  95%     15
  98%     20
  99%     23
 100%     69 (longest request)

如果我测试 n=1000、c=100 ... 或更高,则相同 Apache 总是快两倍。

有什么改变吗?他们是否大大加快了 apache 2.4 的速度?还是 node.js 变老变慢了?

我真的记得,只要有高于 5 或​​ 10 的并发,node.js 就更快了……

我错了吗?任何意见表示赞赏。

亲切的问候 马丁

更新

我在网上找到这篇文章http://zgadzaj.com/benchmarking-nodejs-basic-performance-tests-against-apache-php

我无法重现这些结果。当我尝试相同的设置时,Apache 会更快。

【问题讨论】:

  • 这些只是我对这个主题的想法以及我对 node 和 apache 的了解,不确定它们是否是答案:Node 只是一个处理所有这些请求的线程,肯定会花更多时间来处理这些小问题与我认为是多线程的 apache 相比,您正在设置的请求?但是,如果你有各种各样的请求(一些小的,一些中等的和一些重的),nodejs 会表现出色,能够在所有作业之间快速切换,而不是被束缚在繁重的作业上,而 apache 的线程将会工作繁重的工作直到完成?只是一个想法。
  • @Stian 我完全同意。也许测试设置并不是真正显示 node.js 优势的设置。在这种情况下,apache 可能总是更快...

标签: javascript node.js apache performance benchmarking


【解决方案1】:

查看两组结果。 他们是一样的吗? 这里有一个区别: 非 2xx 响应:10000

你的测试结果不一样,所以在修复之前我们不能说性能。

【讨论】:

    猜你喜欢
    • 2013-02-01
    • 2013-06-05
    • 2019-12-05
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    相关资源
    最近更新 更多