nodejs performance compare with PHP

常言道 V8 超快 XD

所以心中一直有疑惑,到底是快到哪裡去 = =

node.js 真的有比其他的快嘛?

所以做了一點小實驗

比較 node.js 跟 php 的 performance
採用的軟體如下

  • Linux Kernel : 2.6.38-gentoo-r3 compiled with non-preemptive
  • web server : nginx 0.8.53
  • php version : PHP 5.3.6-pl1-gentoo running on php-fpm with port 8000
  • node.js : v0.4.7

php 的程式碼如下

<?php
$i = 0;
for($i = 0; $i < 10; ++$i){
}
echo "Hello World! " . $i . "\n";
view raw index.php hosted with ❤ by GitHub

我們利用 ab (apache benchmark program)

Concurrency Level 是 200
Total 打 10000 次

得到的資訊如下

Server Software: nginx
Server Hostname: 10.2.201.249
Server Port: 80
Document Path: /
Document Length: 16 bytes
Concurrency Level: 200
Time taken for tests: 19.834 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 15400000 bytes
HTML transferred: 1600000 bytes
Requests per second: 5041.85 [#/sec] (mean)
Time per request: 39.668 [ms] (mean)
Time per request: 0.198 [ms] (mean, across all concurrent requests)
Transfer rate: 758.25 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 76.6 1 3013
Processing: 11 36 152.9 25 3680
Waiting: 11 36 152.9 25 3680
Total: 12 39 170.8 27 3680
Percentage of the requests served within a certain time (ms)
50% 27
66% 34
75% 38
80% 40
90% 45
95% 49
98% 53
99% 61
100% 3680 (longest request)
view raw gistfile1.txt hosted with ❤ by GitHub

這裡可能會有人有疑惑說為什麼 php 不要用 unix socket 要用 tpc/ip socket 跑

因為我發現 php-fpm 綁在 unix socket 結果當 concurrency 是 200 時有一半以上不是回傳 200 ok

但是 concurrency 降到 100 時候又全部 ok

反正怪怪的 後來綁 tcp/ip 的就都 ok 了! (當然效能會折損)

所以 php-fpm 綁 port 8000 然後下面介紹的 node.js 是綁在 port 3000

nginx 都用 proxy_pass mode

而 node.js 的程式如下

var http = require('http');
http.createServer(function(request, response) {
var i;
for(i=0;i<10;++i){
}
var body = 'Hello World! ' + i + '\n';
response.writeHead(200, {
'Content-Type' : 'text/plain',
'Content_Length' : body.length
});
response.end(body);
}).listen(3000);
view raw server.js hosted with ❤ by GitHub

結果如下

Server Software: nginx
Server Hostname: 10.2.201.249
Server Port: 80
Document Path: /nodejs/
Document Length: 16 bytes
Concurrency Level: 200
Time taken for tests: 18.671 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 17500000 bytes
HTML transferred: 1600000 bytes
Requests per second: 5355.79 [#/sec] (mean)
Time per request: 37.343 [ms] (mean)
Time per request: 0.187 [ms] (mean, across all concurrent requests)
Transfer rate: 915.30 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 65.9 1 3014
Processing: 1 32 158.7 22 3262
Waiting: 1 32 158.7 22 3262
Total: 1 35 171.8 24 3262
Percentage of the requests served within a certain time (ms)
50% 24
66% 29
75% 33
80% 35
90% 41
95% 47
98% 56
99% 64
100% 3262 (longest request)
view raw gistfile1.txt hosted with ❤ by GitHub

我大概前後都各跑了 5 次 結果數值都差不多

我們可以發現 node.js on V8 比 php 快一點點 不管在 RPS (Requests per second) 或者是 Processing 上都快一點點

不過比較奇怪的是最長的時間都是三秒多 XD 是 nginx 的問題嘛 - - 承受不了這麼多 request

至於在 CPU 跟記憶體的比較上 我覺得好像差不多耶 沒有很明顯的差距拉出來

然後我又做了一點點小延伸 這次我裝了 node.js 的 framework Express

然後一樣的 output , app.js 的部份程式碼如下

app.get('/', function(req, res){
var i = 0;
for( i = 0 ; i<10; ++i){
}
res.send("Hello World! " + i + "\n");
});
view raw app.js hosted with ❤ by GitHub

得到的結果是這樣

Server Software: nginx
Server Hostname: 10.2.201.249
Server Port: 80
Document Path: /nodejs/
Document Length: 16 bytes
Concurrency Level: 200
Time taken for tests: 28.333 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 18900000 bytes
HTML transferred: 1600000 bytes
Requests per second: 3529.45 [#/sec] (mean)
Time per request: 56.666 [ms] (mean)
Time per request: 0.283 [ms] (mean, across all concurrent requests)
Transfer rate: 651.43 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 52.9 1 3007
Processing: 1 51 212.6 36 3218
Waiting: 1 51 212.6 36 3218
Total: 2 54 219.0 37 3222
Percentage of the requests served within a certain time (ms)
50% 37
66% 46
75% 52
80% 55
90% 63
95% 69
98% 76
99% 86
100% 3222 (longest request)
view raw gistfile1.txt hosted with ❤ by GitHub

效能大概降了 30% 有吧 蠻慘的 XD

遠輸於 php 不過我沒有跑 Zend 試看看 我想一但有 framework 上去 不管什麼都會變慢吧

尤其是 routing 最麻煩 所以應該都會慢很多

所以結論是原生的 node.js 是比 php 快一點點的 (約 5%)

後記:
這裡有一篇類似的 不過他打的數量好高 * *
Benchmarking Node.js – basic performance tests against Apache + PHP