【www.bbyears.com--Ubuntu】
环境:
64位Ubuntu14.04,i5-3230M
PHP5.4.31 with ZendOPcache
Node.JS 0.10.35
Node.JS 测试
var http = require("http");
http.createServer(function (req, res) {
res.writeHead(200, {"Content-Type": "text/html;charset=UTF-8"});
res.write("
res.end();
}).listen(8082, "127.0.0.1");
运行程序:
node t.js
查看系统CPU空闲率:
sar 1
压力测试,并发100,完成10万请求:
ab -c100 -n100000 http://127.0.0.1:8082/t.js
内存从11MB涨到61MB,系统CPU空闲率65%, RPS达到6049.
PHP CLI Server 测试
运行程序:
php -S 127.0.0.1:8081 -t ./
查看系统CPU空闲率:
sar 1
压力测试,并发100,完成10万请求:
ab -c100 -n100000 http://127.0.0.1:8081/t.php
内存从19.6MB涨到20.0MB,系统CPU空闲率57%, RPS达到11405.
不开启OPCACHE时,内存从8.3MB涨到9.0MB,系统CPU空闲率57%,RPS达到8851.
PHP Swoole 测试
$http = new swoole_http_server("0.0.0.0", 9501);
$http->set([
"worker_num" => 5 //开启5个工作进程
]);
$http->on("request", function (swoole_http_request $request, swoole_http_response $response) {
$response->header("Content-Type", "text/html;charset=UTF-8");
$response->end("
});
$http->start();
运行程序:
php swoole.php
查看系统CPU空闲率:
sar 1
压力测试,并发100,完成20万请求:
ab -c100 -n200000 http://127.0.0.1:9501/swoole.php
测试后内存占用达到117MB,系统CPU空闲率39%, RPS达到19216.
结论:Node.JS和PHP CLI Server都是单进程处理 HTTP 请求,但RPS上PHP几乎是Node.JS的两倍,内存占用上PHP却只有Node.JS的1/3。Node.JS值得称道的是,系统CPU空闲率要比PHP高8%左右。PHP Swoole应用了多进程和多线程,RPS近20K,充分利用了多核,所以CPU空闲率也是三者中最低的。另外,PHP-FPM虽然不支持HTTP协议,但其处理PHP请求的性能并不会比PHP CLI Server差,而且PHP-FPM可以开启多个工作进程,充分利用多核。