调试PHP时,XDebug一直是大众的不二选择,搭配上Webgrind,可以获得不错的效果。今天看某人的栖息地里的介绍,才发现了XHProf,于是体验了一下。XHProf 比Xdebug信息更详细些。
另外,在我本机 UBuntu10.10 KCacheGrind
“could not open the file "/home/danssion/web/test/xhprof/output/4eb75f171b0c5.7k7k". Check it exists and you have enough permissions to read it.” 绝对不是权限问题,更改文件权限,甚至重新新建本用户文件 通用打不开。
另外按照觉大多数的网络教程安装都会有一下提示:
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.
解决方式 ,按本教程操作。
1.安装XHProf
- http://pecl.php.net/get/xhprof-0.9.2.tgz
- zxf xhprof-0.9.2.tgz
- xhprof-0.9.2
- -r xhprof_html xhprof_lib <directory_for_htdocs>
- 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.
- ;
3. 安装Graphviz
- http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz
- zxf graphviz-2.24.0.tar.gz
- graphviz-2.24.0
- ./configure
- make
- make install
编译graphviz提示信息png: No (missing png.h),也就是dot 不支持PNG,所以需要安装libpng包,如:
- http://nchc.dl.sourceforge.net/project/libpng/libpng15/1.5.1/libpng-1.5.1.tar.gz
- zxf libpng-1.5.1.tar.gz
- libpng-1.5.1
- ./configure
- make
- make install
也可采用 apt-get libpng 安装库文件。
然后重新编译安装graphviz,加上参数--with-png=yes。完成后,应确保命令dot在PATH环境变量里(默认应该就在路径里,一般不需要特别设置),以便XHProf能找到它。
使用XHProf
在你要监测的Php代码头尾部分别加入代码xhprof_enable()和xhprof_disable()
// start profiling
xhprof_enable();
// run program
....
// stop profiler
$xhprof_data = xhprof_disable();
//
// Saving the XHProf run
// using the default implementation of iXHProfRuns.
//
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.
#php相关