`

php性能分析工具xhprof的安装使用

 
阅读更多

项目域名:http://www.ajia.com

项目路径:/home/www/

 

1.安装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 /home/www/  (复制xhprof_html、xhprof_lib这两个目录到“项目目录”下,此处目的是建立xhprof的数据分析目录,可将此目录配置成虚拟主机访问)  

cd extension/  

phpize (如果没有改命令,先安装php-del)  

./configure  -–with-php-config=/usr/bin/php-config   (php-config的路径需要正常)  

make

make install  (提示安装成功后显示xhprof.so扩展的存放路径)

 

 

2.php.ini配置文件中添加

extension=xhprof.so

xhprof.output_dir=/www/logs/xhprof (这里存放的是xhprof每次性能分析的“记录”的文件,前台显示的数据就来自该文件,该目录必须是“存在且能读写”)

 

 

3.重启php生效xhprof扩展,通过phpinfo()查看是否使用了xhprof扩展

 

 

4.安装libpng包(为了能够通过图例来显示性能分析,可以不安装)

wget http://hivelocity.dl.sourceforge.net/project/libpng/libpng15/older-releases/1.5.15/libpng-1.5.15.tar.gz

tar xvzf libpng-1.5.15.tar.gz && cd libpng-1.5.15

./configure

make

make install

 

 

5.安装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 --with-png=yes

make

make install

 

 

 

php代码使用示例:

 

在头部(在所有执行php进程开始的地方,一般是头部):

xhprof_enable(); 

//xhprof_enable(XHPROF_FLAGS_NO_BUILTINS); 不记录内置的函数

//xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);  同时分析CPU和Mem的开销

$xhprof_on = true;

 

/*

//生产环境可使用:

if (mt_rand(1, 10000) == 1) {

   xhprof_enable();

   $xhprof_on = true;

}

*/

 

尾部(在所有php进程结束的地方,一般是底部):

if($xhprof_on){

$xhprof_data = xhprof_disable();

$xhprof_root = '/home/www/';  //是上面安装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://www.ajia.com/xhprof_html/index.php?run='.$run_id.'&source=hx" target="_blank">统计</a>';

}

 

/*

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

*/

 

 

 

输出结果的含义:

 

ct 函数调用次数,

wt 花费的时间,

cpu 花费的 CPU 时间(微秒即百万分之一秒),

mu 使用的内存(bytes),

pmu 使用的内存峰值(bytes)。

 

 

web 分析结果页面含义

 

 

Calls:函数的调用次数

Incl. Wall Time (microsec) :包含内部函数花费的时间,单位微秒

Excl. Wall Time (microsec):不包含内部函数花费的时间,单位微秒

及所占百分比(%)

 

注:Incl.:为 Including 包含的简写

Excl.:为 Excluding 不包含的简写

Wall Time:意为挂钟时间即任务花费的时间

 

main():一个虚构的函数,程序根节点

bar@2:递归调用 2 次

 

Incl. CPU (microsecs):包含内部函数 CPU 花费的时间,单位微秒

Excl. CPU (microsec):不包含内部函数 CPU 花费的时间,单位微秒

Incl. MemUse (bytes):包含内部函数所占内存,单位字节

Excl. MemUse (bytes):不包含内部函数所占内存,单位字节

Incl. PeakMemUse (bytes):包含内部函数所占内存峰值,单位字节

Excl. PeakMemUse (bytes):不包含内部函数所占内存峰值,单位字节

及所占百分比(%)

 

可以认为共三种情况:

1. 包括内部函数

2. 不包括内部函数或者说函数本身

3. 所占总数(时间或内存使用)的百分比

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics