·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> php网站开发 >> 关于PHP加速eAccelerator、Xcache、APC和Zend Optimizer

关于PHP加速eAccelerator、Xcache、APC和Zend Optimizer

作者:佚名      php网站开发编辑:admin      更新时间:2022-07-23
关于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关注加速器了。既然遇到这样的问题,也从网上总结了一下东西,网友们看过就行了,不必去试验了。

安装哪一个呢?对性能会有什么影响呢?有时间要测试一下。

从别处看来的。

程序环境非必要Zend Optimizer的情况下,首选pecl-APC(它和Zend Optimizer不兼容)。总结:1.PHP缓冲(加速)首选pecl-APC,兼容性和性能表现都非常优秀。2.如果你的PHP环境需要Zend Optimizer,就安装eAccelerator,并把Zend Optimizer的压缩级别调到0。

zend optimizer 是一个代码优化的模块,可以调优php代码,实现的原理是对那些在被最终执行之前由运行编译器(Run-Time Compiler)产生的代码进行优化。代码性能可以提高40%到100%,从这点上来说,应该不具有强大的缓存功能,没有读过源码,不清楚是否具有缓存 以及缓存质量如何。eAccelerator 是一个将编译之后的php代码缓存在share memory中的模块。通过访问共享内存可以得到编译后的代码并直接执行用以提高效率,这个对于php的执行效率的提高还是很大的。同 时,eAccelerator也可以缓存数据至文件中,这个部分由于是对文件的操作,我想对大多数的文件cache来说,原理相似,性能相近。APC 原理上来讲和eAccelerator相近,所以差别不大。不通过修改参数详细测试,不能看出两者的优劣。所以取其一即可。

三款免费的PHP加速器:APC、eAccelerator、XCache比较

一、PHP加速器介绍

PHP加速器是一个为了提高PHP执行效率,从而缓存起PHP的操作码,这样PHP后面执行就不用解析转换了,可以直接调用PHP操作码,这样速度上就提高了不少。

Apache中使用mod_php的请求、响应执行流程:

  1、Apache接收请求。2、Apache传递请求给mod_php。3、mod_php定位磁盘文件,并加载到内存中。4、mod_php编译源代码成为opcode树。5、mod_php执行opcode树。

PHP加速器相应的就是第四步,它的目的就是防止PHP每次请求都重复编译PHP代码,因为在高访问量的网站上,大量的编译往往没有执行速度快呢?所以这 里面有个瓶颈就是PHP的重复编译既影响了速度又加载了服务器负载,为了解决此问题,PHP加速器就这样诞生了。

二、PHP加速器安装与配置

1、安装配置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; APCapc.enabled = 1apc.shm_segments = 1apc.shm_size = 64apc.optimization = 1apc.num_files_hint = 0apc.ttl = 0apc.gc_ttl = 3600apc.cache_by_default = on

这样重启apache就会在phpinfo()信息中显示。

2、安装配置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; eAcceleratoreaccelerator.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()检查是否安装成功.

3、安装配置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; xcachexcache.admin.user = "admin"xcache.admin.pass = "(执行) echo ’(你的密码)’|md5sum(得出的密文)";xcache.size = 24Mxcache.shm_scheme = "mmap"xcache.count = 2xcache.slots = 8kxcache.ttl = 0xcache.gc_interval = 0

xcache.var_size = 8Mxcache.var_count = 1xcache.var_slots = 8kxcache.var_ttl = 0xcache.var_maxttl = 0xcache.var_gc_interval = 300xcache.test = Offxcache.readonly_protection = Onxcache.mmap_path = "/tmp/xcache"xcache.coredump_directory = ""xcache.cacher = Onxcache.stat = Onxcache.optimizer = Off;xcache.coverager = Onxcache.coveragedump_directory = ""

创建缓存目录,重启apache

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

去查看phpinfo()信息吧!

三、PHP加速器测试

1、测试环境

硬件: 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次请求)

2、测试结果

无任何加速器:

Document Path: /Document Length: 21757 bytesConcurrency Level: 5Time taken for tests: 288.255212 secondsComplete requests: 3000Failed requests: 0Write errors: 0Total transferred: 66777000 bytesHTML transferred: 65271000 bytesRequests 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] receivedConnection Times (ms)min mean[+/-sd] median maxConnect: 0 0 0.5 0 19Processing: 181 479 186.0 444 1822Waiting: 166 461 184.7 427 1708Total: 181 479 186.0 444 1822Percentage of the requests served within a certain time (ms)50% 44466% 52575% 57780% 61990% 73295% 81998% 94699% 1012100% 1822 (longest request)

APC加速器:

Document Path: /Document Length: 21757 bytesConcurrency Level: 5Time taken for tests: 98.530068 secondsComplete requests: 3000Failed requests: 0Write errors: 0Total transferred: 66777000 bytesHTML transferred: 65271000 bytesRequests 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] receivedConnection Times (ms)min mean[+/-sd] median maxConnect: 0 0 0.0 0 2Processing: 58 163 71.2 155 2452Waiting: 53 158 69.6 150 2329Total: 58 163 71.2 155 2452Percentage of the requests served within a certain time (ms)50% 15566% 17875% 19380% 20490% 23595% 25898% 28599% 302100% 2452 (longest request)

eAccelerator加速器:

Document Path: /Document Length: 21757 bytesConcurrency Level: 5Time taken for tests: 95.983986 secondsComplete requests: 3000Failed requests: 0Write errors: 0Total transferred: 66777000 bytesHTML transferred: 65271000 bytesRequests 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] receivedConnection Times (ms)min mean[+/-sd] median maxConnect: 0 0 0.1 0 3Processing: 57 159 91.3 148 3830Waiting: 50 152 89.8 142 3704Total: 57 159 91.3 148 3830Percentage of the requests served within a certain time (ms)50% 14866% 17475% 19380% 20590% 23995% 26398% 28999% 309100% 3830 (longest request)

XCache加速器:

Document Path: /Document Length: 21757 bytesConcurrency Level: 5Time taken for tests: 99.76300 secondsComplete requests: 3000Failed requests: 0Write errors: 0Total transferred: 66777000 bytesHTML transferred: 65271000 bytesRequests 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] receivedConnection Times (ms)min mean[+/-sd] median maxConnect: 0 0 0.0 0 2Processing: 59 164 83.4 155 3367Waiting: 52 156 66.4 148 1802Total: 59 164 83.4 155 3367Percentage of the requests served within a certain time (ms)50% 15566% 17875% 19680% 20690% 23795% 26398% 28799% 305100% 3367 (longest request)

3、结果摘要

请求时间(秒)单次请求时间(毫秒