php性能分析工具xhprof

参考文章:http://www.chenglin.name/php/optimization/439.html

http://pecl.php.net/get/xhprof-0.9.4.tgz 
官网下载:http://pecl.php.net/package/xhprof
 

cd xhprof-0.9.4/extension/

/apps/svr/php/bin/phpize


./configure --enable-xhprof --with-php-config=/apps/svr/php/bin/php-config


make && make install


vim /apps/svr/php/etc/php.ini (找下自己的位置php.ini)


[xhprof]extension=xhprof.so;下面的这个地址是用来保存测量记录的目录,

在页面输出测量得到的数据的时候,它会自动到这儿来找输出的文件。xhprof.output_dir="/tmp/xhprof/"

  1. [xhprof]  
  2. extension=xhprof.so;  
  3. ; directory used by default implementation of the iXHProfRuns  
  4. ; interface (namely, the XHProfRuns_Default class) for storing  
  5. ; XHProf runs.  
  6. ;  
  7. ;xhprof.output_dir=<directory_for_storing_xhprof_runs>  
  8. xhprof.output_dir=/tmp/xhprof  

 

 3、拷贝文件

       把xhprof-0.9.4目录下的目录xhprof_html 和 xhprof_lib 下的所有文件拷贝到你网站的主目录下,假设网站主目录:/opt/wwwroot/

重启php-fpm


 <?phpxhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY); function bar($x) {  if ($x > 0) {    bar($x - 1);  }}function foo() {  for ($idx = 0; $idx < 5; $idx++) {    bar($idx);    $x = strlen("abc");  }}// start profilingxhprof_enable();// run programfoo();// stop profiler$xhprof_data = xhprof_disable();// display raw xhprof data for the profiler runprint_r($xhprof_data);/*echo 1;$xhprof_data = xhprof_disable();// display raw xhprof data for the profiler runprint_r($xhprof_data);*/$XHPROF_ROOT = realpath(dirname(__FILE__)).'/xhprof';///mnt/hgfs/project/www.shanxinhui.com/xhprof/xhprof_lib/utils/xhprof_lib.php // echo $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";exit('ffaaa');include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php"; 
 // save raw data for this profiler run using default// implementation of iXHProfRuns.$xhprof_runs = new XHProfRuns_Default();//print_r($xhprof_runs );exit;// save the run under a namespace "xhprof_foo"$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo"); 
 echo "---------------\n". 
 "Assuming you have set up the http based UI for \n". 
 "XHProf at some address, you can view run at \n". 
 "http://www.shanxinhui.com/xhprof/xhprof_html/index.php?run=$run_id&source=xhprof_foo\n". 
 "---------------\n";exit('faaaa');  

exit('fffffffff');

运行www.sample.com

Array( [foo==>bar] => Array ( [ct] => 5 [wt] => 17 [cpu] => 0 [mu] => 5040 [pmu] => 0 ) [bar==>bar@1] => Array ( [ct] => 4 [wt] => 9 [cpu] => 0 [mu] => 3888 [pmu] => 0 ) [bar@1==>bar@2] => Array ( [ct] => 3 [wt] => 5 [cpu] => 0 [mu] => 2848 [pmu] => 0 ) [bar@2==>bar@3] => Array ( [ct] => 2 [wt] => 2 [cpu] => 0 [mu] => 1792 [pmu] => 0 ) [bar@3==>bar@4] => Array ( [ct] => 1 [wt] => 0 [cpu] => 0 [mu] => 752 [pmu] => 0 ) [main()==>foo] => Array ( [ct] => 1 [wt] => 30 [cpu] => 0 [mu] => 6112 [pmu] => 0 ) [main()] => Array ( [ct] => 1 [wt] => 35 [cpu] => 0 [mu] => 7120 [pmu] => 0 ))---------------Assuming you have set up the http based UI for XHProf at some address, you can view run at http://www.sample.com/xhprof/xhprof_html/index.php?run=5897dc245ab5f&source=xhprof_foo---------------faaaa

执行http://www.sample.com/xhprof/xhprof_html/index.php?run=5897dc245ab5f&source=xhprof_foo 出现下图

简述:XHProf是一个分层PHP性能分析工具。它报告函数级别的请求次数和各种指标,包括阻塞时间,CPU时间和内存使用情况。一个函数的开 销,可细分成调用者和被调用者的开销,XHProf数据收集阶段,它记录调用次数的追踪和包容性的指标弧在动态callgraph的一个程序。它独有的数 据计算的报告/后处理阶段。在数据收集时,XHProfd通过检测循环来处理递归的函数调用,并通过给递归调用中每个深度的调用一个有用的命名来避开死循 环。XHProf分析报告有助于理解被执行的代码的结构,它有一个简单的HTML的用户界面( PHP写成的)。基于浏览器的性能分析用户界面能更容易查看,或是与同行们分享成果。也能绘制调用关系图。

  #2.安装Graphviz
wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.28.0.tar.gz
这个能下载:http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz 
目前最新:http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.34.0.tar.gz 
tar zxf graphviz-2.28.0.tar.gz
cd graphviz-2.28.0
./configure --prefix=/elain/apps/graphviz
make  && make install

点击

[View Full Callgraph]

看效果图

thinkphp 使用例子

把xhprof_lib目录下的utils/xhprof_lib.php内容全部添加到你的项目的common.php文件里 

把utils/xhprof_runs.php文件copy到你的项目文件的Lib/ORG/下。(如果没有这个ORG目录,自己mkdir一下)

我的测试,直接放在ThinkPHP\Library\Org\Util  import("ORG.Util.XhprofRuns"); //引用。。与例子不一样

 

//开启调试xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);

              写代码调用model

        //停止监测$xhprof_data = xhprof_disable();import("ORG.Util.XhprofRuns"); //引用。。与例子不一样$xhprof_runs = new \XHProfRuns_Default();// xhprof_foo 指命名空间,可以为任意字符串$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");echo "---------------\n"."Assuming you have set up the http based UI for \n"."XHProf at some address, you can view run at \n"."http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n"."---------------\n";exit;

   

输出结果的含义:

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. 所占总数(时间或内存使用)的百分比


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值