XHProf 性能分析

1. 安装 XHProf

# page : https://pecl.php.net/package/xhprof
wget http://pecl.php.net/get/xhprof-2.2.0.tgz
tar -zxvf xhprof-2.2.0.tgz
cd xhprof-2.2.0/extension
cd xhprof-master/extension/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-xhprof
make && make install

2. 安装 graphivz

# 安装graphviz用于查看性能图片
yum -y install graphviz

3. 配置 php

[xhprof]
extension=xhprof.so
# 指定了生成的profile文件存储的位置
xhprof.output_dir=/tmp

# 重启php  service php-fpm restart

4. 补充所需文件

将步骤1文件夹里的2个目录xhprof_htmlxhprof_lib移动到项目中

//xhprof下载压缩包中的xhprof_html和xhprof_lib
cp -r xhprof-2.2.0/xhprof_html /usr/local/nginx/html/xhprof/
cp -r xhprof-2.2.0/xhprof_lib /usr/local/nginx/html/xhprof/

Nginx 并配置域名可以访问到对应目录http://xhprof.alice.show

server{
     listen 80;
     server_name xhprof.alice.show;
     location / {
          root /usr/local/nginx/html/demo/xhprof;
          index index.html;
     }
     location ~ \.php$ {
          root /usr/local/nginx/html/demo/xhprof;
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include  fastcgi_params;
     }
 }

5. 测试demo

xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);

################# 这里用于业务逻辑开始

for ($i = 0; $i <= 10000; $i++) {
    $a = $i * $i;
}
################ 这里用于业务逻辑结束
$XHPROF_ROOT = __DIR__;
include_once __DIR__."/xhprof_lib/utils/xhprof_lib.php";
include_once __DIR__."/xhprof_lib/utils/xhprof_runs.php";
# //数据会保存在php.ini中xhprof.output_dir设置的目录去中 
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "test");

echo "http://".$_SERVER['HTTP_HOST']."/xhprof_html/index.php?run={$run_id}&source=test\n";

6. 分析结果

先运行业务代码;

然后浏览器打开 http://xhprof.alice.show/, 点击最后一次生成xhprof文件

注意到中间的View Full Callgraph链接,通过该链接我们可以看到图形化的分析结果

图中红色的部分为性能比较低,耗时比较长的部分,我们可以根据根据哪些函数被标记为红色对系统的代码进行优化

另外附上, xhprof报告字段含义:

  • Function Name:方法名称。

  • Calls:方法被调用的次数。

  • Calls%:方法调用次数在同级方法总数调用次数中所占的百分比。

  • Incl.Wall Time(microsec):方法执行花费的时间,包括子方法的执行时间。(单位:微秒)

  • IWall%:方法执行花费的时间百分比。

  • Excl. Wall Time(microsec):方法本身执行花费的时间,不包括子方法的执行时间。(单位:微秒)

  • EWall%:方法本身执行花费的时间百分比。

  • Incl. CPU(microsecs):方法执行花费的CPU时间,包括子方法的执行时间。(单位:微秒)

  • ICpu%:方法执行花费的CPU时间百分比。

  • Excl. CPU(microsec):方法本身执行花费的CPU时间,不包括子方法的执行时间。(单位:微秒)

  • ECPU%:方法本身执行花费的CPU时间百分比。

  • Incl.MemUse(bytes):方法执行占用的内存,包括子方法执行占用的内存。(单位:字节)

  • IMemUse%:方法执行占用的内存百分比。

  • Excl.MemUse(bytes):方法本身执行占用的内存,不包括子方法执行占用的内存。(单位:字节)

  • EMemUse%:方法本身执行占用的内存百分比。

  • Incl.PeakMemUse(bytes)Incl.MemUse峰值。(单位:字节)

  • IPeakMemUse%Incl.MemUse峰值百分比。

  • Excl.PeakMemUse(bytes)Excl.MemUse峰值。单位:(字节)

  • EPeakMemUse%Excl.MemUse峰值百分比。

引用资源

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值