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)