NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
# APD扩展(过时) # PHP 效能分析工具 APD是高级PHP调试器。它的目的是为PHP代码提供分析和调试功能,并提供打印完整堆栈回溯的能力。APD支持交互式调试,但在默认情况下,它将数据写入跟踪文件。它还提供了基于事件的日志记录,因此可以为各个脚本打开或关闭不同级别的信息(包括函数调用、传递的参数、计时等) > APD是一个Zend扩展,它修改了PHP内部处理函数调用的方式,因此可能与其他Zend扩展(例如Zend Optimizer)兼容,也可能不兼容 > **已经不再维护了 请使用Xdebug** ini配置: 名字默认可修改范围描述apd.dumpdirNULLPHP\_INI\_ALL设定 APD 写入调试输出文件的目录。可以指定绝对路径或相对路径,可以在[apd\_set\_pprof\_trace()](https://www.php.net/manual/zh/function.apd-set-pprof-trace.php)中以参数指定一个不同目录apd.statement\_tracing"0"PHP\_INI\_ALL指定是否进行每行的跟踪。将此项打开(设为 1)将影响到程序的性能开启: 1、首先去[package](http://pecl.php.net/package/apd) 下载对用的apd包解压或者git clone <https://github.com/ZeWaren/pecl-apd> ``` <pre class="calibre10">``` cd pecl<span class="token1">-</span>apd<span class="token1">/</span> phpize configure make <span class="token1">&&</span> make install <span class="token">//安装好以后显示 Installing shared extensions: /usr/lib/php5/20131226/</span> chmod <span class="token6">644</span> <span class="token1">/</span>usr<span class="token1">/</span>lib<span class="token1">/</span>php5<span class="token1">/</span><span class="token6">20131226</span><span class="token1">/</span>apd<span class="token3">.</span>so ``` ``` 2、配置php.ini ``` <pre class="calibre10">``` <span class="token3">[</span>apd<span class="token3">]</span> zend_extension<span class="token1">=</span><span class="token1">/</span>usr<span class="token1">/</span>lib<span class="token1">/</span>php5<span class="token1">/</span><span class="token6">20131226</span><span class="token1">/</span>apd<span class="token3">.</span>so # 将log写进<span class="token1">/</span>tmp<span class="token1">/</span>apd<span class="token1">/</span>trace文件夹里 apd<span class="token3">.</span>dumpdir <span class="token1">=</span> <span class="token1">/</span>tmp<span class="token1">/</span>apd<span class="token1">/</span>trace apd<span class="token3">.</span>statement_tracing <span class="token1">=</span> <span class="token6">0</span> ``` ``` > zend\_extension=...而不死extension=... > apd.dumpdir 是日志地址 > 3、 ``` <pre class="calibre10">``` mkdir <span class="token1">-</span>p <span class="token1">/</span>tmp<span class="token1">/</span>apd<span class="token1">/</span>trace chmod <span class="token1">-</span>R <span class="token6">777</span> <span class="token1">/</span>tmp<span class="token1">/</span>apd<span class="token1">/</span> php5enmod apd # 啟用 apd service apache2 reload ``` ``` # 预定义常量 ConstantValueDescription**`FUNCTION_TRACE`**(integer)1**`ARGS_TRACE`**(integer)2**`ASSIGNMENT_TRACE`**(integer)4**`STATEMENT_TRACE`**(integer)8**`MEMORY_TRACE`**(integer)16**`TIMING_TRACE`**(integer)32**`SUMMARY_TRACE`**(integer)64**`ERROR_TRACE`**(integer)128**`PROF_TRACE`**(integer)256**`APD_VERSION`**(string)example:*1.0.2-dev*函数: apd\_breakpoint — Stops the interpreter and waits on a CR from the socket apd\_callstack — Returns the current call stack as an array apd\_clunk — Throw a warning and a callstack apd\_continue — Restarts the interpreter apd\_croak — Throw an error, a callstack and then exit apd\_dump\_function\_table — Outputs the current function table apd\_dump\_persistent\_resources — Return all persistent resources as an array apd\_dump\_regular\_resources — Return all current regular resources as an array apd\_echo — Echo to the debugging socket apd\_get\_active\_symbols — Get an array of the current variables names in the local scope apd\_set\_pprof\_trace — Starts the session debugging apd\_set\_session\_trace\_socket — Starts the remote session debugging apd\_set\_session\_trace — Starts the session debugging apd\_set\_session — Changes or sets the current debugging level override\_function — Overrides built-in functions rename\_function — Renames orig\_name to new\_name in the global function table 用法: 1、在PHP脚本的第一行,调用apd\_set\_pprof\_trace()函数来启动跟踪,虽然可以在脚本中任何一行插入这一行,但是如果不是从脚本开头开始跟踪的话,则丢失了部分数据,有可能造成性能上的瓶颈 2、浏览器或者cli运行脚本。转储输出将被写入ap .dumpdir/pprof\_pid.ext,如果运行的是 CGI 版的 PHP,需要加入 '-e' 标记启用扩展信息以使 APD 正常工作。例如: **`php -e -f script.php`** 运行脚本后会产生pprof.xxxxx.0,然后再使用 pprofp xxx 來查看即可 分析日志的方法 手册里这样写到 ``` <pre class="calibre10">``` pprofp <span class="token1">-</span>R <span class="token1">/</span>tmp<span class="token1">/</span>pprof<span class="token3">.</span><span class="token6">22141.0</span> ``` ``` 注意这里的pprofp 在你编译好的 apd的源码目录 其实就是一个php文件 这里你要修改pprofp文件开头的 php路径 才可以正常执行 php 以cgi方式运行的时候 据说 记着 -e参数 ![](https://img.kancloud.cn/4f/75/4f7546bb3baed81db2dc0ab96671b34e_801x374.png)