栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

使用 Xdebug + Webgrind 分析 PHP 程序

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用 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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/229221.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号