为什么 node.js 比 apache 2.4 慢那么多

Why is node.js so much slower than apache 2.4

几年前我测试了node.js vs 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 变老变慢了?

我真的记得 node.js 并发高于 5 或​​ 10 时速度更快...

我错了吗?任何评论表示赞赏。

亲切的问候 马丁

更新

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

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

查看2组结果。 它们相同吗? 这里有一个区别: 非 2xx 响应:10000

你们的测试结果不一样,所以在问题解决之前我们不能说任何关于性能的事情。