一直想找一些關(guān)于PHP加速的文章,偶然看到殺客的這篇文章,感覺(jué)不錯(cuò),分享給大家,再此感謝殺客。

一、PHP加速器介紹

        PHP加速器是一個(gè)為了提高PHP執(zhí)行效率,從而緩存起PHP的操作碼,這樣PHP后面執(zhí)行就不用解析轉(zhuǎn)換了,可以直接調(diào)用PHP操作碼,這樣速度上就提高了不少。

        Apache中使用mod_php的請(qǐng)求、響應(yīng)執(zhí)行流程:

  1、Apache接收請(qǐng)求。
2、Apache傳遞請(qǐng)求給mod_php。
3、mod_php定位磁盤(pán)文件,并加載到內(nèi)存中。
4、mod_php編譯源代碼成為opcode樹(shù)。
5、mod_php執(zhí)行opcode樹(shù)。

       PHP加速器相應(yīng)的就是第四步,它的目的就是防止PHP每次請(qǐng)求都重復(fù)編譯PHP代碼,因?yàn)樵诟咴L問(wèn)量的網(wǎng)站上,大量的編譯往往沒(méi)有執(zhí)行速度快呢?所以這里面有個(gè)瓶頸就是PHP的重復(fù)編譯既影響了速度又加載了服務(wù)器負(fù)載,為了解決此問(wèn)題,PHP加速器就這樣誕生了。

二、PHP加速器安裝與配置

        1、安裝配置APC

             APC全稱(chēng)是Alternative PHP Cache,官方翻譯叫”可選PHP緩存”,它是PHP PECL中的一個(gè)擴(kuò)展,好像是facebook在使用它,下面開(kāi)始安裝(ubuntu環(huán)境): 
$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

下面我們?cè)倥渲肁PC,因?yàn)槲业腜ECL擴(kuò)展路徑改變了,所以我得移動(dòng)下編譯好的文件:
$sudo mv /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/apc.so /usr/local/php/lib/php/extensions/PECL

然后我們?cè)倬庉媝hp.ini文件進(jìn)行配置,請(qǐng)把下面的代碼加入到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就會(huì)在phpinfo()信息中顯示。

       2、安裝配置eAccelerator

          eAccelerator的前身其實(shí)是truck-mmcache,因?yàn)殚_(kāi)發(fā)truk-mmcache的人被Zend給招安了,所以開(kāi)發(fā)eAccelerator的人繼承了truk-mmcache的一些特性,設(shè)計(jì)出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"

創(chuàng)建緩存目錄,重啟apache

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

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

3、安裝配置XCache

XCache作為國(guó)人自己開(kāi)發(fā)的東西,做小菜鳥(niǎo)的我也感到驕傲,而且XCache無(wú)論在速度還是性能上都做的不錯(cuò)。下面就趕緊讓我們品嘗它吧!

$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 = "(執(zhí)行) 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 = ""

創(chuàng)建緩存目錄,重啟apache

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

去查看phpinfo()信息吧!

三、PHP加速器測(cè)試

1、測(cè)試環(huán)境

硬件: AMD Athlon 64 X2 Dual Core Processor 4400+ @ 2.2GHz CPU, 2GB 內(nèi)存. 160GB SATA 硬盤(pán)

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

測(cè)試指令: ab -c5 -n3000 http://example.com/ (我們使用的是Apache Benchmark (ab) 工具,并發(fā)連接為5,3000次請(qǐng)求)

2、測(cè)試結(jié)果

無(wú)任何加速器:

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)

3、結(jié)果摘要

  請(qǐng)求時(shí)間(秒) 單次請(qǐng)求時(shí)間(毫秒) 最大內(nèi)存占用(MB) 最小內(nèi)存占用(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

四、PHP加速器比較結(jié)果總結(jié)

     1、通過(guò)測(cè)試得出eAccelerator在請(qǐng)求時(shí)間和內(nèi)存占用綜合方面是最好的。

     2、通過(guò)測(cè)試得出使用加速器比無(wú)加速器在請(qǐng)求時(shí)間快了3倍左右。

     3、通過(guò)各個(gè)官方觀察,XCache是更新最快的,這也說(shuō)明最有發(fā)展的。

        以上是總結(jié)結(jié)果,你也許會(huì)問(wèn)我到底用那個(gè)加速器好呢?我只能告訴你,首先,用一定比不用好,其次每個(gè)加速器還有一些可以調(diào)優(yōu)的參數(shù),所以要根據(jù)你的系統(tǒng)環(huán)境而定,然后,我個(gè)人覺(jué)得你可以詳細(xì)研究下eAccelerator和XCache,這兩款潛力還是很大的,最后我從比較專(zhuān)業(yè)的測(cè)試網(wǎng)站搞了一張結(jié)果圖:

cache

本文轉(zhuǎn)載自:http://killker.com/blog/?p=94