php性能分析工具xhprof
安装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
编辑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。
安装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
ref:http://hi.baidu.com/thinkinginlamp/blog/item/f4bd08fa1a03ba9e59ee90fd.html
下面是使用过程:
考虑到我的本地有多个虚拟站点,因此想做一个通用的引入以便一劳永逸。
首先是新建头部和尾部文件,放到指定位置。eg:/var/www/xhprof_html/下:
xheader.php <?php xhprof_enable();
xfooter.php <?php $xhprof_data = xhprof_disable(); // display raw xhprof data for the profiler run echo "<pre style=' height: 200px; overflow-y: scroll; width: 500px; border: 1px solid #000; padding: 1em;'>"; print_r($xhprof_data); echo "</pre>"; $XHPROF_ROOT = realpath(dirname(__FILE__) .'/..'); //$XHOROF_ROOT = ''; include_once $XHPROF_ROOT . "/xhprof_lib/config.php"; include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php"; include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php"; // save raw data for this profiler run using default // implementation of iXHProfRuns. $xhprof_runs = new XHProfRuns_Default(); // save the run under a namespace "xhprof_foo" $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo"); echo "<pre>". "<a href='http://www.trasinbm.com/index.php?run=$run_id&source=xhprof_foo' target='_blank'>". "View the XH GUI for this run". "</a>\n". "</pre>\n";
下来配置apache将对每个站点加入引入文件:
php_value include_path /var/www/ php_value auto_prepend_file "xhprof_lib/xheader.php" php_value auto_append_file "xhprof_lib/xfooter.php"
当然这段也可以放到.htaccess文件中,但别忘记设置:
AllowOverride All
重启apache就可以了。
运行一段测试,得到:
然后测试结果就出来了
下面可以查看整个图表:
使用runid进行对比:/callgraph.php?run1=4f9654adefc52&run2=4f965498a1134&source=xhprof_foo&all=1
ref:
http://mirror.facebook.net/facebook/xhprof/doc.html
http://www.162cm.com/p/xhprofdoc.html#ui_setup