之乎者也2011

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

调试PHP时,XDebug一直很好选择,搭配上Webgrind,可以获得不错的效果,但是很耗资源,CPU一会就到100%了。最近看别人的文章,发现了XHProf,于是体验了一下。感觉很酷,与 XDebug 相比,运行更轻便,表现更易懂。这个软件本是 Facebook 内部的一个应用工具,2009年3月份开源,为PHP的性能监测提供了很好的工具。

安装

#wget http://pecl.php.net/get/xhprof-0.9.2.tgz
一开始wget用不了,报错:Resolving dada… failed: Temporary failure in name resolution.
查明是DNS解析的原因,然后更改 /etc/resolv.conf,问运维,写上正确的IP。
#tar zxf xhprof-0.9.2.tgz
#cd cd xhprof-0.9.2/extension/
#phpize
#./configure –with-php-config=/usr/bin/php-config
#make
#make install
#make test

然后配置 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 = 这里写你要保存xhp生成log的目录

done!!下面测试下

function bar($x){ 
	if ($x --> 0){
		bar($x - 1);
	}
}
function foo() {
	for ($idx = 0; $idx < 2; $idx++) 
	{ 		
		bar($idx); 		
		$x = strlen("abc"); 	
	} 
} 
xhprof_enable(); #start profiling
foo(); #run program
$xhprof_data = xhprof_disable(); #stop profiler
print_r($xhprof_data); #display raw xhprof data for the profiler run

最后返回数组,就表示安装好了。具体哪些值是什么意思先别管,因为下面有UI的配置。会很直观!

使用

首先,为了直观和美观装个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

安装完成后,会生成 /usr/local/bin/dot文件,你应该确保路径在PATH环境变量里,以便XHProf能找到它。然后把上面的PHP测试代码拿过来改改,在最后面,加上几行:

<?php
include_once ("/Data/web/changyou/xhprof_lib/utils/xhprof_lib.php");
include_once ("/Data/web/changyou/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 "---------------".
"Assuming you have set up the http based UI for".
"XHProf at some address, you can view run at".
"http://192.168.5.148/changyou/xhprof_html/index.php?run=$run_id&source=xhprof_foo".
"---------------";
?>

然后,好了,运行那个打印出来的URL,OK啦!
看看效果,如果你安装了Graphviz,还会有华丽的大图噢!

http://www.auu.name/153/index.html

posted on 2011-09-05 05:26  之乎者也2011  阅读(656)  评论(0编辑  收藏  举报