2016-06-22 109 views
2

几年前,我测试了node.js与apache 2.结果令人印象深刻。 node.js真的很快,特别是在并发性很高的情况下。为什么node.js比apache慢得多2.4

昨天我想向人们证明一下,.... 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"; 
?> 

我发起与阿帕奇port 80 然后我在端口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并发高...

难道我错了吗?任何评论赞赏。

亲切的问候 马丁

UPDATE

我发现这篇文章在网络http://zgadzaj.com/benchmarking-nodejs-basic-performance-tests-against-apache-php

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

+2

这些只是我对这个主题的想法以及关于node和apache的知识,不知道它们是否是答案: 只有一个处理所有这些请求的线程肯定会花费更多时间来处理这些小请求设置,相比,我相信是多线程的Apache?然而,如果你有各种各样的请求(一些小的,一些中等和一些重要的),nodejs将会非常出色,能够在所有工作之间快速切换,而不是被束缚在繁重的工作上,而apache的线程正在工作沉重的工作直到完成?只是一个想法。 –

+0

@Stian我完全同意。也许测试设置并不是真正显示node.js优点的那个。在这种情况下,Apache可能总是更快...... – marschro

回答

3

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

您的测试结果是不一样的,所以我们不能说性能的任何东西,直到这是固定的。

相关问题