# yum install php-pecl-xdebug
# vi /etc/php.d/xdebug.ini;加入以下内容; profilerxdebug.profiler_enable=1xdebug.profiler_enable_trigger=1xdebug.profiler_output_dir=/tmp/xdebugxdebug.profiler_output_name=cachegrind.out.%p; tracexdebug.auto_trace=1xdebug.show_exception_trace=1xdebug.trace_output_dir=/tmp/xdebugxdebug.trace_output_name=trace.%c
注:需手工创建 /tmp/xdebug
目录且所属用户应为 php-fpm
配置文件中的 user
用户。 配置完毕,重启 service php-fpm restart
。
$ cd /var/www/html/$ git clone git://github.com/jokkedk/webgrind.git
# yum install graphviz
graphviz
附带 dot
命令用于下文的配置。
webgrind v1.1 的版本增加了 Show Call Graph
按钮,依赖 python
和 dot
命令, 类似 xhprof 的View Full Callgraph
绘制程序调用流程图功能,相对于 xhprof 较简洁。 更多 xhprof 信息请查看 使用 XHProf 分析你的 PHP 程序。
配置 webgrind 的绘制流程图功能,查看 python
和 dot
命令位置 which python
which dot
,编辑 webgrind/config.php
,将 $pythonExecutable = '/usr/bin/python'
和 $dotExecutable = '/usr/bin/dot'
替换为刚查到相应路径。
在浏览器访问任意一个项目路径,/tmp/xdebug
目录下会生成一个 cachegrind.out.xxxxx
文件作为 webgrind 的数据来源,打开 webgrind 页面,如:http://localhost/webgrind/
, 点击右上角update
按钮即会分析最后一次访问的项目地址的 PHP 程序,可以通过选择第二个 下拉框中的地址来分析相应的程序,点击 Show Call Graph
按钮就会显示当前分析的程序的 简明调用流程图。
表格中后三列的含义:
InvocationCount:函数调用次数TotalSelfCost:函数本身花费的时间TotalInclusiveCost:包含内部函数花费的时间
webgrind
生成流程图使用源码下的 library/gprof2dot.pypython
脚本, 先将cachegrind.out.xxxxx
转换成 DOT,再使用 dot
命令转换成图片输出。
PHP 使用 shell_exec 函数执行:
/usr/bin/python library/gprof2dot.py -n 10-f callgrind /tmp/xdebug/cachegrind.out.xxxxx |/usr/bin/dot -Tpng-o /tmp/xdebug/cachegrind.out.xxxxx-10.webgrind.png
转载请注明出处。
本文地址:http://blog.aboutc.net/profiling/18/php-profiler-xdebug-webgrind