PHP性能分析工具-xhprof扩展的安装以及使用

Share on:

简述

安装

% wget http://pecl.php.net/get/xhprof-0.9.4.tgz
% tar zxf xhprof-0.9.4.tgz
% cd <xhprof_source_directory>/extension/
% phpize
% ./configure --with-php-config=<path to php-config>
% 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> //日志路径需要修改 记得给写权限
% php-fpm reload //重启一下fpm
% 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

使用分析

if(function_exists('xhprof_enable')){
	//if (mt_rand(1, 1000) == 1) { 生产环境中开启
	$xhprof_on = true;
	xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
	define('XHPROF_ROOT',realpath(dirname(__FILE__)));
	include_once XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
	include_once XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
	//}
}
//程序源码
if ($xhprof_on) {
	$xhprof_data = xhprof_disable();
	// 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");
}

访问域名,然后访问域名/xhprof_html/就可以看到屌爆了的报表了!

【相关解释】
Inclusive Time (或子树时间):包括子函数所有执行时间。
Exclusive Time/Self Time:函数执行本身花费的时间,不包括子树执行时间。
Wall时间:花去了的时间或挂钟时间。
CPU时间:用户耗的时间+内核耗的时间

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% 峰值百分比

闽ICP备12003472号-7