PHP性能测试利器 xhprof

本文详细介绍了如何在PHP环境中安装并配置XHProf,用于性能分析。包括下载、编译、配置PHP.ini,安装Graphviz以及如何使用XHProf进行代码性能跟踪与报告生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,安装XHProf:

1
2
3
4
5
6
7
8
9
10
11
12
wget http: //pecl .php.net /get/xhprof-0 .9.2.tgz
tar zxf xhprof-0.9.2.tgz
cd xhprof-0.9.2
mkdir -p /web/xhprof/output
cp -r xhprof_html xhprof_lib /web/xhprof #你的网站根目录下的xhprof目录
chown -R web.web /web/xhprof
cd extension
phpize
. /configure
make
make install
make test #可以看一下是否通过测试


编辑 /etc/php.ini:

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

重启PHP服务(/etc/init.d/php-fpm restart)让修改生效,现在就可以使用XHProf了,不过为了显示效果更炫,最好继续安装Graphviz。

安装libpng和Graphviz:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#必须编译安装,不要yum install libpng
wget http: //sourceforge .net /projects/libpng/files/libpng15/1 .5.11 /libpng-1 .5.11. tar .gz /download
tar zxf libpng-1.5.11. tar .gz
cd libpng-1.5.11
. /configure
make
make install
 
wget http: //www .graphviz.org /pub/graphviz/stable/SOURCES/graphviz-2 .24.0. tar .gz
tar zxf graphviz-2.24.0. tar .gz
cd graphviz-2.24.0
. /configure
make
make install
dot -V #确认是否可以使用dot程序

安装完成后,会生成/usr/local/bin/dot文件,你应该确保路径在PATH环境变量里,以便XHProf能找到它。

使用XHProf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// start profiling
xhprof_enable();
 
// run program
sleep(1);
 
// stop profiler
$xhprof_data = xhprof_disable();
 
//
// Saving the XHProf run
// using the default implementation of iXHProfRuns.
//
$XHPROF_ROOT = "/web/xhprof" ;
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php" ;
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php" ;
 
$xhprof_runs = new XHProfRuns_Default();
 
// Save the run under a namespace "xhprof_foo".
//
// **NOTE**:
// By default save_run() will automatically generate a unique
// run id for you. [You can override that behavior by passing
// a run id (optional arg) to the save_run() method instead.]
//
$run_id = $xhprof_runs ->save_run( $xhprof_data , "xhprof_foo" );
 
echo "<a href='/xhprof/xhprof_html/index.php?run=$run_id&source=xhprof_foo'>[display xhprof report]</a>" ;

如此一来,会在上面设定的xhprof.output_dir目录里生成名字类似49bafaa3a3f66.xhprof_foo的数据文件,可以很方便的通过Web方式浏览效果。

你应该看到:

Function NameCallsCalls%Incl. Wall Time
(microsec)
IWall%Excl. Wall Time
(microsec)
EWall%
main()133.3%1,000,135100.0%400.0%
sleep133.3%1,000,094100.0%1,000,094100.0%
xhprof_disable133.3%10.0%10.0%

点击[View Full Callgraph]就可以看到图片:


【全文完】

转载于:https://my.oschina.net/alexwu/blog/71065

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值