フレームワーク対決:Node.js+SailsとPHP+Phalconのベンチマーク

(Last Updated On: 2018年8月13日)

Node.jsは速いと言われています。Node.jsのMVCフレームワークであるSailsPHP最速フレームワークであるPhalconとの性能を比較しました。

テスト環境はFedora19 x86_64です。Node.jsとPHPはFedora付属の物を使い予めインストールされているものとします。性能比較は公正を期すためにシングルプロセスで行います。

Node.jsは1つのコアしか使いません。Node.jsも複数のインスタンスを実行すればマルチコアも利用可能ですが、PHP+Apacheを利用するとホストのコア全部を使ってしまいます。あまりに不公平なのでシングルプロセス(PHPのビルトインWebサーバー)でのベンチマークにしました。

SailsとPhalconのインストール

まずSailsのインストールから起動までのコマンドです。

$ sudo npm -g install sails
$ sails new testProject
$ cd testProject
$ sails lift

これで http://127.0.0.1:1337 にアクセスするとインストール直後のページが表示されます。

次にPhalconのインストールです。

$ sudo yum install http://www.provephp.com/downloads/php-phalcon-1.2.4-1.fc19.x86_64.rpm
$ phalcon project testproject
$ cd testproject/public
$ php -S 127.0.0.1:8888

これで http://127.0.0.1:8888 にアクセスするとインストール直後のページが表示されます。

どちらもインストールは簡単に行えます。(自分で作ったPhalcon RPMパッケージを使っているので反則?!)

ベンチマークするページ

SailsとPhalconのデフォルトのindexページ内容は異なります。同じにしないと公正ではないので内容を

Hello World

に変更します。それぞれのプロジェクトのルートディレクトリから編集します。

Sailsのインデックスファイルの編集

$ vi views/home/index.ejs 

Phalconのインデックスファイルの編集

$ vi public/index.volt

ベンチマーク

別のマシンからベンチマークした方が良いのですが、簡易ベンチマークということで同じホストからabコマンドでHello Worldページにアクセスします。ウォームアップする為に2回目のabコマンドの結果を採用します。

Sailsのベンチマーク

[yohgaki@dev testProject]$ ab -c 10 -n 10000 http://localhost:1337/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        
Server Hostname:        localhost
Server Port:            1337

Document Path:          /
Document Length:        2861 bytes

Concurrency Level:      10
Time taken for tests:   57.177 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      31696530 bytes
HTML transferred:       28610000 bytes
Requests per second:    174.89 [#/sec] (mean)
Time per request:       57.177 [ms] (mean)
Time per request:       5.718 [ms] (mean, across all concurrent requests)
Transfer rate:          541.36 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     8   57   6.9     54     101
Waiting:        6   57   6.9     54     101
Total:          8   57   6.9     54     101

Percentage of the requests served within a certain time (ms)
  50%     54
  66%     57
  75%     58
  80%     58
  90%     62
  95%     76
  98%     80
  99%     86
 100%    101 (longest request)

Phalconのベンチマーク

[yohgaki@dev testproject]$ ab -c 10 -n 10000 http://localhost:8888/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        
Server Hostname:        localhost
Server Port:            8888

Document Path:          /
Document Length:        12 bytes

Concurrency Level:      10
Time taken for tests:   10.566 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      1370000 bytes
HTML transferred:       120000 bytes
Requests per second:    946.40 [#/sec] (mean)
Time per request:       10.566 [ms] (mean)
Time per request:       1.057 [ms] (mean, across all concurrent requests)
Transfer rate:          126.62 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       1
Processing:     2   11   0.6     10      16
Waiting:        1   10   0.5     10      15
Total:          2   11   0.6     10      16
WARNING: The median and mean for the processing time are not within a normal deviation
        These results are probably not that reliable.
WARNING: The median and mean for the total time are not within a normal deviation
        These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
  50%     10
  66%     10
  75%     11
  80%     11
  90%     11
  95%     11
  98%     12
  99%     13
 100%     16 (longest request)

ベンチマーク結果

Sails
Requests per second: 174.89 [#/sec] (mean)
100% 101 (longest request)

Phalcon
Requests per second: 946.40 [#/sec] (mean)
100% 16 (longest request)

PhalconはSailsの5倍以上高速で、レイテンシーも大幅に小さい事が分かります。

ただし、SailsはViewテンプレートを”Hello World”だけにしても自動的にかなりの量のテキストを追加しています。これはレイテンシー・パフォーマンスに影響していると思われますが、ベンチマーク結果に大きな影響はないでしょう。

Sailsが極端に遅いのか?というとPHPの比較的大きなフレームワークならこの程度の速さです。極端に遅いとは言えません。Phalconが速いだけです。

まとめ

Node.jsが速いのか?というと普通にMVCフレームワークを使ってアプリを作れば、PHPでMVCフレームワークを使うアプリとそれほど変わらないでしょう。

Node.jsも複数のインスタンスを作ってマルチコアを使う事もできますが、PHPの場合Apache/NginxなどのWebサーバーを利用すれば普通にマルチコアを使い切ります。シングルコア対マルチコアになれば更に差が開くのは言うまでもありません。

Node.jsが遅いのでしょうか?Node.jsが遅い訳ではありません。最速PHPフレームワークのPhalconには歯が立たないだけと言えるでしょう。

LL系の最速MVCフレームワークが必要な場合、圧倒的に速いPhalconは魅力的な選択肢になると思います。

FacebookにPhalconのページを作っています。よろしければ「いいね!」をお願いします。「いいね!」をして頂くとFalconのページが更新された場合に自分のページに掲載されます。

追記:
データ量が違うので速度がかなり変わる?と考える方も居ると思います。Sailsが出力したコンテンツと同じデータをindex.voltに貼り付けた結果は以下の通りです。

Total transferred:      29860000 bytes
HTML transferred:       28610000 bytes
Requests per second:    946.94 [#/sec] (mean)
Time per request:       10.560 [ms] (mean)
Time per request:       1.056 [ms] (mean, across all concurrent requests)
Transfer rate:          2761.29 [Kbytes/sec] received

 100%     17 (longest request)

ネットワークやCPUがボトルネックになっていないので予想通り、ほとんど変わっていません。

投稿者: yohgaki