http://aofengblog.blog.163.com/blog/static/6317021201341851510578/
一些新加入团队的同学,对XHProf不熟悉,因此业余时间编写了一个简明的教程,包括如何部署XHProf和读懂XHProf报告。
说明
$PHP_HOME
表示PHP的安装目录。
$XHPROF_UI_HOME 表示xhprof ui的存放目录(自己选择一个目录即可,如:/home/nieyong/xhprof_ui)。
XHProf安装
1、下载XHProf源码并解压。
wget http://pecl.php.net/get/xhprof-0.9.2.tgz tar zxvf xhprof-0.9.2.tgz
cd xhprof-0.9.2/extension/ $PHP_HOME/bin/phpize
./configure --with-php-config=$PHP_HOME/bin/php-config make make install
vi $PHP_HOME/lib/php.ini
;; 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
执行命令
php -m
[PHP Modules]
Core
......
xhprof
......
zlib
如果在[PHP Modules]下的扩展列表中出现了xhprof表示安装和配置成功。
Graphviz安装
1、方式一:编译源代码安装。
tar zxvf graphviz-2.28.0.tar.gz
cd graphviz-2.28.0
./configure --prefix=$HOME/local/graphviz-2.28.0
make
make install
2、方式二:二进制文件安装。
1)切换至root用户。
2)执行命令。
XHProf UI配置
1、准备XHProf UI运行文件。
将xhprof-0.9.2.tgz解压后目录中的xhprof_html和xhporf_lib复制到$XHPROF_UI_HOME中。
2、在Nginx配置XHProf UI。
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。
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分析报告。
<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
显示界面类似如下:
点击[View Full Callgraph]链接,显示方法的调用关系树,如下图:
如何看懂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峰值百分比。 |
<正文结束>
一些新加入团队的同学,对XHProf不熟悉,因此业余时间编写了一个简明的教程,包括如何部署XHProf和读懂XHProf报告。
说明
$PHP_HOME
表示PHP的安装目录。
$XHPROF_UI_HOME 表示xhprof ui的存放目录(自己选择一个目录即可,如:/home/nieyong/xhprof_ui)。
XHProf安装
1、下载XHProf源码并解压。
wget http://pecl.php.net/get/xhprof-0.9.2.tgz tar zxvf xhprof-0.9.2.tgz
cd xhprof-0.9.2/extension/ $PHP_HOME/bin/phpize
./configure --with-php-config=$PHP_HOME/bin/php-config make make install
vi $PHP_HOME/lib/php.ini
;; 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
执行命令
php -m
[PHP Modules]
Core
......
xhprof
......
zlib
如果在[PHP Modules]下的扩展列表中出现了xhprof表示安装和配置成功。
Graphviz安装
1、方式一:编译源代码安装。
tar zxvf graphviz-2.28.0.tar.gz
cd graphviz-2.28.0
./configure --prefix=$HOME/local/graphviz-2.28.0
make
make install
2、方式二:二进制文件安装。
1)切换至root用户。
2)执行命令。
XHProf UI配置
1、准备XHProf UI运行文件。
将xhprof-0.9.2.tgz解压后目录中的xhprof_html和xhporf_lib复制到$XHPROF_UI_HOME中。
2、在Nginx配置XHProf UI。
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。
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分析报告。
<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
显示界面类似如下:
点击[View Full Callgraph]链接,显示方法的调用关系树,如下图:
如何看懂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峰值百分比。 |
<正文结束>