详细比较 PHP 三加速扩展 APC、xcache、eAccelerator 的性能
详细比较 PHP 三加速扩展 APC、xcache、eAccelerator 的性能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 加速器工作原理对应的就是第 4 步,目的是为避免 PHP 每次请求都重复编译 PHP 代码。因为对高访问量网站进行大量编译,往往还没有执行速度快?所以,这里面有个瓶颈就是 PHP 的重复编译,既影响速度又加重服务器负载。为解决此问题,PHP 加速器就这样应运而生。
1、 安装配置 APCAPC 全称 Alternative PHP Cache,官方译为 ”可选 PHP 缓存”。它是 PHP PECL 中的一个扩展,下面开始安装 (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
make install配置 APC,因为 PECL 扩展路径有变化;所以,要移动编译好的文件: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() 信息中显示。
2、 安装配置 eAcceleratoreAccelerator 的前身其实是 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
make install
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"创建缓存目录,重启 Apachemkdir /tmp/eaccelerator
chmod 777 /tmp/eaccelerator
/usr/local/apache/apachectl restart用 phpinfo() 检查是否安装成功。
3、 安装配置 XCacheXCache 是国人自己开发的东西,且 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
make install
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 = ""创建缓存目录,重启 Apachemkdir /tmp/xcache
chmod 777 /tmp/xcache
/usr/local/apache/bin/apachectl restart用 phpinfo() 检查是否安装成功。
4、 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 (mean)
Time per request: 96.085 (mean, across all concurrent requests)
Transfer rate: 226.23 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 (mean)
Time per request: 32.843 (mean, across all concurrent requests)
Transfer rate: 661.84 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 (mean)
Time per request: 31.995 (mean, across all concurrent requests)
Transfer rate: 679.39 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 (mean)
Time per request: 33.025 (mean, across all concurrent requests)
Transfer rate: 658.19 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)结果如下图所示
测试比较总结:
1、通过测试,得出 eAccelerator 在请求时间和内存占用综合方面是最好的。
2、通过测试得出,使用加速器比无加速器在请求时间方面快了 3 倍左右。
3、通过观察,XCache 是更新最快的,这也说明它是最有发展的。
注意:
1、程序环境非必要 Zend Optimizer 的情况下,首选 APC (它和 Zend Optimizer 是不兼容的);且 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 相近,所以差别不大。不通过修改参数详细测试,是不能看出两者优劣的。所以二者取其一即可。
版权声明:
本文为独家原创稿件,版权归 德云社区,未经许可不得转载;否则,将追究其法律责任。
LNMP, LAMP, Apache, PHP, APC
LNMP, LAMP, Apache, PHP, APC
页:
[1]