PHP7が更に爆速になると噂のOPcacheとAPCuをインストールしてテストする。
WordPressを利用しているサイトでは効果絶大との情報だが、はたしてどれだけの速度増加が認められるだろうか?
前回のサイトの表示速度測定がこちら↓
早速試してみる。
※動作確認環境
CentOS 7.6
PHP 7.2.13
Nginx 1.14.2
PHPアクセラレータとは
PHPアクセラレータとはPHPを高速化するための拡張機能です。「OPcache」や「APCu」などがそれにあたります。
機能としては、アクセスされたPHPスクリプトのソースコードがコンパイルされた段階で中間キャッシュとしてメモリに保存し、以降同じPHPスクリプトが実行された場合はメモリに保存された中間キャッシュを再利用することが出来るようになります。
ちなみにPHPスクリプトの実行時は以下の順番で実行されます。
- PHPスクリプトにアクセス
- コンパイル
- 中間コード生成
- 実行
上記の「コンパイル」の部分がキャッシュ利用により省略されることで単純に高速化する仕組みで、「OPcache」はソースコード全般を担当し、「APCu」はオブジェクトや変数を担当します。
では早速インストールしていきます。
「OPcache」「APCu」のインストール
現在のPHPのバージョン調査
まず、現在のPHPのバージョンを調べます
# php -v
PHP 7.2.13 (cli) (built: Dec 8 2018 12:11:34) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
インストール
PHP 7.2用の「remi-php72」を利用してphp-opcacheとphp-pecl-apcuをインストールします。
※PHPのバージョンに合ったremi-phpを利用してインストールしてください。(PHP 7.2は「remi-php72」、PHP 7.1は「remi-php71」、PHP 7.0は「remi-php70」等)
# yum -y install --enablerepo=epel,remi,remi-php72 php-opcache php-pecl-apcu
読み込んだプラグイン:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: ftp.iij.ad.jp
* epel: ftp.iij.ad.jp
* extras: ftp.iij.ad.jp
* remi: ftp.riken.jp
* remi-php72: ftp.riken.jp
* remi-safe: ftp.riken.jp
* updates: ftp.iij.ad.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ php-opcache.x86_64 0:7.2.13-2.el7.remi を インストール
---> パッケージ php-pecl-apcu.x86_64 0:5.1.16-1.el7.remi.7.2 を インストール
--> 依存性解決を終了しました。
依存性を解決しました
===================================================================================================
Package アーキテクチャー
バージョン リポジトリー 容量
===================================================================================================
インストール中:
php-opcache x86_64 7.2.13-2.el7.remi remi-php72 282 k
php-pecl-apcu x86_64 5.1.16-1.el7.remi.7.2 remi-php72 75 k
トランザクションの要約
===================================================================================================
インストール 2 パッケージ
総ダウンロード容量: 357 k
インストール容量: 1.1 M
Downloading packages:
(1/2): php-pecl-apcu-5.1.16-1.el7.remi.7.2.x86_64.rpm | 75 kB 00:00:00
(2/2): php-opcache-7.2.13-2.el7.remi.x86_64.rpm | 282 kB 00:00:00
---------------------------------------------------------------------------------------------------
合計 1.2 MB/s | 357 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
インストール中 : php-pecl-apcu-5.1.16-1.el7.remi.7.2.x86_64 1/2
インストール中 : php-opcache-7.2.13-2.el7.remi.x86_64 2/2
検証中 : php-opcache-7.2.13-2.el7.remi.x86_64 1/2
検証中 : php-pecl-apcu-5.1.16-1.el7.remi.7.2.x86_64 2/2
インストール:
php-opcache.x86_64 0:7.2.13-2.el7.remi php-pecl-apcu.x86_64 0:5.1.16-1.el7.remi.7.2
完了しました!
インストール後の確認
ちゃんとインストールできたか調べます。
# php -v
PHP 7.2.13 (cli) (built: Dec 8 2018 12:11:34) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.13, Copyright (c) 1999-2018, by Zend Technologies
OKですね。「with Zend OPcache v7.2.13」を確認しました。
設定ファイルの調査
設定ファイルを調べます。
# php -i | grep opcache
Additional .ini files parsed => /etc/php.d/10-opcache.ini,
opcache.blacklist_filename => /etc/php.d/opcache*.blacklist => /etc/php.d/opcache*.blacklist
opcache.consistency_checks => 0 => 0
opcache.dups_fix => Off => Off
opcache.enable => On => On
opcache.enable_cli => Off => Off
opcache.enable_file_override => Off => Off
opcache.error_log => no value => no value
opcache.file_cache => no value => no value
opcache.file_cache_consistency_checks => 1 => 1
opcache.file_cache_only => 0 => 0
opcache.file_update_protection => 2 => 2
opcache.force_restart_timeout => 180 => 180
opcache.huge_code_pages => On => On
opcache.inherited_hack => On => On
opcache.interned_strings_buffer => 8 => 8
opcache.lockfile_path => /tmp => /tmp
opcache.log_verbosity_level => 1 => 1
opcache.max_accelerated_files => 4000 => 4000
opcache.max_file_size => 0 => 0
opcache.max_wasted_percentage => 5 => 5
opcache.memory_consumption => 128 => 128
opcache.opt_debug_level => 0 => 0
opcache.optimization_level => 0x7FFFBFFF => 0x7FFFBFFF
opcache.preferred_memory_model => no value => no value
opcache.protect_memory => 0 => 0
opcache.restrict_api => no value => no value
opcache.revalidate_freq => 2 => 2
opcache.revalidate_path => Off => Off
opcache.save_comments => 1 => 1
opcache.use_cwd => On => On
opcache.validate_permission => Off => Off
opcache.validate_root => Off => Off
opcache.validate_timestamps => On => On
設定ファイルは「/etc/php.d/10-opcache.ini」ですね。
設定ファイルをカスタマイズ(任意)
設定をカスタマイズしたい場合は、PHPの公式マニュアルを参照してカスタマイズしてください。
■HPの公式マニュアル(opcache.configuration)
URL: http://php.net/manual/ja/opcache.configuration.php
opcache.enable boolean
オペコード・キャッシュを有効にします。 無効にした場合、コードは最適化もキャッシュもされません。 opcache.enable の設定を、実行時に ini_set() で有効化することはできません。 実行時にできるのは、無効化だけです。スクリプト内で有効化しようとすると、警告が発生します。opcache.enable_cli boolean
PHP の CLI 版に対してオペコード・キャッシュを有効にします。opcache.memory_consumption integer
OPcache によって使用される共有メモリ・ストレージのサイズ。( MB 単位)opcache.interned_strings_buffer integer
インターン (intern) された文字列を格納するために使用されるメモリ量。( MB 単位) この設定ディレクティブの値は、PHP 5.3.0 未満では無視されます。opcache.max_accelerated_files integer
OPcache ハッシュテーブルのキー(すなわちスクリプト)の最大数。 使用される現時点の値は、 素数の集合 { 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987 } のうち、 設定値以上の最初の数値です。 最小値は 200 です。最大値は、PHP 5.5.6 より前のバージョンでは 100000、PHP 5.5.6 以降では 1000000 となります。
↑このあたりでOPcacheの基本的なカスタマイズができます。
※当方はデフォルト設定のままにしました。
WEBサーバー再起動
※Apacheの場合
# systemctl restart httpd
※Nginxの場合
# systemctl restart php-fpm
# systemctl restart nginx
以上でインストール作業は完了です。
速度比較テスト
まずキャッシュを貯めるためにブラウザで何度かサイトにアクセスしてから再度測定を行います。
Apache Benchによるテスト
■インストール前
# ab -n 100 -c 10 https://onoredekaiketsu.com/
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 onoredekaiketsu.com (be patient).....done
Server Software: nginx
Server Hostname: onoredekaiketsu.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
Document Path: /
Document Length: 401557 bytes
Concurrency Level: 10
Time taken for tests: 29.444 seconds
Complete requests: 100
Failed requests: 99
(Connect: 0, Receive: 0, Length: 99, Exceptions: 0)
Write errors: 0
Total transferred: 39939618 bytes
HTML transferred: 39902218 bytes
Requests per second: 3.40 [#/sec] (mean)
Time per request: 2944.360 [ms] (mean)
Time per request: 294.436 [ms] (mean, across all concurrent requests)
Transfer rate: 1324.69 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 2 6 7.7 2 39
Processing: 1096 2744 537.3 2975 3421
Waiting: 1068 2733 539.0 2957 3416
Total: 1109 2750 536.5 2978 3424
Percentage of the requests served within a certain time (ms)
50% 2978
66% 3135
75% 3163
80% 3186
90% 3300
95% 3341
98% 3406
99% 3424
100% 3424 (longest request)
■インストール後
# ab -n 100 -c 10 https://onoredekaiketsu.com/
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 onoredekaiketsu.com (be patient).....done
Server Software: nginx
Server Hostname: onoredekaiketsu.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
Document Path: /
Document Length: 400036 bytes
Concurrency Level: 10
Time taken for tests: 14.713 seconds
Complete requests: 100
Failed requests: 99
(Connect: 0, Receive: 0, Length: 99, Exceptions: 0)
Write errors: 0
Total transferred: 40027814 bytes
HTML transferred: 39990414 bytes
Requests per second: 6.80 [#/sec] (mean)
Time per request: 1471.260 [ms] (mean)
Time per request: 147.126 [ms] (mean, across all concurrent requests)
Transfer rate: 2656.88 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 2 5 5.1 2 23
Processing: 317 1385 266.8 1414 1825
Waiting: 300 1376 269.2 1405 1817
Total: 322 1390 265.9 1416 1829
Percentage of the requests served within a certain time (ms)
50% 1416
66% 1538
75% 1577
80% 1594
90% 1701
95% 1749
98% 1812
99% 1829
100% 1829 (longest request)
【結果】
Requests per second: 3.40 → 6.80 ※3.40[#/sec]アップ!
Time per request: 2944.360 → 1471.260 ※1473.1[ms]減少!
Time per request: 294.436 → 147.126 ※147.31[ms]減少!
Transfer rate: 1324.69 → 2656.88 ※1332.19[Kbytes/sec]アップ!
※全てにおいて約2倍のパフォーマンスとなっています。
PageSpeed Insights
URL: https://developers.google.com/speed/pagespeed/insights/
■インストール前

■インストール後

【結果】
スマホ:スコア73 → スコア73 ※変化なし
PC:スコア98 → スコア99 ※1ポイントアップ!
Test My Site(テストマイサイト)
URL: https://testmysite.withgoogle.com/intl/ja-jp
■インストール前

■インストール後

【結果】
3Gでの読み込み時間:4秒(良好)→ 3秒(特に良好)※1秒アップ!
訪問者の推定離脱率:10% → 低 ※改善!
Pingdom Website Speed Test
URL: https://tools.pingdom.com/
■インストール前

■インストール後

【結果】
Performance grade:82 → 82 ※変化なし
Load time:2.29秒 → 1.76秒 ※0.32秒短縮!
※PHPの実行速度に関係のあるものだけ抜粋
GTmetrix
■インストール前

■インストール後

【結果】
PageSpeed Score:A(91%)→ A(91%)※変化なし
YSlow Score:C(79%)→ C(79%)※変化なし
Fully Loaded Time:3.3秒 → 3.0秒 ※0.3秒減少!
※PHPの実行速度に関係のあるものだけ抜粋
総評
インストール作業はとても簡単です。
今回に実際にやったことは、ある意味以下の一行をSSHで実行してWEBサーバーを再起動しただけ。(※remi-php72はPHPのバージョンにより変えてください。)
# yum -y install --enablerepo=epel,remi,remi-php72 php-opcache php-pecl-apcu簡単な手順でPHP自体の速度アップが可能なので、これは必ずインストールしておくべきですね。
更に今後も高速化のチャレンジを続けていきます。


