一、xhprof安装与配置
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>
<directory_for_storing_xhprof_runs>配置到/logs/xhprof
重启服务让修改生效,现在就可以使用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
01.xhprof_enable();//
打开
xhprof
02.
03./******
程序逻辑
Start******/
04.function
test1(){
05. sleep(3);
06. return;
07.}
08.function
test2(){
09. test1();
10.}
11.function
test3(){
12. test2();
13.}
14.function
p(){
15. echo
'<H3>xhproftest</H3>';
16.}
17.p();
18.test3();
19./******
程序逻辑
End******/
20.
21.$xhprof_data
=xhprof_disable();//
关闭
xhprof
22.
23.//
保存
xhprof
数据
24.include_once
'../xhprof_lib/utils/xhprof_lib.php';
25.include_once
'../xhprof_lib/utils/xhprof_runs.php';
26.
27.$xhprof_runs
= new
XHProfRuns_Default();
28.
$xhprof_source =
'xhprof_test';
29.$run_id
=$xhprof_runs->save_run($xhprof_data, $xhprof_source);
30.
$report_url =
'http://hanyf.okooo.com/xhprof_html/index.php?run='.
$run_id.
'&source='.
$xhprof_source;
31.echo
'<BR>';
32.
echo 'view the performance report:<A href="'.
$report_url.
'" target=_blank>'.
$report_url.
'</A>';
如此一来,会在上面设定的xhprof.output_dir目录里生成名字类似4c236583ef490.xhprof_test的数据文件,可以很方便的通过Web方式浏览效果:
http://hanyf.okooo.com/xhprof_html/index.php?run=4c236583ef490&source=xhprof_test
解析:
1. Calls:调用次数
2. Calls%:此函数调用次数占总调用次数百分比
3. Incl.Wall Time:包含子函数所有执行时间
4. Iwall%:包含子函数所有执行时间所占百分比
5. Excl.Wall Time: 函数执行本身花费的时间
6. Ewall%:函数执行本身花费的时间占总时间的百分比
目前显示的是表格形式的显示,点击页面上的[View FullCallgraph],就能看到精美的图片显示了。
最后,一些术语解释:
1. Inclusive Time (或子树时间):包括子函数所有执行时间。
2. Exclusive Time/Self Time:函数执行本身花费的时间,不包括子树执行时间。
3. Wall时间:花去了的时间或挂钟时间。
4. CPU时间:用户耗的时间+内核耗的时间
Xhprof文档(中文):http://home.cnblogs.com/group/topic/39870.html
Xhprof文档(英文):http://mirror.facebook.net/facebook/xhprof/doc.html