[PHP]为项目引入函数级的性能监控
使用PHP扩展XHProf来实现函数级的性能监控。
引用官方对XHProf的描述:
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.
提供函数级别的性能信息,例如调用次数,其中包括阻塞时间,内存使用情况和CPU时间。
1. 安装与配置
/data/software # wget http://pecl.php.net/get/xhprof-0.9.2.tgz
/data/software # tar zxvf xhprof-0.9.2.tgz
/data/software # cd xhprof-0.9.2/
/data/software/xhprof-0.9.2 # ls -alp
总计 968
drwxr-xr-x 6 root root 4096 2013-03-18 09:09 ./
drwxr-xr-x 47 root root 8192 2013-03-18 09:09 ../
-rw-r--r-- 1 1425 users 3359 2009-06-02 01:52 CHANGELOG
-rw-r--r-- 1 1425 users 266 2009-06-02 01:52 CREDITS
drwxr-xr-x 2 root root 4096 2013-03-18 09:09 examples/ # demo
drwxr-xr-x 8 root root 4096 2013-03-18 09:09 extension/ # pecl
-rw-r--r-- 1 1425 users 10174 2009-06-02 01:52 LICENSE
-rw-r--r-- 1 1425 users 186 2009-06-02 01:52 README
-rw-r--r-- 1 root root 931660 2009-06-02 01:55 xhprof
drwxr-xr-x 6 root root 4096 2013-03-18 09:09 xhprof_html/ # 自带的界面包
drwxr-xr-x 4 root root 4096 2013-03-12 15:40 xhprof_lib/ # 函数库
/data/software/xhprof-0.9.2 # cd extension/
/data/software/xhprof-0.9.2/extension # phpize
/data/software/xhprof-0.9.2/extension # ./configure --with-php-config=/usr/local/services/php/bin/php-config --enable-xhprof
/data/software/xhprof-0.9.2/extension # make
/data/software/xhprof-0.9.2/extension # make install
Installing shared extensions: /usr/local/services/php/lib/php/extensions/no-debug-non-zts-20090626/
/data/software/xhprof-0.9.2/extension # cp /usr/local/services/php/lib/php/extensions/no-debug-non-zts-20090626/xhprof.so /usr/local/services/php/extensions/
# 将生成的xhprof.so复制到extension_dir目录下,配置php.ini,重启php-fpm或服务器即可
// php.ini
[xhprof]
extension=xhprof.so
xhprof.output_dir="/data/logs/servers/xhprof"
# 检查xhprof是否加载
/data/software/xhprof-0.9.2/extension # php -m|grep xhprof
xhprof
2.性能分析与日志记录
首先开启XHProf,以便记录十分之一的请求的性能分析日志(日志记录在/data/logs/servers/xhprof):
$xhprofEnabled = FALSE;
if(extension_loaded('xhprof') && mt_rand(1, 10) === 1) { // 分析十分之一的请求性能
// 开启性能监测
xhprof_enable(/*XHPROF_FLAGS_CPU + */XHPROF_FLAGS_MEMORY + XHPROF_FLAGS_NO_BUILTINS );
$xhprofEnabled = TRUE;
}
接着注册请求结束处理器,以便请求结束时记录下XHProf性能分析日志:
function profile_record() {
if(isset($GLOBALS['xhprofEnabled']) && $GLOBALS['xhprofEnabled']) {
$xhprof_data = xhprof_disable();
$path = $_SERVER['PHP_SELF'];
if($pos = strpos($path, '/htdocs')) {
$path = substr($path, $pos+7);
}
$basename = str_replace(array('.', '/'), '_', ltrim($path, '/'));
/* 官方Demo,请参考下载包里的xhprof-0.9.2/examples/sample.php
* //
* // 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();
*/
$xhprof_runs = new HWSL_XhProfRuns();
// 记录日志时引入时间截,方便比较不同版本间的性能差异
$run_id = $xhprof_runs->save_run($xhprof_data, 'xhprof_itravel_'.$basename.'_'.date('YmdHis'));
}
}
register_shutdown_function('profile_record');
生成的日志以文件保存在:xhprof.output_dir=”/data/logs/servers/xhprof”
生成的日志文件命名规则为:
其中命名空间
$basename记录相应的文件位置,date(‘YmdHis’)记录当前时间戳,以便比较同一文件不同版本间的性能差异。
3.日志分析
然后,通过官方提供的用户界面包xhprof-0.9.2/xhprof_html/来查看运行报告:
要查看run_id是
其中各列含义如下:
Inclusive Time (或子树执行时间) :包括子树执行时间的所有执行时间。
Exclusive Time/Self Time :函数执行本身的时间花费。不包括子树执行时间。
Wall时间 :又名经过的时间或挂钟时间。www.2cto.com
CPU时间 : CPU时间在用户空间+ CPU时间在内核空间。
也可以查看图形化的性能分析报告(其中高亮显示的路径直观反映了函数调用栈中最大的性能损耗路径):
4.扩展阅读
在生产环境中使用php性能测试工具XHProf
关于改善XHProf使用情况的设想
XHProf文件(草稿)

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

CakePHP 是一个开源MVC 框架。它使开发、部署和维护应用程序变得更加容易。 CakePHP 有许多库可以减少大多数常见任务的过载。

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储
