简介

XHProf 是一个轻量级的分层性能测量分析器。 在数据收集阶段,它跟踪调用次数与测量数据,展示程序动态调用的弧线图。 它在报告、后期处理阶段计算了独占的性能度量,例如运行经过的时间、CPU 计算时间和内存开销。 函数性能报告可以由调用者和被调用者终止。 在数据搜集阶段 XHProf 通过调用图的循环来检测递归函数,通过赋予唯一的深度名称来避免递归调用的循环。

XHProf 包含了一个基于 HTML 的简单用户界面(由 PHP 写成)。 基于浏览器的用户界面使得浏览、分享性能数据结果更加简单方便。 同时也支持查看调用图。

XHProf 的报告对理解代码执行结构常常很有帮助。 比如此分层报告可用于确定在哪个调用链里调用了某个函数。

XHProf 对两次运行进行比较(又名 “diff” 报告),或者多次运行数据的合计。 对比、合并报告,很像针对单次运行的“平式视图”性能报告,就像“分层式视图”的性能报告。

安装/配置

编译安装

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

修改php.ini文件

[xhprof]
extension=xhprof.so
xhprof.output_dir=/var/log/xhprof

以上完成后重启apache,可以看到xhprof扩展

最后写个测试检验一下

//test.php
<?php
xhprof_enable();
strlen('hello world');
$xhprof_data = xhprof_disable();
var_dump($xhprof_data);
?>

返回结果(为了减少篇幅长度,这里做了格式化)

array(3) {
["main()==>strlen"]=>array(2) {
["ct"]=> int(1)
["wt"]=> int(4)
}
["main()==>xhprof_disable"]=>array(2) {
["ct"]=> int(1)
["wt"]=> int(1)
}
["main()"]=>array(2) {
["ct"]=> int(1)
["wt"]=> int(20)
}
}

预定义常量

XHPROF_FLAGS_NO_BUILTINS (integer) | 使得跳过所有内置(内部)函数。

XHPROF_FLAGS_CPU (integer) | 使输出的性能数据中添加 CPU 数据。

XHPROF_FLAGS_MEMORY (integer) | 使输出的性能数据中添加内存数据。

可视化

xhprof 为我们提供了可视化的工具,将tar包里面的xhprof_lib、xhprof_html复制到站点目录下即可

cp -r xhprof_html/ xhprof_lib/ /usr/local/httpd-2.4.7/htdocs/xhprof

写一个测试脚本。把结果输出到可视化界面上

<?php
xhprof_enable();
for ($i = 0; $i <= 1000; $i++) {
$a = $i * $i;
}
$xhprof_data = xhprof_disable();
$XHPROF_ROOT = "/www/project/merchant.wbaipay.com/html/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();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing"); echo "http://localhost/xhprof/xhprof_html/index.php?run={$run_id}&source=xhprof_testing\n";
?>

打开链接,可以查看效果

在需要调试的地方加如header.php

php_admin_value auto_prepend_file /var/www/xhprof/external/header.php

header.php

<?php
if (extension_loaded('xhprof') )
{
xhprof_enable(); register_shutdown_function(function(){
$xhprof_data = xhprof_disable();
$XHPROF_ROOT = __DIR__.'/..';
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();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
echo "http://192.168.41.121:93/?run={$run_id}&source=xhprof_testing\n";
});
}else{
error_log('xhgui - either extension xhprof or uprofiler must be loaded');
}

安装Graphviz

wget "http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.26.3.tar.gz"
tar zxf graphviz-2.26.3.tar.gz
cd graphviz-2.26.3
./configure --prefix=/usr
make && make install

最新文章

  1. 图解DataGridView编辑列
  2. .NET组件控件实例编程系列——5.DataGridView数值列和日期列
  3. caffe初步实践---------使用训练好的模型完成语义分割任务
  4. 第五天 loadmore
  5. 地址(Address)——统一资源表示(URI)——WCF学习笔记(2)
  6. .net加载到vb 进程
  7. 通过HTML条件注释判断IE版本的HTML语句详解&lt;!--[if IE]&gt; &lt;![endif]--&gt;
  8. 关于 Log4Net
  9. LibVLC video controls
  10. DUMP 3.8 企业级电商项目 支付宝之类
  11. 预制体,Mask组件
  12. MSSQL Server2012备份所有数据库到网络共享盘上面,并自动删除几天前的备份。。
  13. Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval o
  14. Linux下C语言的文件操作
  15. 设计模式之观察者模式(c++)
  16. Java基础【基本数据类型包装类、int与String 之间的相互转换】
  17. Mp4 to Img
  18. h5的部分新元素或属性
  19. 获取用户真实的IP
  20. 【ML系列】简单的二元分类——Logistic回归

热门文章

  1. Traffic Steering for Service Function Chaining
  2. 用P4对数据平面进行编程
  3. lintcode-491-回文数
  4. zabbix简介
  5. Halcon 笔记2 Blob分析
  6. PHP 多维数组排序 array_multisort()
  7. hbase windows安装
  8. BZOJ 2131 圈地计划(最小割+黑白染色)
  9. [codeforces696B]Puzzles
  10. 笔记-自己看Day20-待续