环境: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('<!DOCTYPE html><html><head><meta charset="UTF-8" /><title>Node测试</title></head><body>'+new Date().getTime()+'</body></html>');
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/
内存从11MB涨到61MB,系统CPU空闲率65%, RPS达到6049.
RPS即Requests per second,每秒处理的请求数,越大越好.
提示:点击图片,查看原图。



PHP CLI Server 测试
<?php header('Content-Type: text/html;charset=UTF-8'); ?> <!DOCTYPE html><html><head><meta charset="UTF-8" /><title>PHP测试</title></head><body><?php echo time(); ?></body></html>运行程序:
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 测试
<?php $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('<!DOCTYPE html><html><head><meta charset="UTF-8" /><title>PHP测试</title></head><body>'.time().'</body></html>'); }); $http->start(); 运行程序: php swoole.php 查看系统CPU空闲率: sar 1 压力测试,并发1000,完成20万请求: ab -c1000 -n200000 http://127.0.0.1:9501/ 测试过程中系统CPU空闲率39%, **RPS达到17554**.  结论:Node.JS和PHP CLI Server都是单进程处理 HTTP 请求,但RPS上PHP几乎是Node.JS的两倍,内存占用上PHP却只有Node.JS的1/3。Node.JS值得称道的是,系统CPU空闲率要比PHP高8%左右。PHP Swoole应用了多进程和多线程,RPS近18K,充分利用了多核,所以CPU空闲率也是三者中最低的。另外,PHP-FPM虽然不支持HTTP协议,但其处理PHP请求的性能并不会比PHP CLI Server差,而且PHP-FPM可以开启多个工作进程,充分利用多核。 **附加测试****: 连接Redis的get/set测试** ------------------------------- 1\. Apache 2.4.10 mpm\_prefork\_module 一个工作进程,使用pecl install redis安装Redis客户端. <?php header('Content-Type: text/html;charset=UTF-8'); $redis = new Redis(); $redis->pconnect('127.0.0.1', 6379); $redis->set('time\_php', time()); $redis->get('time\_php'); ?> <!DOCTYPE html><html><head><meta charset="UTF-8" /><title>PHP测试</title></head><body><?php echo time(); ?></body></html>测试: CPU空闲率在65%, RPS达到5140.
ab -c100 -n50000 http://127.0.0.1:8080/redis.php

2.Node.JS,使用npm install redis安装Redis客户端.
var redis = require('redis');
var client = redis.createClient('6379', '127.0.0.1');
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html;charset=UTF-8'});
res.write('<!DOCTYPE html><html><head><meta charset="UTF-8" /><title>Node测试</title></head><body>'+new Date().getTime()+'</body></html>');
client.set("time_node", new Date().getTime(), function (err, reply) {
client.get("time_node");
});
res.end();
}).listen(8082, '127.0.0.1');
测试: CPU空闲率在65%, RPS达到5369.
ab -c100 -n50000 http://127.0.0.1:8082/

也就是说,就算在Apache老式的Prefork面前,Node.JS(RPS 5369)对比PHP(RPS 5140)的性能优势也不明显.别忘了,Apache还是一个经典的完备的HTTP服务器.