關於PHP加速eAccelerator、Xcache、APC和Zend Optimizer

之前只關注過Zend Optimizer,由於高胖子的書就是這樣教的,可是遇到奇葩公司的面試題提問你知道多少個php加速器/緩存,我一會兒楞了,由於我所知道的php5.2.x只用過Zend Optimizer,並且加密過的php程序只能在Zend Optimizer下面運行。可是php5.3.x以上版本,Zend Optimizer支持不了了,更別提eAccelerator、Xcache、APC。Zend guard loader能夠支持php5.3.x以上版本,並且最新的php5.5.x版本,官網說內置了Zend guard,因此更不要phper關注加速器了。既然遇到這樣的問題,也從網上總結了一下東西,網友們看過就好了,沒必要去試驗了。php

安裝哪個呢?對性能會有什麼影響呢?有時間要測試一下。html

從別處看來的。面試

程序環境非必要Zend Optimizer的狀況下,首選pecl-APC(它和Zend Optimizer不兼容)。
總結:
1.PHP緩衝(加速)首選pecl-APC,兼容性和性能表現都很是優秀。
2.若是你的PHP環境須要Zend Optimizer,就安裝eAccelerator,並把Zend Optimizer的壓縮級別調到0。apache

zend optimizer 是一個代碼優化的模塊,能夠調優php代碼,實現的原理是對那些在被最終執行以前由運行編譯器(Run-Time Compiler)產生的代碼進行優化。代碼性能能夠提升40%到100%,從這點上來講,應該不具備強大的緩存功能,沒有讀過源碼,不清楚是否具備緩存 以及緩存質量如何。

eAccelerator 是一個將編譯以後的php代碼緩存在share memory中的模塊。經過訪問共享內存能夠獲得編譯後的代碼並直接執行用以提升效率,這個對於php的執行效率的提升仍是很大的。同 時,eAccelerator也能夠緩存數據至文件中,這個部分因爲是對文件的操做,我想對大多數的文件cache來講,原理類似,性能相近。

APC 原理上來說和eAccelerator相近,因此差異不大。不經過修改參數詳細測試,不能看出二者的優劣。因此取其一便可。ubuntu

 

 

 

三款免費的PHP加速器:APC、eAccelerator、XCache比較緩存

 

1、PHP加速器介紹服務器

        PHP加速器是一個爲了提升PHP執行效率,從而緩存起PHP的操做碼,這樣PHP後面執行就不用解析轉換了,能夠直接調用PHP操做碼,這樣速度上就提升了很多。併發

        Apache中使用mod_php的請求、響應執行流程:工具

  一、Apache接收請求。
二、Apache傳遞請求給mod_php。
三、mod_php定位磁盤文件,並加載到內存中。
四、mod_php編譯源代碼成爲opcode樹。
五、mod_php執行opcode樹。性能

       PHP加速器相應的就是第四步,它的目的就是防止PHP每次請求都重複編譯PHP代碼,由於在高訪問量的網站上,大量的編譯每每沒有執行速度快呢?因此這 裏面有個瓶頸就是PHP的重複編譯既影響了速度又加載了服務器負載,爲了解決此問題,PHP加速器就這樣誕生了。

2、PHP加速器安裝與配置

        一、安裝配置APC

             APC全稱是Alternative PHP Cache,官方翻譯叫」可選PHP緩存」,它是PHP PECL中的一個擴展,好像是facebook在使用它,下面開始安裝(ubuntu環境): 
$wget http://pecl.php.net/get/APC-3.0.19.tgz
$tar xvzf APC-3.0.19.tgz
$cd APC-3.0.19/APC-3.0.19
$/usr/local/php/bin/phpize
$./configure –enable-apc –enable-apc-mmap –with-php-config=/usr/local/php/bin/php-config
$make
$sudo make install

下面咱們再配置APC,由於個人PECL擴展路徑改變了,因此我得移動下編譯好的文件:
$sudo mv /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/apc.so /usr/local/php/lib/php/extensions/PECL

而後咱們再編輯php.ini文件進行配置,請把下面的代碼加入到php.ini中便可:
extension_dir = "/usr/local/php/lib/php/extensions/PECL"
extension = apc.so
; APC
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 64
apc.optimization = 1
apc.num_files_hint = 0
apc.ttl = 0
apc.gc_ttl = 3600
apc.cache_by_default = on

     這樣重啓apache就會在phpinfo()信息中顯示。

       二、安裝配置eAccelerator

          eAccelerator的前身實際上是truck-mmcache,由於開發truk-mmcache的人被Zend給招安了,因此開發 eAccelerator的人繼承了truk-mmcache的一些特性,設計出eAccelerator加速器。安裝以下:
$wget http://jaist.dl.sourceforge.net/sourceforge/eaccelerator/eaccelerator-0.9.5.tar.bz2
$tar -jxf eaccelerator-0.9.5.tar.bz2
$cd eaccelerator-0.9.5
$/usr/local/php/bin/phpize
$./configure –enable-eaccelerator=shared –with-php-config=/usr/local/php/bin/php-config
$make
$sudo make install
$sudo mv /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so /usr/local/php/lib/php/extensions/PECL

將下面代碼加入php.ini文件中
extension = eaccelerator.so
; eAccelerator
eaccelerator.shm_size = "16"
eaccelerator.cache_dir = "/tmp/eaccelerator"
eaccelerator.enable = "1"
eaccelerator.optimizer = "1"
eaccelerator.check_mtime = "1"
eaccelerator.debug = "0"
eaccelerator.filter = ""
eaccelerator.shm_max = "0"
eaccelerator.shm_ttl = "0"
eaccelerator.prune_period = "0"
eaccelerator.shm_only = "0"
eaccelerator.compress = "1"
eaccelerator.compress_level = "9"

建立緩存目錄,重啓apache

$sudo mkdir /tmp/eaccelerator
$sudo chmod 777 /tmp/eaccelerator
$sudo /usr/local/apache/apachectl restart

在phpinfo()檢查是否安裝成功.

三、安裝配置XCache

XCache做爲國人本身開發的東西,作小菜鳥的我也感到驕傲,並且XCache不管在速度仍是性能上都作的不錯。下面就趕忙讓咱們品嚐它吧!

$wget http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz
$tar xvzf xcache-1.2.2.tar.gz
$cd xcache-1.2.2
$/usr/local/php/bin/phpize
$./configure –enable-xcache –enable-xcache-coverager –with-php-config=/usr/local/php/php-config
$make
$sudo make install
$sudo mv /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so /usr/local/php/lib/php/extensions/PECL

在php.ini添加配置信息:

extension = xcache.so
; xcache
xcache.admin.user = "admin"
xcache.admin.pass = "(執行) echo ’(你的密碼)’|md5sum(得出的密文)"
;
xcache.size = 24M
xcache.shm_scheme = "mmap"
xcache.count = 2
xcache.slots = 8k
xcache.ttl = 0
xcache.gc_interval = 0

xcache.var_size = 8M
xcache.var_count = 1
xcache.var_slots = 8k
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.test = Off
xcache.readonly_protection = On
xcache.mmap_path = "/tmp/xcache"
xcache.coredump_directory = ""
xcache.cacher = On
xcache.stat = On
xcache.optimizer = Off
;
xcache.coverager = On
xcache.coveragedump_directory = ""

建立緩存目錄,重啓apache

$sudo mkdir /tmp/xcache
$sudo chmod 777 /tmp/xcache
$sudo /usr/local/apache/bin/apachectl restart

去查看phpinfo()信息吧!

3、PHP加速器測試

一、測試環境

硬件: AMD Athlon 64 X2 Dual Core Processor 4400+ @ 2.2GHz CPU, 2GB 內存. 160GB SATA 硬盤

軟件: Linux Ubuntu server Gutsy 7.10, Apache 2.2.4, MySQL 5.0.45 和 PHP 5.2.3

測試指令: ab -c5 -n3000 http://example.com/ (咱們使用的是Apache Benchmark (ab) 工具,併發鏈接爲5,3000次請求)

二、測試結果

無任何加速器:

Document Path: /
Document Length: 21757 bytes
Concurrency Level: 5
Time taken for tests: 288.255212 seconds
Complete requests: 3000
Failed requests: 0
Write errors: 0
Total transferred: 66777000 bytes
HTML transferred: 65271000 bytes
Requests per second: 10.41 [#/sec] (mean)
Time per request: 480.425 [ms] (mean)
Time per request: 96.085 [ms] (mean, across all concurrent requests)
Transfer rate: 226.23 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 19
Processing: 181 479 186.0 444 1822
Waiting: 166 461 184.7 427 1708
Total: 181 479 186.0 444 1822
Percentage of the requests served within a certain time (ms)
50% 444
66% 525
75% 577
80% 619
90% 732
95% 819
98% 946
99% 1012
100% 1822 (longest request)

APC加速器:

Document Path: /
Document Length: 21757 bytes
Concurrency Level: 5
Time taken for tests: 98.530068 seconds
Complete requests: 3000
Failed requests: 0
Write errors: 0
Total transferred: 66777000 bytes
HTML transferred: 65271000 bytes
Requests per second: 30.45 [#/sec] (mean)
Time per request: 164.217 [ms] (mean)
Time per request: 32.843 [ms] (mean, across all concurrent requests)
Transfer rate: 661.84 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 2
Processing: 58 163 71.2 155 2452
Waiting: 53 158 69.6 150 2329
Total: 58 163 71.2 155 2452
Percentage of the requests served within a certain time (ms)
50% 155
66% 178
75% 193
80% 204
90% 235
95% 258
98% 285
99% 302
100% 2452 (longest request)

eAccelerator加速器:

Document Path: /
Document Length: 21757 bytes
Concurrency Level: 5
Time taken for tests: 95.983986 seconds
Complete requests: 3000
Failed requests: 0
Write errors: 0
Total transferred: 66777000 bytes
HTML transferred: 65271000 bytes
Requests per second: 31.26 [#/sec] (mean)
Time per request: 159.973 [ms] (mean)
Time per request: 31.995 [ms] (mean, across all concurrent requests)
Transfer rate: 679.39 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 3
Processing: 57 159 91.3 148 3830
Waiting: 50 152 89.8 142 3704
Total: 57 159 91.3 148 3830
Percentage of the requests served within a certain time (ms)
50% 148
66% 174
75% 193
80% 205
90% 239
95% 263
98% 289
99% 309
100% 3830 (longest request)

XCache加速器:

Document Path: /
Document Length: 21757 bytes
Concurrency Level: 5
Time taken for tests: 99.76300 seconds
Complete requests: 3000
Failed requests: 0
Write errors: 0
Total transferred: 66777000 bytes
HTML transferred: 65271000 bytes
Requests per second: 30.28 [#/sec] (mean)
Time per request: 165.127 [ms] (mean)
Time per request: 33.025 [ms] (mean, across all concurrent requests)
Transfer rate: 658.19 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 2
Processing: 59 164 83.4 155 3367
Waiting: 52 156 66.4 148 1802
Total: 59 164 83.4 155 3367
Percentage of the requests served within a certain time (ms)
50% 155
66% 178
75% 196
80% 206
90% 237
95% 263
98% 287
99% 305
100% 3367 (longest request)

三、結果摘要

  請求時間(秒) 單次請求時間(毫秒) 最大內存佔用(MB) 最小內存佔用(MB)
None 10.41 96.08 24 24
APC 30.45 32.84 21 21
eAccelerator 31.26 31.99 23 18
XCache 30.28 33.02 29 19

4、PHP加速器比較結果總結

     一、經過測試得出eAccelerator在請求時間和內存佔用綜合方面是最好的。

     二、經過測試得出使用加速器比無加速器在請求時間快了3倍左右。

     三、經過各個官方觀察,XCache是更新最快的,這也說明最有發展的。

        以上是總結結果,你也許會問我到底用那個加速器好呢?我只能告訴你,首先,用必定比不用好,其次每一個加速器還有一些能夠調優的參數,因此要根據你的系統環 境而定,而後,我我的以爲你能夠詳細研究下eAccelerator和XCache,這兩款潛力仍是很大的,最後我從比較專業的測試網站搞了一張結果圖:

cache

原文連接地址:http://www.vpser.net/opt/apc-eaccelerator-xcache.html

相關文章
相關標籤/搜索