一、什么是XHProf
XHProf官网:http://pecl.php.net/package/xhprof
XHProf是一个分层PHP性能分析工具。它报告函数级别的请求次数和各种指标,包括 阻塞时间,CPU时间和内存使用情况。一个函数的开销,可细分成调用者和被调用者的开销,XHProf数据收集阶段,它记录调用次数的追踪和包容性的指标 弧在动态callgraph的一个程序。它独有的数据计算的报告/后处理阶段。在数据收集时,XHProfd通过检测循环来处理递归的函数调用,并通过给 递归调用中每个深度的调用一个有用的命名来避开死循环。XHProf分析报告有助于理解被执行的代码的结构,它有一个简单的HTML的用户界面( PHP写成的)。基于浏览器的性能分析用户界面能更容易查看,或是与同行们分享成果。也能绘制调用关系图。
二、安装XHProf扩展模块
1.安装
1 | wget -c http://pecl.php.net/get/xhprof-0.9.2.tgz |
2 | tar -xzvf ./xhprof-0.9.2.tgz |
3 | cp ./xhprof-0.9.2.tgz ./www //xhprof自身带有一个web版的分析页面,放到我的web服务器下面 |
4 | cd ./xhprof-0.9.2/extension |
5 | /usr/ local /php/bin/phpize |
6 | ./configure -- enable -xhprof --with-php-config=/usr/ local /php/bin/php-config |
7 | make && make install |
2.配置,在php.ini后追加如下内容:
1 | [xhprof] |
2 | extension=xhprof.so |
3 | xhprof.output_dir=/data/xhprof //如果不加存放目录的话,默认是放在/tmp下面 (注意:要有写的权限,直接 777 吧)! |
3.重启php-fpm
1 | service php-fpm restart |
三、XHProf测试
前面我们说过了,XHProf自身带有一个web版的测试工具,里面还有一个小例子。看一下这个例子,我做了一点修改和注释:
01 | <?php |
02 | function bar( $x ) |
03 | { |
04 | if ( $x > 0) |
05 | { |
06 | bar( $x - 1); |
07 | } |
08 | } |
09 |
10 | function foo() |
11 | { |
12 | for ( $idx = 0; $idx < 5; $idx ++) |
13 | { |
14 | bar( $idx ); |
15 | $x = strlen ( "abc" ); |
16 | } |
17 | } |
18 |
19 | // 启动xhprof |
20 | xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); |
21 |
22 | // 调用foo函数,也是我们要分析的函数 |
23 | foo(); |
24 |
25 | // 停止xhprof |
26 | $xhprof_data = xhprof_disable(); |
27 |
28 | // 取得统计数据 |
29 | print_r( $xhprof_data ); |
30 |
31 | $XHPROF_ROOT = realpath (dirname( __FILE__ ) . '/..' ); |
32 | include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php" ; |
33 | include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php" ; |
34 |
35 | // 保存统计数据,生成统计ID和source名称 |
36 | $xhprof_runs = new XHProfRuns_Default(); |
37 | $run_id = $xhprof_runs ->save_run( $xhprof_data , "xhprof_foo" ); //source名称是xhprof_foo |
38 |
39 | // 查看统计信息 |
40 | echo "<div style='margin: 50px auto;width:200px;'><a href='../xhprof_html/index.php?run=" . $run_id . "&source=xhprof_foo' target='_blank'>XHProf view</a></s></div>" ; |
41 | ?> |
以下是部分的结果:
1 | [foo==>bar] => Array |
2 | ( |
3 | [ct] => 5 //bar()这个函数被调用了5次 |
4 | [wt] => 63 //每次运行bar()所要的时间,不知道这个是不是平均值 |
5 | [cpu] => 0 //每次运行bar(),cpu运算时间 |
6 | [mu] => 2860 //每次运行bar(),php所使用内存的改变 |
7 | [pmu] => 0 //每次运行bar(),php在内存使用最高峰时,所使用内存的改变 |
8 | ) |
点击链接“XHProf view”,可查看到分析结果:
结果分析:
Inclusive Time (或子树时间):包括子函数所有执行时间。
Exclusive Time/Self Time:函数执行本身花费的时间,不包括子树执行时间。
Wall时间:花去了的时间或挂钟时间。
CPU时间:用户耗的时间+内核耗的时间。
表单中的:
列名 | 描述 |
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 Zend 扩展提供的PHP函数介绍
1).xhprof_enable
函数原型:void xhprof_enable ([ int $flags = 0 [, array $options ]] )
功能:启动 xhprof 性能分析器
参数:$flags可选标识,由XHProf扩展定义,XHPROF_FLAGS_NO_BUILTINS 使得跳过所有内置(内部)函数,XHPROF_FLAGS_CPU使输出的性能数据中添加 CPU 数据,XHPROF_FLAGS_MEMORY使输出的性能数据中添加内存数据,多个标识可通过位运算进行组合;$options array类型的可选选项,就是通过传递 'ignored_functions' 选项来忽略性能分析中的某些函数。
返回值:无
说明:在开始分析代码时调用此函数
2).xhprof_disable
函数原型:array xhprof_disable ( void )
功能:停止 xhprof 分析器
参数:无
返回值:本次运行的 array 类型的 xhprof 数据
说明:在结束分析代码时调用此函数
3).xhprof_sample_enable
函数原型:void xhprof_sample_enable ( void )
功能:以采样模式启动 XHProf 性能分析
参数:无
返回值:无
说明:xhprof_enable() 的更轻量的版本,以采样模式开始性能分析。 抽样的间隔为 0.1 秒,样本记录了完整的函数调用堆栈。 主要使用的情况是以较低的性能开销来进行性能监控和诊断。
4).xhprof_sample_disable
函数原型:array xhprof_sample_disable ( void )
功能:停止 xhprof 性能采样分析器
参数:无
返回值:本次运行的xhprof采样数据,array 类型
说明:暂无
四、安装Graphviz,一个画图工具:
1.安装
01 | # 安装libpng |
02 | wget -c http://sourceforge.net/projects/libpng/files/libpng15/1.5.19/libpng-1.5.19. tar .gz |
03 | tar -xzvf ./libpng-1.5.19. tar .gz |
04 | cd ./libpng-1.5.19 |
05 | ./configure |
06 | make && make install |
07 |
08 | # 安装graphviz |
09 | wget -c http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0. tar .gz |
10 | tar -xzvf ./graphviz-2.24.0. tar .gz |
11 | cd ./graphviz-2.24.0 |
12 | ./configure |
13 | make && make install |
2.测试
点击链接“View Full Callgraph”,可以查看到一张统计分析图:
红色的部分基本是耗时比较多的地方,提醒您需要优化(当然这仅仅是供参考而已)!
注意事项:
1、如果不安装 Graphviz,点击链接“View Full Callgraph”,会报如下错误:
failed to execute cmd: " dot -Tpng". stderr: `Format: "png" not recognized. Use one of: canon cmap cmapx cmapx_np dot eps fig gv imap imap_np ismap pic plain plain-ext pov ps ps2 svg svgz tk vml vmlz xdot xdot1.2 xdot1.4 '
2、Graphviz 需要开启PHP的 proc_open类函数;
3、XHProf的输出目录(xhprof.output_dir=/data/xhprof) 要有写的权限,直接 777 吧!
4、比较蛋疼的一次经历,花了不少时间:
我开始给 PHP5.3 下载的是最新版的 “xhprof-0.9.4.tgz + graphviz-2.38.0.tar.gz + libpng-1.5.19.tar.gz”,编译安装之后,那个 Graphviz 老不起作用,总是报 “failed to execute cmd: " dot -Tpng".”错误,换成低版本的“xhprof-0.9.2.tgz + graphviz-2.24.0.tar.gz + libpng-1.5.19.tar.gz”,问题才解决!
5、当页面访问很慢(如,至少7-8秒才加载完),这个时候,用XHProf去分析下页面,特别管用!
参考:
http://php.net/manual/zh/book.xhprof.php
延伸阅读:
给Windows + Apache 2.2 + PHP 5.3 安装PHP性能测试工具 xhprof_0.10.3_php53_vc9.dll