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 /www/www.hx.com/xhprof/
cd extension/
/usr/local/webserver/php/bin/phpize
./configure  --with-php-config=/usr/local/webserver/php/bin/php-config
make && make install

安装完提示:

Installing shared extensions:     /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/

php.ini中添加

extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"

这句我原来就有了,就这用添加下面两句

extension=xhprof.so
xhprof.output_dir=/www/logs/xhprof

分析日志输出在/www/logs/xhprof目录。


重新加载php配置文件和重启web

/usr/local/webserver/php/sbin/php-fpm reload
/usr/local/webserver/nginx/sbin/nginx -s reload

刷新phpinfo页面,看到输出中有了xhprof信息。
xhprof  0.9.2
CPU num  2

安装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

程序试例
头部:

xhprof_enable();
//xhprof_enable(XHPROF_FLAGS_NO_BUILTINS); 不记录内置的函数
//xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);  同时分析CPU和Mem的开销
$xhprof_on = true;

我觉得用xhprof_enable();就够用了,只统计运行时间(Wall Time)。

生产环境可使用:

if (mt_rand(1, 10000) == 1) {
   xhprof_enable();
   $xhprof_on = true;
}

尾部:

if($xhprof_on){
$xhprof_data = xhprof_disable();
$xhprof_root = '/www/www.hx.com/xhprof/';
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();
$run_id = $xhprof_runs->save_run($xhprof_data, "hx");
echo '<a href="http://192.168.1.158:858/xhprof/xhprof_html/index.php?run='.$run_id.'&source=hx" target="_blank">统计</a>';
}

运行程序,底部出现统计字样,点过去就可以看到性能分析了。按运行时间排序,很容易找出化时间最长的函数。
点[View Full Callgraph]图形化显示,最大的性能问题会用红色标出,其次是黄色,很明显。

名词:
   Function Name 函数名

   Calls 调用次数

   Calls% 调用百分比

   Incl. Wall Time (microsec) 调用的包括子函数所有花费时间 以微秒算(一百万分之一秒)

   IWall% 调用的包括子函数所有花费时间的百分比

   Excl. Wall Time (microsec) 函数执行本身花费的时间,不包括子树执行时间,以微秒算(一百万分之一秒)

   EWall% 函数执行本身花费的时间的百分比,不包括子树执行时间

   Incl. CPU(microsecs) 调用的包括子函数所有花费的cpu时间。减Incl. Wall Time即为等待cpu的时间

   减Excl. Wall Time即为等待cpu的时间

   ICpu% Incl. CPU(microsecs)的百分比

   Excl. CPU(microsec) 函数执行本身花费的cpu时间,不包括子树执行时间,以微秒算(一百万分之一秒)。

   ECPU% Excl. CPU(microsec)的百分比

   Incl.MemUse(bytes) 包括子函数执行使用的内存。

   IMemUse% Incl.MemUse(bytes)的百分比

   Excl.MemUse(bytes) 函数执行本身内存,以字节算

   EMemUse% Excl.MemUse(bytes)的百分比

   Incl.PeakMemUse(bytes) Incl.MemUse的峰值

   IPeakMemUse% Incl.PeakMemUse(bytes) 的峰值百分比

   Excl.PeakMemUse(bytes) Excl.MemUse的峰值

   EPeakMemUse% EMemUse% 峰值百分比

posted @ 2014-04-11 17:53  [九狐科技]keheng  阅读(200)  评论(0编辑  收藏  举报