欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP开启opcache提升代码性能

程序员文章站 2023-09-02 16:59:17
配置指令如下: [opcache] zend_extension=opcache.so opcache.enable_cli=1 ;共享内存大小, 这个根据...

配置指令如下:

[opcache]
zend_extension=opcache.so
opcache.enable_cli=1
;共享内存大小, 这个根据你们的需求可调
opcache.memory_consumption=256   
;interned string的内存大小, 也可调
opcache.interned_strings_buffer=8
;最大缓存的文件数目
opcache.max_accelerated_files=4000
;60s检查一次文件更新
opcache.revalidate_freq=60
;打开快速关闭, 打开这个在php request shutdown的时候 会收内存的速度会提高
opcache.fast_shutdown=1
;不保存文件/函数的注释
opcache.save_comments=0 

实际性能对比:

下面是实际测试中没有开启opcache的数据:

[root@localhost ~]# ab -n 10000 -c 200 "http://112.126.69.14/main.php?a=role&m=createrole"
this is apachebench, version 2.3 <$revision: 655654 $>
copyright 1996 adam twiss, zeus technology ltd, http://www.zeustech.net/
licensed to the apache software foundation, http://www.apache.org/

benchmarking 112.126.69.14 (be patient)
completed 1000 requests
completed 2000 requests
completed 3000 requests
completed 4000 requests
completed 5000 requests
completed 6000 requests
completed 7000 requests
completed 8000 requests
completed 9000 requests
completed 10000 requests
finished 10000 requests

server software:    openresty/1.7.2.1
server hostname:    112.126.69.14
server port:      80

document path:     /main.php?a=role&m=createrole
document length:    2 bytes

concurrency level:   200
time taken for tests:  26.061 seconds
complete requests:   10000
failed requests:    20
  (connect: 0, receive: 0, length: 20, exceptions: 0)
write errors:      0
non-2xx responses:   20
total transferred:   1713580 bytes
html transferred:    23520 bytes
requests per second:  383.72 [#/sec] (mean)
time per request:    521.216 [ms] (mean)
time per request:    2.606 [ms] (mean, across all concurrent requests)
transfer rate:     64.21 [kbytes/sec] received

connection times (ms)
       min mean[+/-sd] median  max
connect:    2  3  3.2   2   60
processing:  17 461 905.0  219  16496
waiting:    17 461 904.9  219  16496
total:     21 464 905.0  222  16502

percentage of the requests served within a certain time (ms)
 50%  222
 66%  271
 75%  369
 80%  412
 90%  805
 95%  1248
 98%  2597
 99%  3489
 100% 16502 (longest request)

开启之后的数据:

[root@localhost ~]# ab -n 10000 -c 200 "http://112.126.69.14/main.php?a=role&m=createrole"
this is apachebench, version 2.3 <$revision: 655654 $>
copyright 1996 adam twiss, zeus technology ltd, http://www.zeustech.net/
licensed to the apache software foundation, http://www.apache.org/

benchmarking 112.126.69.14 (be patient)
completed 1000 requests
completed 2000 requests
completed 3000 requests
completed 4000 requests
completed 5000 requests
completed 6000 requests
completed 7000 requests
completed 8000 requests
completed 9000 requests
completed 10000 requests
finished 10000 requests


server software:    openresty/1.7.2.1
server hostname:    112.126.69.14
server port:      80

document path:     /main.php?a=role&m=createrole
document length:    2 bytes

concurrency level:   200
time taken for tests:  14.237 seconds
complete requests:   10000
failed requests:    0
write errors:      0
total transferred:   1711710 bytes
html transferred:    20020 bytes
requests per second:  702.40 [#/sec] (mean)
time per request:    284.739 [ms] (mean)
time per request:    1.424 [ms] (mean, across all concurrent requests)
transfer rate:     117.41 [kbytes/sec] received

connection times (ms)
       min mean[+/-sd] median  max
connect:    2  66 272.6   2  3005
processing:   4 176 666.4   6  9026
waiting:    4 163 642.8   6  9026
total:     6 242 745.7   9  10028

percentage of the requests served within a certain time (ms)
 50%   9
 66%   14
 75%   99
 80%  122
 90%  1006
 95%  1476
 98%  2853
 99%  3543
 100% 10028 (longest request)

以上所述就是本文的全部内容,希望大家能够喜欢。