为什么node.js比apache 2.4

时间:2016-06-22 20:07:14

标签: javascript node.js apache performance benchmarking

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

昨天我想向某人展示......并且apch 2.4更快。

设置:

Node.js(Express.js,node 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会更快。

1 个答案:

答案 0 :(得分:3)

查看2组结果。 它们是一样的吗? 这里有一点不同: 非2xx回复:10000

您的测试结果不尽相同,因此在修复之前我们无法对性能发表任何意见。