xdebug php 使用,使用 Xdebug + Webgrind 分析 PHP 程序

安装 xdeubg zend 扩展# yum install php-pecl-xdebug

配置 php.d/xdebug.ini# 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。

下载 webgrind$ cd /var/www/html/$ git clone git://github.com/jokkedk/webgrind.git

安装 graphviz# yum install graphviz

graphviz 附带 dot 命令用于下文的配置。

使用 webgrind

webgrind v1.1 的版本增加了 Show Call Graph 按钮,依赖 python 和 dot 命令, 类似 xhprof 的View Full Callgraph 绘制程序调用流程图功能,相对于 xhprof 较简洁。 更多 xhprof 信息请查看 使用 XHProf 分析你的 PHP 程序。

配置 webgrind 的绘制流程图功能,查看 python 和 dot 命令位置 which pythonwhich 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 生成流程图

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值