PHP OPcache性能

测试在php中加入OPcache后性能会有多大改变。

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

首先,在未安装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。

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似乎是更好的选择。

广告
将在 10 秒后关闭
bannerAds