PHP性能监测的工具介绍 - XHProf -参考自https://jingyan.baidu.com/article/7082dc1c173359e40a89bd95.html
Posted on 2018-03-08 14:14 懒人ABC 阅读(910) 评论(6) 编辑 收藏 举报XHProf
这个软件本是Facebook内部的一个应用工具,2009年3月份开源,为PHP的性能监测提供了很好的工具。官方的介绍中提到:
方法/步骤
-
XHProf
这个软件本是Facebook内部的一个应用工具,2009年3月份开源,为PHP的性能监测提供了很好的工具。官方的介绍中提到:
XHProf is a hierarchical profiler for PHP. It reports function-level call counts and inclusive and exclusive metrics such as wall (elapsed) time, CPU time and memory usage.
XHProf's light-weight nature and aggregation capabilities make it well suited for collecting "function-level" performance statistics from production environments.
可以先来看看 XHProf 提供的图形界面的截图
XHProf的一些特性:
1、Flat Profile. 提供函数级的汇总信息,比如调用次数、执行时间、内存使用、CPU占用等。
-
2、Hierarchical Profile。 对每个程序,进行了父级调用和子级调用的分解。
3、Diff Reports(差异报告)。有很多种情况,我们希望能够对比,比如新版本比旧版本提升了多少速度,两个版本的差距究竟在哪里。Diff Report 就是这样的工具,接收两个输入,并且分别给出各自的 Flat Profile 和 Hierarchical Profile 报告。
4、Callgraph View(调用视图)。性能监测的数据可以绘制成调用视图,方便我们查看。
-
5、Memory Profile(内存监控)。这个特性帮助我们了解PHP如何分配和释放内存。值得注意的是,XHProf并不是严格的监测内存的分配和释放动作,而是计算每个函数进入和退出时的内存状况,这是一个相对简单的实现方式,但是基本上也能够满足我们日常的监控需求。
6、如何处理外部文件。XHProf将 include,require,include_once,require_once进来的文件视作是一个 function。
XHProf目前只支持一个级别的函数追踪,但是貌似也没有特别大的影响。
XHProf的安装配置
-
xhprof的安装配置很简单,我们首先在 PECL 的网站上下载 源码包 然后执行安装过程
% cd <xhprof_source_directory>/extension/ % phpize % ./configure --with-php-config=<path to php-config> % make % make install % make test
php.ini file: You can update your php.ini file to automatically load your extension. Add the following to your php.ini file.
[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的使用也很简单,只要将需要监控的脚本放在 xhprof_enable() 和 xhprof_disable() 中间,就可以得到相应的结果,同时也提供了一些参数可以让我们设置是否监控 Memory, CPU 的使用,是否监控PHP内置的函数,从 0.9.2 之后,还可以设置跳过一些特定的函数。
XHProf 生成的数据,可以用 XHProf UI 来进行简单的显示。
XHProf使用也很简单,下面是一个官方的例子:
-
<?php
function bar($x) { if ($x > 0) { bar($x - 1); } }
function foo() { for ($idx = 0; $idx < 2; $idx++) { bar($idx); $x = strlen("abc"); } }
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
foo();
$xhprof_data = xhprof_disable();
// // Saving the XHProf run // using the default implementation of iXHProfRuns. // include_once "xhprof_lib/utils/xhprof_lib.php"; include_once "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测试结果各参数的解释:
- funciton name : 函数名
- calls: 调用次数
- Incl. Wall Time (microsec): 函数运行时间(包括子函数)
- IWall%:函数运行时间(包括子函数)占比
- Excl. Wall Time(microsec):函数运行时间(不包括子函数)
- EWall%:函数运行时间(不包括子函数)
每一项应该不难理解,以项目自带的
sample.php
为例,示例中编写了一个main()
函数,main()
函数中调用foo()
、bar()
等一些子函数进行了一点字符处理。整个程序运行过程中,main()
函数只运行了一次,并且由于main()
函数中包括了所有的逻辑,所以main()
函数的IWall%占比为100%,但是由于main()
函数的功能都是由子函数实现的,因此main()
函数的EWall%只有0.3%,而foo()
函数完成了主要的工作,EWall%有98.1%。因此在分析更大型的程序时,往往需要根据这几项指标分别排序,从不同的角度审视性能消耗。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
2009-03-08 Web开发中的Drag&Drop完全手册--http://www.cnblogs.com/birdshome/archive/2006/07/22/457369.aspx
2009-03-08 CSS中position的absolute和relative的应用--转载http://www.awflasher.com/blog/archives/731
2009-03-08 当窗口大小变化时,始终让底图居中--转载http://topic.csdn.net/t/20060824/16/4973302.html