PHP开启opcache提升代码性能

 更新时间:2015年04月26日 16:25:05   投稿:hebedich  
APC在PHP5.4及以下版本是性能最好的代码缓存。不过PHP升级到5.5及以上后,APC不再有效。需要使用Zend的OpCache扩展。

配置指令如下:

[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)

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

相关文章

  • PHP之uniqid()函数用法

    PHP之uniqid()函数用法

    这篇文章主要介绍了PHP中uniqid()函数的用法,包括了函数的基本用法与应用分析,是非常实用的技巧,需要的朋友可以参考下
    2014-11-11
  • PHP内存溢出的解决方法详解

    PHP内存溢出的解决方法详解

    内存溢出是指应用系统中存在无法回收的内存或使用的内存过多,最终使得程序运行要用到的内存大于虚拟机能提供的最大内存。本文为大家总结了PHP内存溢出的解决方法,需要的可以参考一下
    2022-07-07
  • PHP回调函数概念与用法实例分析

    PHP回调函数概念与用法实例分析

    这篇文章主要介绍了PHP回调函数概念与用法,简单介绍了回调函数的概念、原理,并结合实例形式分析了回调函数的相关使用技巧,需要的朋友可以参考下
    2017-11-11
  • php设计模式 Interpreter(解释器模式)

    php设计模式 Interpreter(解释器模式)

    php设计模式 Interpreter(解释器模式),需要的朋友可以参考下。
    2011-06-06
  • php进行md5加密简单实例方法

    php进行md5加密简单实例方法

    在本文里小编给大家整理了一篇非常实用的php如何进行md5加密知识点内容,有需要的朋友们可以参考下。
    2019-09-09
  • PHP的Yii框架中View视图的使用进阶

    PHP的Yii框架中View视图的使用进阶

    这篇文章主要介绍了PHP的Yii框架中View视图的使用进阶,包括布局和数据块的创建使用等,需要的朋友可以参考下
    2016-03-03
  • PHP对象递归引用造成内存泄漏分析

    PHP对象递归引用造成内存泄漏分析

    这篇文章主要介绍了PHP对象递归引用造成内存泄漏分析,是PHP程序设计中需要加以重视的一个bug,并且有助于深入理解PHP运行原理,需要的朋友可以参考下
    2014-08-08
  • php获取网卡的MAC地址支持WIN/LINUX系统

    php获取网卡的MAC地址支持WIN/LINUX系统

    这篇文章主要介绍了使用php获取网卡的MAC地址支持WIN/LINUX系统,需要的朋友可以参考下
    2014-04-04
  • 详解phpmyadmin相关配置与错误解决

    详解phpmyadmin相关配置与错误解决

    这篇文章主要介绍了详解phpmyadmin相关配置与错误解决的相关资料,希望通过本文能帮助到大家,让大家遇到这种问题可以解决,需要的朋友可以参考下
    2017-10-10
  • PHP中使用Imagick读取pdf并生成png缩略图实例

    PHP中使用Imagick读取pdf并生成png缩略图实例

    这篇文章主要介绍了PHP中使用Imagick读取pdf并生成png缩略图实例,本文直接给出实现代码,需要的朋友可以参考下
    2015-01-01

最新评论