调试PHP时,XDebug一直是大众的不二选择,搭配上Webgrind,可以获得不错的效果。今天看某人的栖息地里的介绍,才发现了XHProf,于是体验了一下,感觉很酷,与XDebug相比,运行更轻便(本身还包括一个web查看工具),表现更易懂,下面记录一下体验过程。
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.
- ;
- xhprof.output_dir=<directory_for_storing_xhprof_runs>
重启服务让修改生效,现在就可以使用XHProf了,不过为了显示效果更炫,最好继续安装Graphviz。
3. 安装Graphviz
- wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz
- tar zxf graphviz-2.24.0.tar.gz
- cd graphviz-2.24.0
- ./configure
- make
- make install
安装Graphviz的目的是为了xhprof图形化web工具查看profiling log文件。安装后使用web工具查看log图形时,最常见错误是:
提示如下:
引用
Error: either we can not find profile data for run_id 4d7f0bd99a12f or the threshold 0.01 is too small or you do not have ‘dot’ image generation utility installed.
且编译graphviz提示信息png: No (missing png.h),也就是dot 不支持PNG,所以需要安装libpng包,如:
- wget http://nchc.dl.sourceforge.net/project/libpng/libpng15/1.5.1/libpng-1.5.1.tar.gz
- tar zxf libpng-1.5.1.tar.gz
- cd libpng-1.5.1
- ./configure
- make
- make install
然后重新编译安装graphviz,加上参数--with-png=yes。完成后,应确保命令dot在PATH环境变量里(默认应该就在路径里,一般不需要特别设置),以便XHProf能找到它。
使用XHProf
在你要监测的Php代码头尾部分别加入代码xhprof_enable()和xhprof_disable()
- // start profiling
- xhprof_enable();
- // xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); // 允许CPU和内存输出
- // run program
- ....
- // stop profiler
- $xhprof_data = xhprof_disable();
- //
- // Saving the XHProf run
- // using the default implementation of iXHProfRuns.
- //
- $XHPROF_ROOT = "/projects/xhprof";//这里填写的就是你的xhprof的路径
- include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
- include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
- $xhprof_runs = new XHProfRuns_Default();
- // Save the run under a namespace "xhprof_foo".
- //
- // **NOTE**:
- // By default save_run() will automatically generate a unique
- // run id for you. [You can override that behavior by passing
- // a run id (optional arg) to the save_run() method instead.]
- //
- $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");
- echo "---------------\n".
- "Assuming you have set up the http based UI for \n".
- "XHProf at some address, you can view run at \n".
- "http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n".
- "---------------\n";
如此一来,会在上面设定的xhprof.output_dir目录里生成名字类似49bafaa3a3f66.xhprof_foo的数据文件,可以很方便的通过Web方式浏览效果:
http://<xhprof-ui-address>/index.php?run=49bafaa3a3f66&source=xhprof_foo
目前显示的是表格形式的显示,点击页面上的[View Full Callgraph],就能看到精美的图片显示了。看看下面的screenshot.


结果分析
引用
主要指标:
Inclusive Time (或子树时间):包括子函数所有执行时间。
Exclusive Time/Self Time:函数执行本身花费的时间,不包括子树执行时间。
Wall时间:花去了的时间或挂钟时间。
CPU时间:用户耗的时间+内核耗的时间
# 如果xhprof_enable函数写作:xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY)可以输出更多指标。
Function Name 函数名
Calls 调用次数
Calls% 调用百分比
# 消耗时间
Incl. Wall Time (microsec) 调用的包括子函数所有花费时间 以微秒算(一百万分之一秒)
IWall% 调用的包括子函数所有花费时间的百分比
Excl. Wall Time (microsec) 函数执行本身花费的时间,不包括子树执行时间,以微秒算(一百万分之一秒)
EWall% 函数执行本身花费的时间的百分比,不包括子树执行时间
# 消耗CPU
Incl. CPU(microsecs) 调用的包括子函数所有花费的cpu时间。减Incl. Wall Time即为等待cpu的时间
ICpu% Incl. CPU(microsecs)的百分比
Excl. CPU(microsec) 函数执行本身花费的cpu时间,不包括子树执行时间,以微秒算(一百万分之一秒)。
ECPU% Excl. CPU(microsec)的百分比
# 消耗内存
Incl.MemUse(bytes) 包括子函数执行使用的内存。
IMemUse% Incl.MemUse(bytes)的百分比
Excl.MemUse(bytes) 函数执行本身内存,以字节算
EMemUse% Excl.MemUse(bytes)的百分比
# 消耗内存峰值
Incl.PeakMemUse(bytes) Incl.MemUse的峰值
IPeakMemUse% Incl.PeakMemUse(bytes) 的峰值百分比
Excl.PeakMemUse(bytes) Excl.MemUse的峰值
EPeakMemUse% EMemUse% 峰值百分比
来源:http://koda.iteye.com/blog/463891