php OPcacheパフォーマンス

phpにOPcacheを入れてどれくらいパフォーマンスが変わるかテストする。

```Dockerfile
FROM centos:centos8

RUN dnf module install -y php:7.4

COPY ./laravel /app

WORKDIR /app

CMD php artisan serve --host='0.0.0.0' --port='80'
```


```
$ docker build -t php_test . 
$ docker run -it --rm -p 8000:80 php_test
php -v
PHP 7.4.6 (cli) (built: May 12 2020 08:09:15) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
```


![スクリーンショット 2020-12-17 12.52.44.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/278474/1ac2237b-e7d4-3845-9129-a7488a42c1fa.png)

## まずはOPcacheが入っていない状態で計測
1秒で15.78リクエスト捌ける。

```
$ ab -c 10 -n 100 http://localhost:8000/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
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).....done


Server Software:        
Server Hostname:        localhost
Server Port:            8000

Document Path:          /
Document Length:        17473 bytes

Concurrency Level:      10
Time taken for tests:   6.337 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      1861900 bytes
HTML transferred:       1747300 bytes
Requests per second:    15.78 [#/sec] (mean)
Time per request:       633.652 [ms] (mean)
Time per request:       63.365 [ms] (mean, across all concurrent requests)
Transfer rate:          286.95 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:    80  596 110.3    624     666
Waiting:       80  596 110.4    624     666
Total:         81  597 110.3    624     666

Percentage of the requests served within a certain time (ms)
  50%    624
  66%    629
  75%    633
  80%    635
  90%    639
  95%    646
  98%    647
  99%    666
 100%    666 (longest request)
```

## OPcacheありで計測

opcacheのインストール。インストールされるとphp -vでZend OPcacheと表示される

```Dockerfile
FROM centos:centos8

RUN dnf module install -y php:7.4
RUN dnf install php-opcache -y

COPY ./laravel /app

WORKDIR /app

CMD php artisan serve --host='0.0.0.0' --port='80'

```

```
[root@9dcc11702f34 app]# php -v
PHP 7.4.6 (cli) (built: May 12 2020 08:09:15) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.6, Copyright (c), by Zend Technologies
```

秒間107.10リクエスト捌ける。

```
ab -c 10 -n 100 http://localhost:8000/ 
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
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).....done


Server Software:        
Server Hostname:        localhost
Server Port:            8000

Document Path:          /
Document Length:        17473 bytes

Concurrency Level:      10
Time taken for tests:   0.934 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      1861900 bytes
HTML transferred:       1747300 bytes
Requests per second:    107.10 [#/sec] (mean)
Time per request:       93.374 [ms] (mean)
Time per request:       9.337 [ms] (mean, across all concurrent requests)
Transfer rate:          1947.30 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     8   72  19.3     73     193
Waiting:        8   72  19.2     73     192
Total:          9   73  19.2     73     193

Percentage of the requests served within a certain time (ms)
  50%     73
  66%     78
  75%     82
  80%     84
  90%     87
  95%     89
  98%     90
  99%    193
 100%    193 (longest request)
```


## まとめ
OPcache導入すると6倍くらい速くなる。
dockerでマウントすると読み込みも遅くなるからOPcacheで読み込みキャッシュすると速くなるのかな。
積極的にOPcache入れた方が良さそう。