wget http://pecl.php.net/get/xhprof-0.9.4.tgz
tar zxvf xhprof-0.9.4.tgz
cd xhprof-0.9.4/extension/
/usr/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
mkdir /tmp/xhprof
chown -R www:www /usr/local/src/xhprof-0.9.4
# 编辑php.ini:
[xhprof]
extension = xhprof.so
xhprof.output_dir=/tmp/xhprof
重启服务 service php-fpm restart
# 最后返回数组,就表示安装好了。具体哪些值是什么意思先别管,由于下面有UI的配置。会很直观!
yum -y install libjpeg freetype freetype-devel libjpeg-devel liberation-sans-fonts.noarch
自动安装 yum -y install graphviz
为Xhprof配置一个访问站点(虚拟主机)
好比作一个虚拟域名 dev.xhprof.com
绑定到xhprof的站点根目录 /usr/local/xhprof-0.9.4/xhprof_html
# 找到你要分析的代码,在代码开始处添加,start profiling,将会统计内存占用状况
xhprof_enable(XHPROF_FLAGS_MEMORY);
# 在代码结束位置添加
$xhprof_data = xhprof_disable(); // stop profiler, display raw xhprof data for the profiler run
include_once ("/usr/local/src/xhprof-0.9.4/xhprof_lib/utils/xhprof_lib.php"); # 请注意设置站点 include_path 权限
include_once ("/usr/local/src/xhprof-0.9.4/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");
$str = "\r\n------------------\r\n".
"Assuming you have set up the http based UI for \r\n".
"XHProf at some address, you can view run at \r\n".
"http://dev.xhprof.com/index.php?run=$run_id&source=xhprof_foo".
"\r\n------------------\r\n";
echo nl2br($str);
而后进入程序输出的网址(好比 http://dev.xhprof.com/index.php?run=52c0ea0bef834&source=xhprof_foo ),进行查看
下面是一些参数说明
Inclusive Time 包括子函数全部执行时间。
Exclusive Time/Self Time 函数执行自己花费的时间,不包括子树执行时间。
Wall Time 花去了的时间或挂钟时间。
CPU Time 用户耗的时间+内核耗的时间
Inclusive CPU 包括子函数一块儿所占用的CPU
Exclusive CPU 函数自身所占用的CPU
点击 [View Full Callgraph] 可以以图文的形式查看,很方便
注意: 须要使用ctype这个扩展,Callgraph图片生成依赖一些PHP系统级的函数,因此,最好去掉 php.ini 中的函数禁用