C语言 c++ php mysql nginx linux lnmp lamp lanmp memcache redis 面试 笔记 ppt 设计模式 问题 远程连接

随笔 - 305  文章 - 1  评论 - 50  阅读 - 168万

xhprof 安装使用

 

 

1、安装扩展 

  windows下把 xhprof.dll 放到extensions目录下

修改配置文件

复制代码
[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
复制代码

 

linux下安装

复制代码
wget http://pecl.php.net/get/xhprof-0.9.2.tgz

tar zxf xhprof-0.9.2.tgz

cd xhprof-0.9.2/extension/

sudo phpize
./configure --with-php-config=/usr/local/php/bin/php-config
sudo make
sudo make install
复制代码

把生成的 xhprof.so 放到扩展的目录下,并配置记录存放的路径

 

php中增加调试代码 sample.php 文件

复制代码
function bar($x) {
  if ($x > 0) {
    bar($x - 1);
  }
}

function foo() {
  for ($idx = 0; $idx < 5; $idx++) {
    bar($idx);
    $x = strlen("abc");
  }
}

//开启调试
xhprof_enable();

// cpu:XHPROF_FLAGS_CPU 内存:XHPROF_FLAGS_MEMORY
// 如果两个一起:XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

//要测试的php代码
foo();

//停止监测
$xhprof_data = xhprof_disable();

// display raw xhprof data for the profiler run
print_r($xhprof_data);

//包含工具类,在下载的 tgz 包中可以找到
$XHPROF_ROOT = realpath(dirname(__FILE__) .'/..');
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";

// save raw data for this profiler run using default
// implementation of iXHProfRuns. 
$xhprof_runs = new XHProfRuns_Default();

// xhprof_foo 指命名空间,可以为任意字符串
$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";
复制代码

 

以表格方式查看

  访问地址:http://test.cm/xhprof/xhprof_html/index.php?run=539d612de570e&source=xhprof_foo 

run后的参数指生成的文件名, 目录再php.ini中的 xhprof.output_dir 指定

以图表方式查看

  1、安装 Graphviz  软件(windows,linux版都有)

  2、修改配置文件 config.php

 

  3、 然后点击 view full callgraph 链接即可

 

红色节点是整个php程序执行过程中的瓶颈,黄色路径为整个过程耗时最长的路径 

 

 输出结果的含义:

ct 函数调用次数,
wt 花费的时间,
cpu 花费的 CPU 时间(微秒即百万分之一秒),
mu 使用的内存(bytes),
pmu 使用的内存峰值(bytes)。

 

web 分析结果页面含义

Calls:函数的调用次数
Incl. Wall Time (microsec) :包含内部函数花费的时间,单位微秒
Excl. Wall Time (microsec):不包含内部函数花费的时间,单位微秒
及所占百分比(%)

注:Incl.:为 Including 包含的简写
Excl.:为 Excluding 不包含的简写
Wall Time:意为挂钟时间即任务花费的时间
main():一个虚构的函数,程序根节点
bar@2:递归调用 2 次
复制代码
Incl. CPU (microsecs):包含内部函数 CPU 花费的时间,单位微秒
Excl. CPU (microsec):不包含内部函数 CPU 花费的时间,单位微秒
Incl. MemUse (bytes):包含内部函数所占内存,单位字节
Excl. MemUse (bytes):不包含内部函数所占内存,单位字节
Incl. PeakMemUse (bytes):包含内部函数所占内存峰值,单位字节
Excl. PeakMemUse (bytes):不包含内部函数所占内存峰值,单位字节
及所占百分比(%)

可以认为共三种情况:
1. 包括内部函数
2. 不包括内部函数或者说函数本身
3. 所占总数(时间或内存使用)的百分比
复制代码

 

 

参考文章:http://blog.aboutc.net/profiling/17/php-profiler-xhprof

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

     软件下载(并非官方源文件)http://dev.freshsite.pl/php-extensions/xhprof.html

 

posted on   思齐_  阅读(9751)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
历史上的今天:
2013-06-16 单行、多行文字的垂直居中
< 2025年3月 >
23 24 25 26 27 28 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
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示