温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

XHProf性能分析工具怎么在PHP项目中使用

发布时间:2020-12-19 15:04:39 来源:亿速云 阅读:143 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关XHProf性能分析工具怎么在PHP项目中使用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

HProf是facebook开源出来的一个php轻量级的性能分析工具,跟Xdebug类似,但性能开销更低,还可以用在生产环境中,也可以由程序开关来控制是否进行profile。基于浏览

器的性能分析用户界面能更容易查看,或是与同行们分享成果。也能绘制调用关系图。在数据收集阶段,它记录调用次数的追踪和包容性的指标弧在动态callgraph的一个程序。

它独有的数据计算的报告/后处理阶段。在数据收集时,XHProfd通过检测循环来处理递归的函数调用,并通过给递归调用中每个深度的调用一个有用的命名来避开死循环。

XHProf的轻量级性质和汇聚功能,使得它非常适合用于收集“生产环境”的性能统计数据的统计。

1. 安装XHProf

复制代码 代码如下:


wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar zxf xhprof-0.9.2.tgz
cd xhprof-0.9.2
cp -r xhprof_html xhprof_lib <directory_for_htdocs>
cd extension
phpize
./configure
make
make install

2. 配置 php.ini 文件

复制代码 代码如下:


[xhprof]
extension=xhprof.so
;
; directory used by default implementation of the iXHProfRuns
; interface (namely, the XHProfRuns_Default class) for storing
; XHProf runs.
; 记得<directory_for_storing_xhprof_runs>WEB要有写入权限
xhprof.output_dir=<directory_for_storing_xhprof_runs>

重启服务让修改生效,现在就可以使用XHProf了,不过为了显示效果更炫,最好继续安装Graphviz。

3. 安装Graphviz

复制代码 代码如下:


wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.26.3.tar.gz  
tar zxf graphviz-2.26.3.tar.gz   
cd graphviz-2.26.3  
./configure
make   
make install


安装完成后,会生成/usr/local/bin/dot文件,你应该确保路径在PATH环境变量里,以便XHProf能找到它。

4. 应用XHProf

复制代码 代码如下:


xhprof_enable();//打开xhprof

/******程序逻辑 Start******/
function test1(){
 sleep(3);
 return;
}
function test2(){
 test1();
}
function test3(){
 test2();
}
function p(){
 echo '<h4>xhprof test</h4>';
}
p();
test3();
/******程序逻辑 End******/

$xhprof_data = xhprof_disable();//关闭xhprof

//保存xhprof数据
include_once '../xhprof_lib/utils/xhprof_lib.php';
include_once '../xhprof_lib/utils/xhprof_runs.php';

$xhprof_runs = new XHProfRuns_Default();
$xhprof_source = 'xhprof_test';
$run_id = $xhprof_runs->save_run($xhprof_data, $xhprof_source);
$report_url = 'http://xhprof.rebill.info/index.php?run='.$run_id.'&source='.$xhprof_source;
echo '<br>';
echo 'view the performance report:<a href="'.$report_url.'" target="_blank">'.$report_url.'</a>';

如此一来,会在上面设定的xhprof.output_dir目录里生成名字类似4c236583ef490.xhprof_test的数据文件,可以很方便的通过Web方式浏览效果:
http://xhprof.rebill.info/index.php?run=4c236583ef490&source=xhprof_test

目前显示的是表格形式的显示,点击页面上的[View Full Callgraph],就能看到精美的图片显示了。

关于XHProf性能分析工具怎么在PHP项目中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI