1. 安装 XHProf 扩展
pecl install xhprof
2. 配置 xhprof.ini
在 /usr/local/etc/php/7.2/conf.d 目录下新建一个 ext-xhprof.ini 配置文件,然后添加下列配置
zend_extension="/usr/local/Cellar/php@7.2/7.2.26/pecl/20170718/xhprof.so"
3. 克隆项目
git clone https://github.com/phacility/xhprof
4. 运行示例代码
http://localhost/xhprof/examples/sample.php
查看运行效果
http://localhost/xhprof/xhprof_html
在 PHP7.2 版本中提示 create_function()函数被废弃
Deprecated: Function create_function() is deprecated in /Users/s/workspaces/phproot/default/xhprof/xhprof_lib/utils/xhprof_runs.php on line 152
修改xhprof/xhprof_lib/utils/xhprof_runs.php 第 152 行代码
usort($files, create_function('$a,$b', 'return filemtime($b) - filemtime($a);'));
#修改为
usort($files, function ($a, $b) {return filemtime($a)-filemtime($b)>0?0:1;});
5.执行结果显示
查看性能分析数据
http://localhost/xhprof/xhprof_html/?run=5e5620f6abbed&source=xhprof_foo
查看性能分析图像
安装 gprof2dot 系统应用
brew install gprof2dot
http://localhost/xhprof/xhprof_html/callgraph.php?run=5e5620f6abbed&source=xhprof_foo