PHP 性能分析工具XHProf使用

当php程序逻辑执行很复杂的时候,可能会带来性能上的问题,为了有效的找到影响性能的代码,推荐大家使用PHP新能分析工具XHProf,该工具能有效的分析每段代码的执行情况,非常好用

1、安装配置PHP的扩展XHProf
$ wget https://github.com/facebook/xhprof/tarball/master -O xhprof.tar.gz
$ tar zxf xhprof.tar.gz
$ cd facebook-xhprof-b8c76ac/extension/
# phpize
# ./configure --with-php-config=`/path/to/php-config`
# make && make install
# make test


# vi /etc/php.d/xhprof.ini
; 内容为:
extension = xhprof.so
; 注意:output_dir 必须存在且可写
xhprof.output_dir = /tmp/xhpro


然后重启apache服务
# service php-fpm restart 或 service httpd restart 

2、使用XHProf
// start profiling
xhprof_enable();

// run program
....

// stop profiler
$xhprof_data = xhprof_disable();

//
// Saving the XHProf run
// using the default implementation of iXHProfRuns.
//
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 "---------------\n".
"Assuming you have set up the http based UI for \n".
"XHProf at some address, you can view run at \n".
"http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n".
"---------------\n";

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

3、展示输出结果

在 xhprof 源码包中提供了xhprof_html 和 xhprof_lib 两个文件夹,xhprof_lib是用于 PHP 开发,而xhprof_html用于显示 xhprof 分析结果的 web 界面,访问形式如下

http://<xhprof-ui-address>/index.php?run=49bafaa3a3f66&source=xhprof_foo

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值