Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5379437
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: C/C++

2013-07-26 14:13:53

调试PHP时,XDebug一直是大众的不二选择,搭配上Webgrind,可以获得不错的效果。今天看某人的栖息地里的介绍,才发现了XHProf,于是体验了一下,感觉很酷,与XDebug相比,运行更轻便(本身还包括一个web查看工具),表现更易懂,下面记录一下体验过程。 

1.安装XHProf 
Command代码  收藏代码
  1. wget 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   
  5. cd extension  
  6. phpize  
  7. ./configure  
  8. make  
  9. make install  


2 . 配置php.ini 
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=  

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

3. 安装Graphviz 
Command代码  收藏代码
  1. wget http:///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的目的是为了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包,如: 
Linux代码  收藏代码
  1. wget 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  

然后重新编译安装graphviz,加上参数--with-png=yes。完成后,应确保命令dot在PATH环境变量里(默认应该就在路径里,一般不需要特别设置),以便XHProf能找到它。 

使用XHProf 
在你要监测的Php代码头尾部分别加入代码xhprof_enable()和xhprof_disable() 
Php代码  收藏代码
  1. // start profiling  
  2. xhprof_enable();  
  3. // run program  
  4. ....  
  5. // stop profiler  
  6. $xhprof_data = xhprof_disable();  
  7. //  
  8. // Saving the XHProf run  
  9. // using the default implementation of iXHProfRuns.  
  10. //  
  11. include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";  
  12. include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";  
  13.   
  14. $xhprof_runs = new XHProfRuns_Default();  
  15.   
  16. // Save the run under a namespace "xhprof_foo".  
  17. //  
  18. // **NOTE**:  
  19. // By default save_run() will automatically generate a unique  
  20. // run id for you. [You can override that behavior by passing  
  21. // a run id (optional arg) to the save_run() method instead.]  
  22. //  
  23. $run_id = $xhprof_runs->save_run($xhprof_data"xhprof_foo");  
  24.   
  25. echo "---------------\n".  
  26. "Assuming you have set up the http based UI for \n".  
  27. "XHProf at some address, you can view run at \n".  
  28. "\n".  
  29. "---------------\n";  

如此一来,会在上面设定的xhprof.output_dir目录里生成名字类似49bafaa3a3f66.xhprof_foo的数据文件,可以很方便的通过Web方式浏览效果: 



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



阅读(3363) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~