NodeJS基准
我做了基准testing,比较一下NodeJS或者Apache + PHP的更快速度。
当我testing过“Hello world”应用程序时Node速度更快,但是当我尝试使用http.get函数时,情况完全不同。
为什么NodeJS变得如此缓慢? 它在http.get中处理? 或者是什么?
testing环境
CPU Intel(R)Core TM i5 CPU M 430 @ 2.27GHz 内存2927MiB OS Ubuntu 12.04 LTS testing平台Apache Bench NodeJS v0.8.2 Apache Apache / 2.2.22 带有Suhosin-Patch(cli)的PHP PHP 5.3.10-1ubuntu3.2
1.你好世界应用程序:
NodeJS代码:
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个并发(以秒为单位)
NodeJS 1.337 1.361 1.313 1.312 1.329 Apache + PHP 3.923 3.910 3.917 3.926 3.921
ab -n 10000 -c 100主机名
10.000个请求,100个并发(以秒为单位)
NodeJS 1.326 1.369 1.330 1.333 1.459 Apache + PHP 3.933 3.917 3.940 3.908 3.913
ab -n 100000 -c 300主机名
100.000个请求,300个并发(以秒为单位)
NodeJS 13.560 13.647 13.784 13.807 14.082 Apache + PHP 44.061 41.516 41.523 41.466 41.465
2.提要申请:
NodeJS代码:
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个并发(时间以秒为单位)
NodeJS 8.034 8.492 8.619 7.464 7.950 Apache + PHP 18.648 16.699 19.428 17.903 18.297
* ab -n 1000 -c 10主机名*
1000个请求,10个并发(以秒为单位)
NodeJS 68.361 74.705 78.473 74.138 66.779 Apache + PHP 150.568 159.024 161.179 160.819 157.605
* ab -n 10000 -c 100主机名*
10.000个请求,100个并发(以秒为单位)
NodeJS 1666.988 739.370 Apache + PHP 310.062 244.485
* ab -n 10000 -c 50主机名*
10.000个请求,50个并发(以秒为单位)
NodeJS 256.096 260.625 Apache + PHP 149.422 154.422
我已经将我的代码更改为:
NodeJS代码:
var http = require('http'); http.globalAgent.maxSockets = 100000; var options1 = { host: 'www.google.com', port: 80, path: '/', method: 'GET', headers: { 'Connection':'keep-alive' } }; 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);
但是…并没有太大的改变。 它有一点点快,但与Apache + PHP相比,它是非常缓慢的。