PHP Opcode缓存加速组件:APC详解介绍
发布:smiling 来源: PHP粉丝网 添加日期:2018-09-27 13:12:39 浏览: 评论:0
PHP在性能上相对于其他编译型语言来说算不上突出,但是使用了Opcode缓存后性能提升还是很明显的。常见的缓存加速组件主要有 eAccelerator,XCache,APC本文主要介绍APC的安装使用。
APC,全称是Alternative PHP Cache,官方翻译叫"可选PHP缓存"。它为我们提供了缓存和优化PHP的中间代码的框架。 APC的缓存分两部分:系统缓存和用户数据缓存。
系统缓存
它是指APC把PHP文件源码的编译结果缓存起来,然后在每次调用时先对比时间标记。如果未过期,则使用缓存的中间代码运行。默认缓存3600s(一小时)。但是这样仍会浪费大量CPU时间。因此可以在php.ini中设置system缓存为永不过期(apc.ttl=0)。不过如果这样设置,改运php代码后需要重启WEB服务器。目前使用较多的是指此类缓存。
用户数据缓存
缓存由用户在编写PHP代码时用apc_store和apc_fetch函数操作读取、写入的。如果数据量不大的话,可以一试。如果数据量大,使用类似memcache此类的更加专著的内存缓存方案会更好。
APC模块的安装
最简单的方法是直接使用pecl,在命令行下输入:/usr/local/php/bin/pecl install apc
然后按照提示一步步完成即可,示例如下:
- [root@iZ23bm1tc0pZ ~]# /usr/local/php/bin/pecl install apc
- downloading APC-3.1.13.tgz ...
- Starting to download APC-3.1.13.tgz (171,591 bytes)
- .....................................done: 171,591 bytes
- 55 source files, building
- running: phpize
- Configuring for:
- PHP Api Version: 20100412
- Zend Module Api No: 20100525
- Zend Extension Api No: 220100525
- Enable internal debugging in APC [no] : no
- Enable per request file info about files used from the APC cache [no] : no
- Enable spin locks (EXPERIMENTAL) [no] : no
- Enable memory protection (EXPERIMENTAL) [no] : no
- Enable pthread mutexes (default) [no] : no
- Enable pthread read/write locks (EXPERIMENTAL) [yes] : yes
然后重启服务器即可:
lnmp nginx restart
先看一下没有使用apc情况下的压测结果:
- [root@iZ23bm1tc0pZ ~]# ab -n1000 -c100 http://phpfensi.com/index.php
- This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
- Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.phpfensi.com/
- Licensed to The Apache Software Foundation, http://www.apache.org/
- Benchmarking zfsphp.cn (be patient)
- Completed 100 requests
- Completed 200 requests
- Completed 300 requests
- Completed 400 requests
- Completed 500 requests
- Completed 600 requests
- Completed 700 requests
- Completed 800 requests
- Completed 900 requests
- Completed 1000 requests
- Finished 1000 requests
- Server Software: nginx
- Server Hostname: zfsphp.cn
- Server Port: 80
- Document Path: /index.php
- Document Length: 14341 bytes
- Concurrency Level: 100
- Time taken for tests: 15.517 seconds
- Complete requests: 1000
- Failed requests: 0
- Total transferred: 14544000 bytes
- HTML transferred: 14341000 bytes
- Requests per second: 64.45 [#/sec] (mean)
- Time per request: 1551.671 [ms] (mean)
- Time per request: 15.517 [ms] (mean, across all concurrent requests)
- Transfer rate: 915.34 [Kbytes/sec] received
- Connection Times (ms)
- min mean[+/-sd] median max
- Connect: 0 2 4.8 0 17
- Processing: 46 1481 277.0 1560 1638
- Waiting: 42 1481 277.1 1560 1638
- Total: 58 1482 272.8 1560 1638
- Percentage of the requests served within a certain time (ms)
- 50% 1560
- 66% 1576
- 75% 1582
- 80% 1587
- 90% 1602
- 95% 1612
- 98% 1622
- 99% 1629
- 100% 1638 (longest request)
可见最大吞吐率只有64.45reqs/s
然后我们开启apc,测试结果如下:
- [root@iZ23bm1tc0pZ ~]# ab -n1000 -c100 http://phpfensi.com/index.php
- This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
- Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.phpfensi.com/
- Licensed to The Apache Software Foundation, http://www.apache.org/
- Benchmarking zfsphp.cn (be patient)
- Completed 100 requests
- Completed 200 requests
- Completed 300 requests
- Completed 400 requests
- Completed 500 requests
- Completed 600 requests
- Completed 700 requests
- Completed 800 requests
- Completed 900 requests
- Completed 1000 requests
- Finished 1000 requests
- Server Software: nginx
- Server Hostname: zfsphp.cn
- Server Port: 80
- Document Path: /index.php
- Document Length: 14341 bytes
- Concurrency Level: 100
- Time taken for tests: 7.122 seconds
- Complete requests: 1000
- Failed requests: 0
- Total transferred: 14544000 bytes
- HTML transferred: 14341000 bytes
- Requests per second: 140.41 [#/sec] (mean)
- Time per request: 712.189 [ms] (mean)
- Time per request: 7.122 [ms] (mean, across all concurrent requests)
- Transfer rate: 1994.29 [Kbytes/sec] received
- Connection Times (ms)
- min mean[+/-sd] median max
- Connect: 0 1 2.4 0 10
- Processing: 23 677 125.3 705 775
- Waiting: 22 677 125.4 705 775
- Total: 30 678 123.1 705 775
- Percentage of the requests served within a certain time (ms)
- 50% 705
- 66% 719
- 75% 726
- 80% 730
- 90% 742
- 95% 750
- 98% 760
- 99% 765
- 100% 775 (longest request)
可见吞吐率提高了一倍多,达到140.41reqs/s。
然后,我们在开启动态内容缓存(楼主的博客用的是Smarty缓存),测试结果如下:
- [root@iZ23bm1tc0pZ ~]# ab -n1000 -c100 http://phpfensi.com/index.php
- This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
- Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.phpfensi.com/
- Licensed to The Apache Software Foundation, http://www.apache.org/
- Benchmarking zfsphp.cn (be patient)
- Completed 100 requests
- Completed 200 requests
- Completed 300 requests
- Completed 400 requests
- Completed 500 requests
- Completed 600 requests
- Completed 700 requests
- Completed 800 requests
- Completed 900 requests
- Completed 1000 requests
- Finished 1000 requests
- Server Software: nginx
- Server Hostname: phpfensi.com
- Server Port: 80
- Document Path: /index.php
- Document Length: 14341 bytes
- Concurrency Level: 100
- Time taken for tests: 2.263 seconds
- Complete requests: 1000
- Failed requests: 0
- Total transferred: 14544000 bytes
- HTML transferred: 14341000 bytes
- Requests per second: 441.98 [#/sec] (mean)
- Time per request: 226.255 [ms] (mean)
- Time per request: 2.263 [ms] (mean, across all concurrent requests)
- Transfer rate: 6277.49 [Kbytes/sec] received
- Connection Times (ms)
- min mean[+/-sd] median max
- Connect: 0 1 3.1 0 12
- Processing: 18 215 38.1 222 255
- Waiting: 18 215 38.3 222 255
- Total: 26 216 35.6 223 255
- Percentage of the requests served within a certain time (ms)
- 50% 223
- 66% 230
- 75% 232
- 80% 234
- 90% 237
- 95% 239
- 98% 240
- 99% 243
- 100% 255 (longest request)
这一次吞吐率居然达到441.98reqs/s,提高了三倍多,相比最初的64.45reqs/s提高了近7倍,可见使用apc的opcode缓存配合Smarty缓存,对网站性能的优化效果还是相当明显的。
Tags: 缓存 组件
相关文章
- ·php 缓存设计(2013-11-29)
- ·PHP 浏览器缓存的各种操作方法(2014-01-15)
- ·php开发指南:缓存详解(2014-03-21)
- ·PHP清除Memcache过期缓存程序代码(2014-07-26)
- ·PHP缓冲ob_start和页面文件缓存(2014-07-29)
- ·php ob_start() ob_end_flush()缓存技术简单应用(2014-09-02)
- ·php中内存缓存memcached实例(2014-09-11)
- ·php ob_flush flush 输出缓存实例与说明(2014-09-19)
- ·php禁止浏览器使用缓存页面(2014-09-21)
- ·php设置静态内容缓存时间(2014-09-22)
- ·清除网页缓存(html asp php java)(2015-04-09)
- ·禁止ajax缓存的方法(ASP/PHP/JSP/html/js)(2015-04-09)
- ·PHP嵌套输出缓存的例子(2015-04-13)
- ·CodeIgniter框架中启用和清除缓存的教程(2017-03-23)
- ·PHP清除缓存的几种方法总结(2018-09-09)
- ·什么是yii2的缓存依赖?如何使用缓存依赖?(2020-01-10)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)