体验XHProf(原创)

 

调试PHP时,XDebug一直是大众的不二选择,搭配上Webgrind,可以获得不错的效果。今天看某人的栖息地里的介绍,才发现了XHProf,于是体验了一下。XHProf 比Xdebug信息更详细些。

另外,在我本机 UBuntu10.10 KCacheGrind  是开不开 XHProf 文件的 ,总会有一下提示:

“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

  1. wget http://pecl.php.net/get/xhprof-0.9.2.tgz  
  2. tar zxf xhprof-0.9.2.tgz  
  3. cd xhprof-0.9.2  
  4. cp -r xhprof_html xhprof_lib <directory_for_htdocs>  
  5. cd extension  
  6. phpize  
  7. ./configure  
  8. make  
  9. make install
2 . 配置php.ini
  1. [xhprof]  
  2. extension=xhprof.so  
  3.  
  4. directory used by default implementation of the iXHProfRuns  
  5. interface (namely, the XHProfRuns_Default class) for storing  
  6. XHProf runs.  
  7.  
  8. xhprof.output_dir=<directory_for_storing_xhprof_runs> 
3. 安装Graphviz
  1. wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz  
  2. tar zxf graphviz-2.24.0.tar.gz  
  3. cd graphviz-2.24.0  
  4. ./configure  
  5. make  
  6. make install
编译graphviz提示信息png: No (missing png.h),也就是dot 不支持PNG,所以需要安装libpng包,如:
  1. wget http://nchc.dl.sourceforge.net/project/libpng/libpng15/1.5.1/libpng-1.5.1.tar.gz  
  2. tar zxf libpng-1.5.1.tar.gz  
  3. cd libpng-1.5.1  
  4. ./configure  
  5. make  
  6. 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.

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值