XHProf是facebook开源出来的一个php轻量级的性能分析工具,跟Xdebug类似,但性能开销更低,还可以用在生产环境中,也可以由程序开 关来控制是否进行profile。
安装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/
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.soxhprof.output_dir=/www/log/xhprof
分析日志输出在/www/log/xhprof目录。
注意:如果/www/log/xhprof这个目录没有的话,需要手动创建,注意权限。
mkdir /www/log/xhprof
chown -R nobody.nobody /www/log/xhprof
重新加载php配置文件和重启web
/usr/local/webserver/php/sbin/php-fpm reload/usr/local/webserver/nginx/sbin/nginx -s reload
安装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
注意:如果出现报错信息:Error: either we can not find profile data for run_id 4d7f0bd99a12f or the threshold 0.01 is too small or you do not have ‘dot’ image generation utility installed.
原因为:xhprof绘制的是png图,graphviz-2.24.0不支持。绘图的dot拓展没装成功, 不支持PNG。解决方法:wget http://sourceforge.net/projects/libpng/files/libpng15/1.5.1/libpng-1.5.1.tar.gz ./configure make make install重装 graphvizcd 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();
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, “xhprof_foo”);
echo ‘<a href=”http://10.0.5.119/xhprof_html/index.php?run='.$run_id.'&source=xhprof_foo“ target=”_blank”>统计</a>’;
}
运行程序,底部出现统计字样,点过去就可以看到性能分析了。按运行时间排序,很容易找出化时间最长的函数。
点[View Full Callgraph]图形化显示,最大的性能问题会用红色标出,其次是黄色,很明显。
官方文档:http://mirror.facebook.net/facebook/xhprof/doc.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% 峰值百分比