我做了基准测试,比较什么是更快的 NodeJS 或 Apache + PHP?
当我测试“Hello world”应用程序节点时速度更快,但当我尝试使用 http.get 函数时,情况就完全不同了。
为什么 NodeJS 变得这么慢?它在 http.get 中处理吗?或者是什么?
测试环境
CPU Intel(R) Core(TM) i5 CPU M 430 @ 2.27GHz 内存 2927MiB 操作系统 Ubuntu 12.04 LTS 测试平台 Apache Bench NodeJS v0.8.2 阿帕奇阿帕奇/2.2.22 PHP PHP 5.3.10-1ubuntu3.2 与 Suhosin-Patch (cli)
1.你好世界应用程序:
节点代码:
var http        = require('http');
http.createServer(function(req, res) {
    res.writeHead(200, {"Content-Type": "text/html"});
    res.end('hello world');    
}).listen(8888);
PHP代码:
<?php
    echo "hello world"
?>
结果:
标题
ab -n 10000 -c 10 主机名                                 。
10.000 个请求,10 个并发(以秒为单位的时间)
节点JS 1.337 1.361 1.313 1.312 1.329 阿帕奇+PHP 3.923 3.910 3.917 3.926 3.921
ab -n 10000 -c 100 主机名
10.000 个请求,100 个并发(以秒为单位的时间)
节点JS 1.326 1.369 1.330 1.333 1.459 阿帕奇+PHP 3.933 3.917 3.940 3.908 3.913
ab -n 100000 -c 300 主机名
100.000 个请求,300 个并发(以秒为单位的时间)
节点JS 13.560 13.647 13.784 13.807 14.082 阿帕奇+PHP 44.061 41.516 41.523 41.466 41.465
2. 拉取提要应用:
节点代码:
var http = require('http');
var options1 = {
          host: 'www.google.com',
          port: 80,
          path: '/',
          method: 'GET'
        };
http.createServer(function (req, res) {
    http.get(options1, function(response) {
        response.on('data', function (chunk) {
        });
        response.on('end', function (chunk) {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.end('ok');
        });
    });
}).listen(8888);
PHP代码:
<?php
    file_get_contents('http://www.google.com');
    echo 'ok';
?>
结果:
* ab -n 100 -c 10 主机名 *
100 个请求,10 个并发(以秒为单位的时间)
节点JS 8.034 8.492 8.619 7.464 7.950 阿帕奇+PHP 18.648 16.699 19.428 17.903 18.297
* ab -n 1000 -c 10 主机名 *
1000 个请求,10 个并发(以秒为单位的时间)
节点JS 68.361 74.705 78.473 74.138 66.779 阿帕奇+PHP 150.568 159.024 161.179 160.819 157.605
* ab -n 10000 -c 100 主机名 *
 
10.000 个请求,100 个并发(以秒为单位的时间)
节点JS 1666.988 739.370 阿帕奇+PHP 310.062 244.485
* ab -n 10000 -c 50 主机名 *
 
10.000 个请求,50 个并发(以秒为单位的时间)
节点JS 256.096 260.625 阿帕奇+PHP 149.422 154.422