PHP服务缓存优化原理php
Nginx 根据扩展名或者过滤规则将PHP程序请求传递给解析PHP的FCGI,也就是php-fpm进程html
缓存操做码(opcode)nginx
Opcode,PHP编译后的中间文件,缓存给用户访问web
当客户端请求一个PHP程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操做码文件,该文件是执行PHP代码后的一种二进制文件表现形式。默认状况下,这个编译好的操做码文件由PHP引擎执行后丢弃;而操做码缓存的原理就是将编译后的操做码保存下来,并放入到共享内存里,以便再下一次调用该PHP页面时重用它,避免了相同代码的重复编译。节省了PHP引擎重复编译的时间,下降了服务器负载,同时减小了CPU和内存的开销.vim
经常使用的PHP缓存加速软件缓存
1)xcacheruby
经测试,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/
[root@db02 application]# vim /application/php/lib/php.iniextension_dir = "/application/php5.5.32/lib/php/extensions/no-debug-non-zts-20121212/"extension = memcache.soextension = imagick.soextension = xcache.so
[root@db02 application]# /application/php/bin/php -m|grep "XCache" XCache XCache Cacher
[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 = 8Kxcache.ttl = 0 #设置cache对象生存期TTL,0永不过时;若是上线次数多,调小xcache.gc_interval = 0 #回收器扫描过时的对象回收内存空间的间隔,0不扫描xcache.var_size = 4M #变量缓存,而不是opcache缓存xcache.var_count = 1xcache.var_slots = 8Kxcache.var_ttl = 0xcache.var_maxttl = 0xcache.var_gc_interval = 300 xcache.var_namespace_mode = 0xcache.var_namespace = ""xcache.coredump_type = 0
[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
[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压力测试效果
[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.111Server Port: 80Document 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: 0Total 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 maxConnect: 0 1 3.2 0 21Processing: 14 261 32.2 261 331Waiting: 2 260 32.4 260 331Total: 29 261 29.9 261 331Percentage 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)
[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)