摘要:* 近在用 Go 重构一个 PHP 项目,代码写的有点乱,也没文档,没注释,总之就是功能基本靠猜, 然后我找了个小工具快速理清代码执行流程,帮助自己理解代码逻辑。首先打开 xdebug 的分析器 xdebug.profiler_enable = 1 xdebu ...
* 近在用 Go 重构一个 PHP 项目,代码写的有点乱,也没文档,没注释,总之就是功能基本靠猜, 然后我找了个小工具快速理清代码执行流程,帮助自己理解代码逻辑。
首先打开 xdebug 的分析器
- xdebug.profiler_enable = 1
- xdebug.profiler_output_dir = /tmp
分析器有什么用,有兴趣可以去 xdebug 官网 自己去看。
但是 xdebug 分析器输出的内容对人是不友好的,使用工具才能方便的看到自己想要的,这里我们使用 webgrind 帮助我们查看这个内容。
webgrind 依赖 python 和 graphviz,安装完成之后修改 webgrind 的 config.php 文件
- /**
- * Path to python executable
- */
- static $pythonExecutable = '/usr/bin/python';
- /**
- * Path to graphviz dot executable
- */
- static $dotExecutable = '/usr/local/bin/dot';
然后在 webgrind 目录下执行
- php -S 127.0.0.1:19000 //使用php内置http服务器,php version >= 5.4
webgrind 可以生成流程图和分析每个方法函数的执行时间,这里我用 CodeIgniter 做个例子,展示下效果
来源: http://www.phpchina.com/portal.php?mod=view&aid=40353