PHP - XHProf简明教程

来源:互联网 发布:思维导图软件mac版 编辑:程序博客网 时间:2024/06/07 04:24

http://aofengblog.blog.163.com/blog/static/6317021201341851510578/



PHP - XHProf简明教程 - 傲风 - 宝剑锋从磨砺出 梅花香自苦寒来
 
之前用PHP做互联网SNS应用,会定期进行性能分析,找出消耗时间最多的地方。通过对几款工具进行试验和对比后,采用了XHProf。XHProf是Facebook的开源项目,用于分析PHP应用运行性能的轻量级工具,开销很低,可以用在生产环境中。
一些新加入团队的同学,对XHProf不熟悉,因此业余时间编写了一个简明的教程,包括如何部署XHProf和读懂XHProf报告。

说明

$PHP_HOME 表示PHP的安装目录。
$XHPROF_UI_HOME 表示xhprof ui的存放目录(自己选择一个目录即可,如:/home/nieyong/xhprof_ui)。

PHP - XHProf简明教程 - 傲风 - 宝剑锋从磨砺出 梅花香自苦寒来注:命令行和配置文件中如果包含上述两个变量需替换成实际的路径

XHProf安装

1、下载XHProf源码并解压。
wget http://pecl.php.net/get/xhprof-0.9.2.tgztar zxvf xhprof-0.9.2.tgz
2、编译前预处理。
cd xhprof-0.9.2/extension/$PHP_HOME/bin/phpize
3、编译三步曲。
./configure --with-php-config=$PHP_HOME/bin/php-configmakemake install
4、增加PHP配置。
vi $PHP_HOME/lib/php.ini
在php.ini中配置extension 。
;; php载入扩展模块的目录extension_dir="$PHP_HOME/lib/php/extension/no-debug-non-zts-20090626"[xhprof]extension=xhprof.so;; xhprof日志输出目(根据实际环境修改)xhprof.output_dir=/home/nieyong/profile/xhprof
5、验证安装结果。
执行命令
php -m
输出结果类似如下:
[PHP Modules] 
Core
...... 
xhprof
...... 
zlib
 


如果在[PHP Modules]下的扩展列表中出现了xhprof表示安装和配置成功。

Graphviz安装

1、方式一:编译源代码安装。
wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.28.0.tar.gz
tar zxvf graphviz-2.28.0.tar.gz
cd graphviz-2.28.0
./configure --prefix=$HOME/local/graphviz-2.28.0
make
make install
提示:用编译源代码的方式安装后,要将dot所在目录添加到环境变量PATH中。

2、方式二:二进制文件安装。
1)切换至root用户。
2)执行命令。
yum install graphviz
提示:在我的Redhat-5.7上用源代码编译安装没有成功,因为要依赖一堆其他组件以及版本兼容问题,安装非常麻烦,用yum方式简单快速。

XHProf UI配置

1、准备XHProf UI运行文件。
将xhprof-0.9.2.tgz解压后目录中的xhprof_html和xhporf_lib复制到$XHPROF_UI_HOME中。

2、在Nginx配置XHProf UI。
server {
    listen       9090;
    location / {
        root   $XHPROF_UI_HOME/xhprof_html;
        index  index.php index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    location ~ \.php$ {
        root           $XHPROF_UI_HOME/xhprof_html;
        fastcgi_pass   127.0.0.1:9001;   #fpm端口
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME  $XHPROF_UI_HOME/xhprof_html/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

编写测试代码生成XHProf分析报告

1、编写测试代码xhprof_test.php。
<?php
include_once '$XHPROF_UI_HOME/xhprof_lib/utils/xhprof_lib.php';
include_once '$XHPROF_UI_HOME/xhprof_lib/utils/xhprof_runs.php';

function bar($x) 
{
  if ($x > 0) 
  {
    bar($x - 1);
  }
}

function foo() 
{
  for ($idx = 0; $idx < 2; $idx++) 
  {
    bar($idx);
    $x = strlen("abc");
  }
}

// start profiling
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

// run program
foo();

// stop profiler
$xhprof_data = xhprof_disable();

$profiler_namespace="hello";
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace);
 
// url to the XHProf UI libraries (change the host name and path)
$profiler_url = sprintf('http://127.0.0.1:9090/index.php?run=%s&source=%s', $run_id, $profiler_namespace);
echo '<a href="http://aofengblog.blog.163.com/blog/'. $profiler_url .'" target="_blank">Profiler output</a>\\n';

2、运行测试代码并生成XHProf分析报告。
php ./xhprof_test.php
输出结果类似如下:
<a href="http://127.0.0.1:9090/index.php?run=4eface3370442&source=hello" target="_blank">Profiler output</a>

3、查看XHProf分析报告。
在浏览器上输入地址:http://127.0.0.1:9090/index.php?run=4eface3370442&source=hello
显示界面类似如下:
PHP - XHProf简明教程 - 傲风 - 宝剑锋从磨砺出 梅花香自苦寒来
图:XHProf性能分析报告 

点击[View Full Callgraph]链接,显示方法的调用关系树,如下图:
PHP - XHProf简明教程 - 傲风 - 宝剑锋从磨砺出 梅花香自苦寒来
图:方法调用树

PHP - XHProf简明教程 - 傲风 - 宝剑锋从磨砺出 梅花香自苦寒来提示:生成方法调用关系图由Graphviz生成。
 

如何看懂XHProf分析报告

如图1所示,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峰值百分比。


<正文结束>

PHP - XHProf简明教程 - 傲风 - 宝剑锋从磨砺出 梅花香自苦寒来
 
之前用PHP做互联网SNS应用,会定期进行性能分析,找出消耗时间最多的地方。通过对几款工具进行试验和对比后,采用了XHProf。XHProf是Facebook的开源项目,用于分析PHP应用运行性能的轻量级工具,开销很低,可以用在生产环境中。
一些新加入团队的同学,对XHProf不熟悉,因此业余时间编写了一个简明的教程,包括如何部署XHProf和读懂XHProf报告。

说明

$PHP_HOME 表示PHP的安装目录。
$XHPROF_UI_HOME 表示xhprof ui的存放目录(自己选择一个目录即可,如:/home/nieyong/xhprof_ui)。

PHP - XHProf简明教程 - 傲风 - 宝剑锋从磨砺出 梅花香自苦寒来注:命令行和配置文件中如果包含上述两个变量需替换成实际的路径

XHProf安装

1、下载XHProf源码并解压。
wget http://pecl.php.net/get/xhprof-0.9.2.tgztar zxvf xhprof-0.9.2.tgz
2、编译前预处理。
cd xhprof-0.9.2/extension/$PHP_HOME/bin/phpize
3、编译三步曲。
./configure --with-php-config=$PHP_HOME/bin/php-configmakemake install
4、增加PHP配置。
vi $PHP_HOME/lib/php.ini
在php.ini中配置extension 。
;; php载入扩展模块的目录extension_dir="$PHP_HOME/lib/php/extension/no-debug-non-zts-20090626"[xhprof]extension=xhprof.so;; xhprof日志输出目(根据实际环境修改)xhprof.output_dir=/home/nieyong/profile/xhprof
5、验证安装结果。
执行命令
php -m
输出结果类似如下:
[PHP Modules] 
Core
...... 
xhprof
...... 
zlib
 


如果在[PHP Modules]下的扩展列表中出现了xhprof表示安装和配置成功。

Graphviz安装

1、方式一:编译源代码安装。
wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.28.0.tar.gz
tar zxvf graphviz-2.28.0.tar.gz
cd graphviz-2.28.0
./configure --prefix=$HOME/local/graphviz-2.28.0
make
make install
提示:用编译源代码的方式安装后,要将dot所在目录添加到环境变量PATH中。

2、方式二:二进制文件安装。
1)切换至root用户。
2)执行命令。
yum install graphviz
提示:在我的Redhat-5.7上用源代码编译安装没有成功,因为要依赖一堆其他组件以及版本兼容问题,安装非常麻烦,用yum方式简单快速。

XHProf UI配置

1、准备XHProf UI运行文件。
将xhprof-0.9.2.tgz解压后目录中的xhprof_html和xhporf_lib复制到$XHPROF_UI_HOME中。

2、在Nginx配置XHProf UI。
server {
    listen       9090;
    location / {
        root   $XHPROF_UI_HOME/xhprof_html;
        index  index.php index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    location ~ \.php$ {
        root           $XHPROF_UI_HOME/xhprof_html;
        fastcgi_pass   127.0.0.1:9001;   #fpm端口
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME  $XHPROF_UI_HOME/xhprof_html/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

编写测试代码生成XHProf分析报告

1、编写测试代码xhprof_test.php。
<?php
include_once '$XHPROF_UI_HOME/xhprof_lib/utils/xhprof_lib.php';
include_once '$XHPROF_UI_HOME/xhprof_lib/utils/xhprof_runs.php';

function bar($x) 
{
  if ($x > 0) 
  {
    bar($x - 1);
  }
}

function foo() 
{
  for ($idx = 0; $idx < 2; $idx++) 
  {
    bar($idx);
    $x = strlen("abc");
  }
}

// start profiling
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

// run program
foo();

// stop profiler
$xhprof_data = xhprof_disable();

$profiler_namespace="hello";
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace);
 
// url to the XHProf UI libraries (change the host name and path)
$profiler_url = sprintf('http://127.0.0.1:9090/index.php?run=%s&source=%s', $run_id, $profiler_namespace);
echo '<a href="http://aofengblog.blog.163.com/blog/'. $profiler_url .'" target="_blank">Profiler output</a>\\n';

2、运行测试代码并生成XHProf分析报告。
php ./xhprof_test.php
输出结果类似如下:
<a href="http://127.0.0.1:9090/index.php?run=4eface3370442&source=hello" target="_blank">Profiler output</a>

3、查看XHProf分析报告。
在浏览器上输入地址:http://127.0.0.1:9090/index.php?run=4eface3370442&source=hello
显示界面类似如下:
PHP - XHProf简明教程 - 傲风 - 宝剑锋从磨砺出 梅花香自苦寒来
图:XHProf性能分析报告 

点击[View Full Callgraph]链接,显示方法的调用关系树,如下图:
PHP - XHProf简明教程 - 傲风 - 宝剑锋从磨砺出 梅花香自苦寒来
图:方法调用树

PHP - XHProf简明教程 - 傲风 - 宝剑锋从磨砺出 梅花香自苦寒来提示:生成方法调用关系图由Graphviz生成。
 

如何看懂XHProf分析报告

如图1所示,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