助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
# PHP 效能分析工具 APD是高级PHP调试器。它的目的是为PHP代码提供分析和调试功能,并提供打印完整堆栈回溯的能力。APD支持交互式调试,但在默认情况下,它将数据写入跟踪文件。它还提供了基于事件的日志记录,因此可以为各个脚本打开或关闭不同级别的信息(包括函数调用、传递的参数、计时等) >[danger] APD是一个Zend扩展,它修改了PHP内部处理函数调用的方式,因此可能与其他Zend扩展(例如Zend Optimizer)兼容,也可能不兼容 > **已经不再维护了 请使用Xdebug** ini配置: | 名字 | 默认 | 可修改范围 | 描述| | --- | --- | --- | --- | | apd.dumpdir | NULL | PHP\_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 ``` cd pecl-apd/ phpize configure make && make install //安装好以后显示 Installing shared extensions:     /usr/lib/php5/20131226/ chmod 644 /usr/lib/php5/20131226/apd.so ``` 2、配置php.ini ``` [apd] zend_extension=/usr/lib/php5/20131226/apd.so # 将log写进/tmp/apd/trace文件夹里 apd.dumpdir = /tmp/apd/trace apd.statement_tracing = 0 ``` >[danger]zend_extension=...而不死extension=... apd.dumpdir 是日志地址 3、 ``` mkdir -p /tmp/apd/trace chmod -R 777 /tmp/apd/ php5enmod apd # 啟用 apd service apache2 reload ``` # 预定义常量[](https://www.php.net/manual/zh/apd.constants.php#apd.constants) | Constant | Value | Description | | --- | --- | --- | | **`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 來查看即可 分析日志的方法 手册里这样写到 ``` pprofp -R /tmp/pprof.22141.0 ``` 注意这里的pprofp 在你编译好的 apd的源码目录 其实就是一个php文件 这里你要修改pprofp文件开头的 php路径 才可以正常执行 php 以cgi方式运行的时候 据说 记着 -e参数 ![](https://img.kancloud.cn/4f/75/4f7546bb3baed81db2dc0ab96671b34e_801x374.png)