OPcacheの導入

本番サーバにOPcacheを導入してみた。

 

参考にしたサイト

OPcacheを利用してPHPを高速にしよう |

PHPのキャッシュ機構のおさらいと、opcacheの設定とかキャッシュクリアとか - waste of time

OPcache導入してみた!(速さ検証もあるよ!) - Qiita

PHP: インストール手順 - Manual

 

# 推奨設定

opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1

 

# 変更箇所

< opcache.enable_cli=1
< opcache.revalidate_freq=60
< opcache.fast_shutdown=1

 

 

# 計測

ab -n 100 -c 10 https://xxxxx.com

ab -c <同時リクエスト数> -n <総リクエスト数> <URL>

PHP 7をOPcacheとAPCuで高速化する(PHPアクセラレータ) | 己で解決!泣かぬなら己で鳴こうホトトギス

Webサーバー向けのベンチマークツールを使ってみよう(前編)――Apache BenchとSiege | さくらのナレッジ

https://developers.google.com/speed/pagespeed/insights/

https://www.thinkwithgoogle.com/intl/ja-jp/feature/testmysite/

https://gtmetrix.com/

 

 

# 実行コマンド

cd ~
php -v
yum list installed | grep opcache
php -i | grep opcache
sudo yum install php72-opcache
ll /etc/php-7.2.d/10-opcache*
sudo cp -p /etc/php-7.2.d/10-opcache.ini /etc/php-7.2.d/10-opcache.ini.org
sudo vim /etc/php-7.2.d/10-opcache.ini
diff /etc/php-7.2.d/10-opcache.ini /etc/php-7.2.d/10-opcache.ini.org
sudo service httpd restart

 

 

更新間隔を2秒から60秒に変更したので、ファイルに何かしらの変更を加えた際は、

60秒後に更新される。

opcache_reset()

でも更新できるらしいが、CLIからだと何故かうまくいかなった。

60秒待てばいいから、まぁいいかな。

PHP: opcache_reset - Manual

 

 

 

OPcacheを適用した結果、劇的な効果があった。

CPU使用率が、 1/5 〜 1/6 まで下がった。

 

PageSpeedInsightsの結果は少し良くなるくらい。

3点〜9点くらい良くなった。

Largest Contentful Paint が 2.5秒から1.0秒に。PCは2.0秒から0.3秒に大幅に改善。

 

GTmetrixも少し良くなるくらい。

Performanceが90%→93%、Total Blocking Timeが大幅に改善 86ms → 6ms。

 

 

Apache Benchの結果は、

(前)

Requests per second: 13.77 [#/sec] (mean)
Time per request: 726.321 [ms] (mean)
Transfer rate: 1197.76 [Kbytes/sec] received

Connection Times (ms)
Total: 301 676 217.1 668 1283

 

(後)

Requests per second: 14.95 [#/sec] (mean)

Time per request: 668.714 [ms] (mean)

Transfer rate: 1301.12 [Kbytes/sec] received

Connection Times (ms)

Total: 207 635 150.0 666 855

 

微妙に改善といったところだろうか。

 

 

ちなみに、mediumとmicroでの違いもほぼ無し

(micro)

Requests per second: 147.82 [#/sec] (mean)
Time per request: 67.649 [ms] (mean)
Transfer rate: 10257.19 [Kbytes/sec] received

Connection Times (ms)
Total: 44 61 13.5 58 118

 

(medium)

Requests per second: 135.08 [#/sec] (mean)
Time per request: 74.029 [ms] (mean)
Transfer rate: 9373.22 [Kbytes/sec] received

Connection Times (ms)
Total: 42 66 28.3 62 249