PHP 7.1安装xhprof进行性能分析
xhprof failed to execute cmd: " dot -Tpng". stderr: `sh: dot: command not found '
1
2
3
4
5
6
7
|
wget http: //www .graphviz.org /pub/graphviz/ARCHIVE/graphviz-2 .28.0. tar .gz tar xzvf graphviz-2.28.0. tar .gz . /configure --prefix= /home/work/local/graphviz make && make install |
1
2
3
4
5
6
7
|
# 如果出现dot: command not found # 改一下 : xhprof_lib/utils/callgraph_utils.php 中的 # xhprof_generate_image_by_dot函数中的 $cmd #改之前 $cmd = " dot -T" . $type ; #改之后 $cmd = " export LD_LIBRARY_PATH=/home/work/local/graphviz/lib && /home/work/local/graphviz/bin/dot -T" . $type ; |
安装扩展
该 xhprof扩展版本是从 https://github.com/longxinH/xhprof 获取的(第三方的一个库,官方版本不支持php7)
下载并编译xhprof扩展
在web的html目录下操作:
git clone https://github.com/longxinH/xhprof
编译扩展
cd xhprof/extension/
phpize
./configure
#如果不行就运行 ./configure --with-php-config=/usr/local/php7/bin/php-config
make make install
修改php.ini配置
[xhprof] extension=xhprof.so; xhprof.output_dir=/tmp/xhprof
其中 xhprof.output_dir 是 xhprof 的输出目录,每次执行 xhprof 的 save_run 方法时都会生成一个 run_id.project_name.xhprof 文件。这个目录在哪里并不重要。注意此路径的权限要可读写!!否则文件无法生成成功
重启 php-fpm
sudo service php7.1-fpm restart
添加测试代码
<?php xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); // 要检查性能的代码 $xhprof_data = xhprof_disable(); include_once '/var/www/html/xhprof/xhprof_lib/utils/xhprof_lib.php'; include_once '/var/www/html/xhprof/xhprof_lib/utils/xhprof_runs.php'; $xhprof_runs = new XHProfRuns_Default(); $run_id = $xhprof_runs->save_run($xhprof_data, 'your_project');
测试代码中要引入xhprof_lib.php和xhprof_runs.php两个文件
查看生成报告
需要访问:xhprof/xhprof_html/index.php文件查看:
http://localhost/xhprof/xhprof_html/index.php?run=5b35d3dfa8c29&source=your_project
run后的参数为$run_id,source参数为your_project配置的名字
如果图表生成错误,需要安装插件:
sudo apt-get install graphviz
实际演示代码
<?php function test1(){ for($i=0;$i<10;$i++){ echo 'aaa'.$i.'<br>'; } } // start profiling xhprof_enable(); test1(); // stop profiler $xhprof_data = xhprof_disable(); // display raw xhprof data for the profiler run print_r($xhprof_data); include_once "xhprof_lib.php"; include_once "xhprof_runs.php"; // save raw data for this profiler run using default // implementation of iXHProfRuns. $xhprof_runs = new XHProfRuns_Default(); // save the run under a namespace "xhprof_test" $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_test"); echo "--------------- ". "Assuming you have set up the http based UI for ". "XHProf at some address, you can view run at ". "http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_test ". "--------------- ";