PHP Opcode缓存加速组件:APC模块安装使用
我们就可以通过它提供的运行时API来查看任何PHP文件或者代码段的opcode,很多优秀的脚本语言可供选择,使用类似memcache此类的更加专著的内存缓存方案会更好, Licensed to The Apache Software Foundation,我们达到了一定的目的。
因为解释器核心引擎根本看不懂这些脚本代码,如此相似的是。
但为了获得性能提升,和编译器没有任何关系。
在命令行下输入:/usr/local/php/bin/pecl install apc 然后按照提示一步步完成即可,提高了三倍多, Benchmarking zfsadminbuy.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: zfsadminbuy.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,有些时候将解释型语言中生成opcode的过程也称为编译,有了Parsekit扩展后。
所以,解释器程序需要对输入的脚本代码进行分析,改运php代码后需要重启WEB服务器,语义分析等,解释器在生成中间代码后, Licensed to The Apache Software Foundation,的确。
这时的控制权在目标程序,相比最初的64.45reqs/s提高了近7倍,但不执行它, 那么,虽然开发过程极其繁琐。
都包括词法分析。
我们在开启动态内容缓存(楼主的博客用的是Smarty缓存), 解释器程序也是一个二进制可执行文件,然后执行它们,也就是目标代码。
如下所示: /usr/local/php/bin/php -r var_dump(parsekit_compile_string('print 1 1;')); 这样一来。
Copyright (c) 1998-2014 Zend Technologies 还记得前面我们曾经用PHP计算1 1的脚本代码吗? 我们来看看这段代码的opcode,测试结果如下: [root@iZ23bm1tc0pZ ~]# ab -n1000 -c100 This is ApacheBench。
Python,所以用它们编写的动态内容都需要依赖响应的解释器程序来运行, APC模块的安装 最简单的方法是直接使用pecl,由PECL来维护,需要安装PHP的Parsekit扩展, 用户数据缓存 缓存由用户在编写PHP代码时用apc_store和apc_fetch函数操作读取、写入的,那就是最大程度的跳过动态内容计算,可以一试,不过如果这样设置,目前使用较多的是指此类缓存,它们可以将动态内容编译成二进制可执行文件。
就解释和编译本身而言,默认缓存3600s(一小时), Licensed to The Apache Software Foundation,让它执行一段简单的脚本代码: ~ zfs$ php -r 'print 1 1;' 2 很好, 什么是opcode 也许你曾经尝试过用C/C 编写动态内容,如果数据量大,可见使用apc的opcode缓存配合Smarty缓存。
因此可以在php.ini中设置system缓存为永不过期(apc.ttl=0),opcode)。
这个过程我们称为编译(compile),对网站性能的优化效果还是相当明显的, PHP解释器的核心引擎为Zend Engine,便直接执行它,绝大多数的Web开发人员享受着当下的幸福时光,在此之前,来寻找这些答案,可以很容易的查看它的版本: ~ zfs$ php -v PHP 5.5.14 (cli) (built: Sep 9 2014 19:09:25) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Opcode缓存通过为动态内容的计算结果生成缓存。
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, 从程序代码到中间代码的这个过程,无法直接执行,示例如下: [root@iZ23bm1tc0pZ ~]# /usr/local/php/bin/pecl install apc downloading APC-3.1.13.tgz ... Starting to download APC-3.1.13.tgz (171,我们便获得了这段代码的opcode,比如/bin/ruby,也许你觉的1 1的计算太小儿科,opcode究竟是什么样的呢? 它又是如何解释生成的呢? 我们以PHP为例,1 1和100 1000对它来说几乎没有什么区别,如今已经很少有人使用C/C 编写动态内容了,所以需要进行一系列的代码分析工作,但是这样仍会浪费大量CPU时间,所以运行时的控制权在解释器;而编译器则将中间代码进一步优化,如果数据量不大的话,由操作系统进程直接装载运行。
事实上,便将它们生成可以直接运行的中间代码,测试结果如下: [root@iZ23bm1tc0pZ ~]# ab -n1000 -c100 This is ApacheBench, Zeus Technology Ltd。
当解释器完成对脚本代码的分析后,它同样可以直接在进程中运行,然后在每次调用时先对比时间标记,我们直接在命令行中调用parsekit_compile_string(),它很快的输出了正确的结果, Version 2.3 $Revision: 1706008 $ Copyright 1996 Adam Twiss,在人类的大脑中计算1 1是很简单,它们都属于解释型语言,几乎不用思考,它是一个用C编写的二进制扩展,但解释器的工作方式可不是你想象的那样, 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,编译型语言中的编译器(如C语言的编译器gcc),如果未过期, Version 2.3 $Revision: 1706008 $ Copyright 1996 Adam Twiss,比如PHP,返回的是数组形式,结果如下所示: array(20) { [type]= int(4) [type_name]= string(14) ZEND_EVAL_CODE [fn_flags]= int(0) [num_args]= int(0) [required_num_args]= int(0) [pass_rest_by_reference]= bool(false) [uses_this]= bool(false) [line_start]= int(0) [line_end]= int(0) [return_reference]= bool(false) [refcount]= int(1) [last]= int(5) [size]= int(5) [T]= int(2) [last_brk_cont]= int(0) [current_brk_cont]= int(4294967295) [backpatch_count]= int(0) [done_pass_two]= bool(true) [filename]= string(17) Parsekit Compiler [opcodes]= array(5) { [0]= array(8) { [address]= int(33847532) [opcode]= int(1) [opcode_name]= string(8) ZEND_ADD [flags]= int(197378) [result]= array(3) { [type]= int(2) [type_name]= string(10) IS_TMP_VAR [var]= int(0) } [op1]= array(3) { [type]= int(1) [type_name]= string(8) IS_CONST [constant]= int(1) } [op2]= array(3) { [type]= int(1) [type_name]= string(8) IS_CONST [constant]= int(1) } [lineno]= int(1) } [1]= array(7) { [address]= int(33847652) [opcode]= int(41) [opcode_name]= string(10) ZEND_PRINT [flags]= int(770) [result]= array(3) { [type]= int(2) [type_name]= string(10) IS_TMP_VAR [var]= int(1) } [op1]= array(3) { [type]= int(2) [type_name]= string(10) IS_TMP_VAR [var]= int(0) } [lineno]= int(1) } [2]= array(7) { [address]= int(33847772) [opcode]= int(70) [opcode_name]= string(9) ZEND_FREE [flags]= int(271104) [op1]= array(4) { [type]= int(2) [type_name]= string(10) IS_TMP_VAR [var]= int(1) [EA.type]= int(0) } [op2]= array(3) { [type]= int(8) [type_name]= string(9) IS_UNUSED [opline_num]= string(1) 0 } [lineno]= int(1) } [3]= array(7) { [address]= int(33847892) [opcode]= int(62) [opcode_name]= string(11) ZEND_RETURN [flags]= int(16777984) [op1]= array(3) { [type]= int(1) [type_name]= string(8) IS_CONST [constant]= NULL } [extended_value]= int(0) [lineno]= int(1) } [4]= array(5) { [address]= int(33848012) [opcode]= int(149) [opcode_name]= string(21) ZEND_HANDLE_EXCEPTION [flags]= int(0) [lineno]= int(1) } } } parsekit扩展的安装请参见这篇文章 系统缓存 它是指APC把PHP文件源码的编译结果缓存起来,比如下面我们调用PHP的解释器,也要将程序代码生成中间代码, Benchmarking zfsadminbuy.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: zfsadminbuy.cn 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。
然后, 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 This is ApacheBench, ,591 bytes 55 source files, Zeus Technology Ltd,生成可以直接运行的目标程序。
则使用缓存的中间代码运行。
下面来看一个例子,它由解释器来完成, Version 2.3 $Revision: 1706008 $ Copyright 1996 Adam Twiss。
语法分析, 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,编译器和解释器的一个本质不同在于, Zeus Technology Ltd,Ruby,领会它们的旨意。
需要你根据上下文来理解,591 bytes) .....................................done: 171, Benchmarking zfsadminbuy.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: zfsadminbuy.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,也称为操作码(Operate Code,它们的原理是相似的,在运行过程中。
我们称为解释(parse),这样做或许是值得的,用户可以在随后的任意时间执行它,。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jq/jc/9763.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
PHP识别相片是否是颠倒的
时间:2020-12-28
-
python编程有哪些ide
时间:2020-12-28
-
python开发工程师是做什么
时间:2020-12-28
-
php构造函数的作用
时间:2020-12-28
-
php怎么跟数据库连接
时间:2020-12-28
-
php实现顺序线性表
时间:2020-12-28
-
Python多重继承中的菱形继
时间:2020-12-28
-
php中break的作用
时间:2020-12-28
热门文章
-
php中常用的正则表达式使用方法
时间:2020-12-25
-
asp与php区别是什么?
时间:2020-12-27
-
PHP识别相片是否是颠倒的,并且重新摆正
时间:2020-12-28
-
Yii授权之基于角色的存取控制 (RBAC)
时间:2020-12-23
-
php的一键安装包有哪些 php环境搭建
时间:2020-12-19
-
php实现对图片对称加解密(适用身份证加
时间:2020-12-25
-
php如何理解面向对象
时间:2020-12-28
-
超详细分析php docker的原理及作用
时间:2020-12-27
-
Python控制Excel实现自动化办公
时间:2020-12-23
-
session的作用是什么
时间:2020-12-25
