LoginSignup
14
17

More than 5 years have passed since last update.

PHP7って本当に速いの?

Last updated at Posted at 2016-04-08

PHP7がリリースされてはや半年、そろそろ頃合いと思い導入してみました。
全体的に処理速度が改善された上に、下位互換をなるべく保つように開発されているようです。

どんぐらい速くなったのか試してみたくなるのがエンジニアとしての性。
PHP5.6からPHP7にバージョンアップし、ApacheBenchで測定してみました。

あとここではあえて触れませんが、バージョンアップすることによって使えなくなるアプリもあるので、ご自分の環境に導入される際は十分検証してから導入するようにお願いします。
#自分の確認できた範囲ではPEARのfactoryメソッド(参照newがPHP7でNG)、owncloud8が動作しなくなりました。

検証環境

OS: FreeBSD10.1
httpd: nginx1.8 (php-fpmにUNIX domain socketで接続)
DB: MySQL5.6
PHP: 5.6 -> 7.0

コア数3、メモリ2GBのVPSで検証
php-fpmのmax_childrenは3に設定

PHP及びextensionsのインストールはpkgで実施

検証対象

  • プレーンなPHP (DBアクセスなし)
  • Wordpress4.4
  • CakePHP3 (DBアクセス軽め)
  • CakePHP3 (DBアクセス重め)

※Wordpressはキャッシュ系のプラグインなど一切入っておらずほぼプレーンです。

検証PHPバージョン

  • PHP5.6 (OPcacheあり/なし)
  • PHP7.0 (OPcacheあり/なし)

検証方法

ApacheBenchで同時接続数50,リクエスト数500回で3回測定し、1秒当たりのリクエスト処理数(Requests per second)の平均値を算出
ちなみにローカルホスト内でのアクセスです。

ab -c 50 -n 500 http://localhost/hogehoge/

検証対象となるプログラムのソースコードには一切手を入れておりません。

検証結果

PHP5.6(with OPcache) PHP5.6(non OPcache) PHP7(with OPcache) PHP7(non OPcache)
プレーンPHP 5973.07 5020.65 6047.06 4398.00
Wordpress 22.82 11.38 62.51 21.23
CakePHP(軽) 20.22 14.15 54.73 29.73
CakePHP(重) 3.81 3.46 7.94 7.23

単位: [req/sec]

【参考】htmlでの測定値: 12892.25 [req/sec]

検証考察

abの結果はそれぞれの測定結果で一番良かった時のものです。

プレーンPHP

特に複雑な処理をしているわけでもないのでPHP5.6もPHP7もほぼ大差はありませんでした。
それでもOPcacheありとなしではかなり差が開くので、OPcache効果は偉大。

PHP5.6
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
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)


Server Software:        nginx
Server Hostname:        localhost
Server Port:            80

Document Path:          /index.php
Document Length:        1266 bytes

Concurrency Level:      50
Time taken for tests:   0.084 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      711000 bytes
HTML transferred:       633000 bytes
Requests per second:    5973.07 [#/sec] (mean)
Time per request:       8.371 [ms] (mean)
Time per request:       0.167 [ms] (mean, across all concurrent requests)
Transfer rate:          8294.64 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.6      0       2
Processing:     2    7   1.2      8       9
Waiting:        2    7   1.2      7       9
Total:          4    8   1.0      8      11
WARNING: The median and mean for the initial connection 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%      8
  66%      8
  75%      9
  80%      9
  90%      9
  95%      9
  98%      9
  99%     10
 100%     11 (longest request)
PHP7
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
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)


Server Software:        nginx
Server Hostname:        localhost
Server Port:            80

Document Path:          /index.php
Document Length:        1266 bytes

Concurrency Level:      50
Time taken for tests:   0.077 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      710500 bytes
HTML transferred:       633000 bytes
Requests per second:    6463.12 [#/sec] (mean)
Time per request:       7.736 [ms] (mean)
Time per request:       0.155 [ms] (mean, across all concurrent requests)
Transfer rate:          8968.84 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.7      0       2
Processing:     2    7   1.2      7       9
Waiting:        2    6   1.1      6       9
Total:          4    7   1.0      8      10
WARNING: The median and mean for the initial connection 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%      8
  66%      8
  75%      8
  80%      8
  90%      8
  95%      9
  98%      9
  99%     10
 100%     10 (longest request)

Wordpress

トップページへのアクセスでの測定です。
PHP7の方が約3倍速いという速度を叩きだしました!
体感的に、すごいサクサク!ってほどではないですが、気持ち速くなったな~ぐらいにはなってます。

PHP5.6
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking wp.localhost (be patient)


Server Software:        nginx
Server Hostname:        wp.localhost
Server Port:            80

Document Path:          /
Document Length:        63074 bytes

Concurrency Level:      50
Time taken for tests:   21.606 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      31646500 bytes
HTML transferred:       31537000 bytes
Requests per second:    23.14 [#/sec] (mean)
Time per request:       2160.562 [ms] (mean)
Time per request:       43.211 [ms] (mean, across all concurrent requests)
Transfer rate:          1430.41 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.8      0       3
Processing:   954 2058 230.9   2027    3352
Waiting:      887 2000 231.3   1968    3295
Total:        957 2058 231.3   2027    3355

Percentage of the requests served within a certain time (ms)
  50%   2027
  66%   2056
  75%   2073
  80%   2078
  90%   2115
  95%   2427
  98%   2999
  99%   3235
 100%   3355 (longest request)
PHP7
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking wp.localhost (be patient)


Server Software:        nginx
Server Hostname:        wp.localhost
Server Port:            80

Document Path:          /
Document Length:        63074 bytes

Concurrency Level:      50
Time taken for tests:   6.939 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      31646000 bytes
HTML transferred:       31537000 bytes
Requests per second:    72.06 [#/sec] (mean)
Time per request:       693.910 [ms] (mean)
Time per request:       13.878 [ms] (mean, across all concurrent requests)
Transfer rate:          4453.64 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.8      0      11
Processing:    69  661 110.1    700     729
Waiting:       54  644 109.6    682     711
Total:         71  661 109.7    700     731

Percentage of the requests served within a certain time (ms)
  50%    700
  66%    702
  75%    704
  80%    705
  90%    707
  95%    709
  98%    710
  99%    711
 100%    731 (longest request)

CakePHP (DBアクセス軽め)

データ取得条件: 抽出条件なし、取得件数約1000レコード、テーブル結合なし、インデックスのソートのみ
DB処理時間:約0.0005秒

Wordpressとさほど変わらないぐらいの速度改善でした。
軽めのクエリを投げる処理だとかなり改善されますね。

CakePHP (DBアクセス重め)

データ取得条件: rangeインデックスで300万レコードから約2000レコード抽出、インデックスによるテーブル結合あり、ソート
DB処理時間:約0.04秒

ボトルネックがPHPではなくDBアクセスになってしまって、2倍程度の改善で歩留まりです。
I/OがネックになるアプリだとPHPをバージョンアップするメリットは少なそうです。
#ちなみにHTTPに比べ約10倍遅くなるといわれているHTTPSでABを実行してみましたがさほど変わりませんでした。

総評

単純に速くなるから、という理由でのバージョンアップすることはオススメしませんが、検討する価値は十分アリ。
PHP5.6に比べ1プロセス当たりのphp-fpmのメモリ使用量も若干減っている気がするので、非力なVPSでの利用も問題なくいけそうです。

14
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
17