PHP服務緩衝最佳化之ZendOpcache、xcache、eAccelerator對比

來源:互聯網
上載者:User

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倍以上...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.