[php服务缓存优化之怎么打开]PHP服务缓存优化之ZendOpcache、xcache、eAccelerator对比

更新时间:2020-10-31    来源:nginx    手机版     字体:

【www.bbyears.com--nginx】

PHP服务缓存优化原理


Nginx 根据扩展名或者过滤规则将PHP程序请求传递给解析PHP的FCGI,也就是php-fpm进程


缓存操作码(opcode)

Opcode,PHP编译后的中间文件,缓存给用户访问

当客户端请求一个PHP程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码文件,该文件是执行PHP代码后的一种二进制文件表现形式。默认情况下,这个编译好的操作码文件由PHP引擎执行后丢弃;而操作码缓存的原理就是将编译后的操作码保存下来,并放入到共享内存里,以便再下一次调用该PHP页面时重用它,避免了相同代码的重复编译。节省了PHP引擎重复编译的时间,降低了服务器负载,同时减少了CPU和内存的开销.

常用的PHP缓存加速软件

1)xcache

经测试,xcache效率更好、社区活跃、兼容PHP版本多

2)ZendOpcache

Apache公司自主研发软件,5.5版本以后自带,加enbale-opcache编译参数直接使用,但是软件稳定性有待检测

3)eAccelerator

5.3版本以前经常使用的加速软件,随着5.5版本升级,和xcache等优秀软件的出现,社区活跃度开始下降

缓存软件首选xcahe、持续关注ZendOpcache...

xcache部署

1)下载xcache,添加为PHP扩展模块,编译安装

[root@web01 tools]# wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.bz2
[root@web01 tools]# tar xf xcache-3.2.0.tar.bz2
[root@web01 tools]# cd xcache-3.2.0
[root@web01 xcache-3.2.0]# /application/php/bin/phpize
[root@web01 xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/application/php/bin/php-config
[root@web01 xcache-3.2.0]# make && make install
...
Installing shared extensions:     /application/php5.5.32/lib/php/extensions/no-debug-non-zts-20121212/
Installing header files:          /application/php5.5.32/include/php/
2)配置php扩展生效

[root@db02 application]# vim /application/php/lib/php.ini
extension_dir = "/application/php5.5.32/lib/php/extensions/no-debug-non-zts-20121212/"
extension = memcache.so
extension = imagick.so
extension = xcache.so
3)重启php后模块生效

[root@db02 application]# /application/php/bin/php -m|grep "XCache"
XCache
XCache Cacher
4)xcache配置文件

[root@db02 ~]# cat ~/tools/xcache-3.2.0/xcache.ini|egrep -v "^;|^ " >> /application/php/lib/php.ini
[xcache-common]
extension = xcache.so #模块
 
[xcache.admin]
xcache.admin.enable_auth = On #开启密码认证
 
xcache.admin.user = "mOo"
xcache.admin.pass = "md5 encrypted password"
 
[xcache]
 
xcache.shm_scheme =        "mmap" #设置Xcache如何从系统分配共享内存
xcache.size  =               60M    #缓存大小,0禁止缓存
xcache.count =                 1    #指定将xcache切分为多少块,建议与CPU核数相同(grep -c processor /proc/cpuinfo)
xcache.slots =                8K
xcache.ttl   =                 0 #设置cache对象生存期TTL,0永不过期;如果上线次数多,调小
xcache.gc_interval =           0 #回收器扫描过期的对象回收内存空间的间隔,0不扫描
xcache.var_size  =            4M  #变量缓存,而不是opcache缓存
xcache.var_count =             1
xcache.var_slots =            8K
xcache.var_ttl   =             0
xcache.var_maxttl   =          0
xcache.var_gc_interval =     300
 
xcache.var_namespace_mode =    0
xcache.var_namespace =        ""
xcache.coredump_type =         0
5)查看PHP chache加载情况

[root@db02 ~]# /application/php/sbin/php-fpm -v
PHP 5.5.32 (fpm-fcgi) (built: Jun 29 2016 11:32:56)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
    with XCache v3.2.0, Copyright (c) 2005-2014, by mOo
with XCache Cacher v3.2.0, Copyright (c) 2005-2014, by mOo
6)web界面配置

[root@db02 ~]# echo -n "123456"|md5sum
e10adc3949ba59abbe56e057f20f883e  -
[root@db02 ~]# cp ~/tools/xcache-3.2.0/htdocs /application/nginx/html/www/xadmin -a
[root@db02 ~]# vim /application/php/lib/php.ini
[Date]
date.timezone = Asia/Chongqing
[xcache.admin]
xcache.admin.enable_auth = On
 
xcache.admin.user = "admin"
xcache.admin.pass = "e10adc3949ba59abbe56e057f20f883e"
[root@db02 ~]# pkill php-fpm
[root@db02 ~]# /application/php/sbin/php-fpm

ab压力测试效果

1)未加xcache之前

[root@db02 application]# ab -n 3000 -c 100 http://10.0.0.111/test_info.php
# 3000次会话请求、100并发数
Server Software:        nginx/1.6.3
Server Hostname:        10.0.0.111
Server Port:            80
Document Path:          /test_info.php #测试页面
Document Length:        83921 bytes #页面大小
Concurrency Level:      100 #100并发数
Time taken for tests:   7.973 seconds #整个测试持续时间
Complete requests:      3000 #完成的请求总数
Failed requests:        302 #失败的请求次数
   (Connect: 0, Receive: 0, Length: 302, Exceptions: 0)
Write errors:           0
Total transferred:      252203675 bytes #整个过程的网络传输量
HTML transferred:       251762675 bytes #HTML内容传输量
Requests per second:    376.25 [#/sec] (mean) #吞吐量,每秒能够处理的并发数
Time per request:       265.779 [ms] (mean) #平均事务响应时间
Time per request:       2.658 [ms] (mean, across all concurrent requests)
#每个连接请求实际运行时间
Transfer rate:          30889.42 [Kbytes/sec] received
#平均每秒网络上的流量,可以帮助排除是否存在网络流量大导致响应时间延长的问题
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   3.2      0      21
Processing:    14  261  32.2    261     331
Waiting:        2  260  32.4    260     331
Total:         29  261  29.9    261     331
Percentage of the requests served within a certain time (ms)
  50%    261
  66%    268
  75%    273
  80%    276
  90%    287 #90%的请求任务在287ms内完成
  95%    303
  98%    315
  99%    322
 100%    331 (longest request)
2)配置xache之后

[root@db02 application]# ab -n 3000 -c 100 http://10.0.0.111/test_info.php
Server Software:        nginx/1.6.3
Server Hostname:        10.0.0.111
Server Port:            80
Document Path:          /test_info.php
Document Length:        172 bytes
Concurrency Level:      100
Time taken for tests:   0.516 seconds
Complete requests:      3000
Failed requests:        0
Write errors:           0
Non-2xx responses:      3000
Total transferred:      969000 bytes
HTML transferred:       516000 bytes
Requests per second:    5819.42 [#/sec] (mean) #并发数上升为5000+
Time per request:       17.184 [ms] (mean)
Time per request:       0.172 [ms] (mean, across all concurrent requests)
Transfer rate:          1835.62 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.3      0       9
Processing:     6   17   2.1     16      21
Waiting:        0   17   2.2     16      21
Total:          7   17   1.6     16      21
Percentage of the requests served within a certain time (ms)
  50%     16
  66%     17
  75%     18
  80%     19
  90%     19
  95%     19
  98%     20
  99%     21
 100%     21 (longest request)
由于是虚机测试环境,不一定十分准确,未安装xcache并发数在400-500,安装后并发数在5000左右,缓存效果提升10倍以上...

本文来源:http://www.bbyears.com/caozuoxitong/107959.html