使用XHProf进行性能测试

XHProf是一种轻量级的php性能分析工具,以下是pecl上的描述:

XHProf is a function-level hierarchical profiler for PHP and has a simple HTML based navigational interface. The raw data collection component is implemented in C (as a PHP extension). The reporting/UI layer is all in PHP. It is capable of reporting function-level inclusive and exclusive wall times, memory usage, CPU times and number of calls for each function. Additionally, it supports ability to compare two runs (hierarchical DIFF reports), or aggregate results from multiple runs.

目前XHProf 已更新至版本0.9.4


下载和安装

wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar zxf xhprof-0.9.2.tgz
cd xhprof-0.9.2/extension/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

最后生成的扩展位置:/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

 

配置 php.ini

[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=<directory_for_storing_xhprof_runs>
xhprof.output_dir=/tmp/xhprof

(要注意修改该文件夹的读写权限,否则可能无法生成数据)

 

重启php

/usr/local/php/sbin/php-fpm restart

 

安装Graphviz

wgethttp://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

(这里不安装也可以,但结果只能表格显示,不能通过图表显示,

当点击页面中的[View Full Callgraph]会提示Error: either we can not find profile data for run_id 4d7f0bd99a12for the threshold 0.01 is too small or you do not have ‘dot’ image generationutility installed.

因为不能生成png图片)

 

使用XHProf

将xhprof_html和xhprof_lib拷贝到目标文件夹下

cp -r xhprof_html xhprof_lib <directory_for_htdocs>

将代码加入到要测试的php当中

<?php
// cpu:XHPROF_FLAGS_CPU 内存:XHPROF_FLAGS_MEMORY
// 如果两个一起:XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
 
// 要测试的php代码
 
 
$data = xhprof_disable();   //返回运行数据
 
// xhprof_lib在下载的包里存在这个目录,记得将目录包含到运行的php代码中
include_once "xhprof_lib/utils/xhprof_lib.php"; 
include_once "xhprof_lib/utils/xhprof_runs.php"; 
 
$objXhprofRun = new XHProfRuns_Default();
 
// 第一个参数j是xhprof_disable()函数返回的运行信息
// 第二个参数是自定义的命名空间字符串(任意字符串),
// 返回运行ID,用这个ID查看相关的运行结果
$run_id = $objXhprofRun->save_run($data, "xhprof");
var_dump($run_id);

这是一个测试的例子:

<?php
function bar($x) {
  if ($x > 0) {
    bar($x - 1);
  }
}
 
function foo() {
  for ($idx = 0; $idx < 5;$idx++) {
    bar($idx);
    $x =strlen("abc");
  }
}
 
// start profiling
xhprof_enable();
 
// run program
foo();
 
// stop profiler
$xhprof_data = xhprof_disable();
 
include_once "xhprof_lib/utils/xhprof_lib.php";
include_once "xhprof_lib/utils/xhprof_runs.php";
 
// save raw data for this profiler run using default
// implementation of iXHProfRuns.
$xhprof_runs = new XHProfRuns_Default();
 
// save the run under a namespace "xhprof_foo"
$run_id = $xhprof_runs->save_run($xhprof_data,"xhprof_foo");
 
echo "---------------\n".
     "you can view run at\n".
     "http://<xhprof-ui-address>/xhprof_html/index.php?run=$run_id&source=xhprof_foo\n".
    "---------------\n";
?>

通过网页查看结果,例如id=52f8a45843974的url为:

http:// test.ifeng.com/xhprof_html/index.php?run=52f8a45843974&source=xhprof_foo

下面是一些参数说明

Inclusive Time                包括子函数所有执行时间。

Exclusive Time/Self Time  函数执行本身花费的时间,不包括子树执行时间。

Wall Time                       花去了的时间或挂钟时间。

CPU Time                       用户耗的时间+内核耗的时间

Inclusive CPU                 包括子函数一起所占用的CPU

Exclusive CPU                 函数自身所占用的CPU

上例的测试结果:

Overall Summary
Total Incl. Wall Time (microsec):176 microsecs
Number of Function Calls:23


点击[View Full Callgraph]可查看图表格式的测试结果:


好吧今天暂时到这。


参考文献:

http://www.cnblogs.com/bluefrog/archive/2012/03/01/2374922.html

https://github.com/facebook/xhprof

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值