


Installation and use of php lightweight performance analysis tool xhprof_php tips
1. Foreword
It’s better to record the useful things, so as to facilitate future inquiries; this time, record the installation and use of xhprof;
It can also be used in a production environment, and the program switch can also be used to control whether to profile.
2. Installation
wget http://pecl.php.net/get/xhprof-0.9.3.tgz tar zxf xhprof-0.9.3.tgz cd xhprof-0.9.3/extension /usr/bin/phpize (php版本安装后生成的phpize文件,可根据phpinfo查看,所以php版本不同,生成的phpize也不同,此步骤主要生成configure文件) ./configure –with-php-config=/usr/bin/php-config (php-config的路径,也是php安装后生成的文件) make sudo make install
Of course, the specific directory of the php file is different for everyone. You can query it according to phpinfo
3. php.ini configuration
Find the directory of extension_dir according to phpinfo
(/etc/php.d/xhprof.ini)
extension=xhprof.so xhprof.output_dir=/tmp/xhprof //xhprof的分析日志
4. Restart the service
sudo /etc/init.d/http restart
5. How to use
开头: xhprof_enable(); //开启监测 //xhprof_enable(XHPROF_FLAGS_NO_BUILTINS); 不记录内置的函数 //xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); 同时分析CPU和Mem的开销 //要测试的代码 ... ... ... 结尾: $xhprof_data = xhprof_disable(); //停止监测,返回运行数据 $xhprof_root = '/(xhprof的虚拟主机目录)/'; //引入当初安装到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"); echo '<a href="http://(xhprof的虚拟主机域名)/xhprof_html/index.php?run='.$run_id.'&source=xhprof" target="_blank">xhprof统计</a>';
Copy the xhprof_html and xhprof_lib folders in the source package to the virtual directory you created
cp -r xhprof_html xhprof_lib /xxx/xhprof/ (The purpose here is to establish a data analysis directory, which can be configured as a virtual host for access)
After running, click the returned xhprof statistics link for statistics.
6. Attention issues and explanations of terms
In the displayed statistics page, click [View Full Callgraph] for graphical display (the biggest performance problems will be highlighted in red, followed by yellow);After clicking, an error message may be prompted. Just execute the following command
yum install -y graphviz yum install graphviz-gd
Function Name 函数名 Calls 调用次数 Calls% 调用百分比 Incl. Wall Time (microsec) 调用的包括子函数所有花费时间 以微秒算(一百万分之一秒) IWall% 调用的包括子函数所有花费时间的百分比 Excl. Wall Time (microsec) 函数执行本身花费的时间,不包括子树执行时间,以微秒算(一百万分之一秒) EWall% 函数执行本身花费的时间的百分比,不包括子树执行时间 Incl. CPU(microsecs) 调用的包括子函数所有花费的cpu时间。减Incl. Wall Time即为等待cpu的时间 减Excl. Wall Time即为等待cpu的时间 ICpu% Incl. CPU(microsecs)的百分比 Excl. CPU(microsec) 函数执行本身花费的cpu时间,不包括子树执行时间,以微秒算(一百万分之一秒)。 ECPU% Excl. CPU(microsec)的百分比 Incl.MemUse(bytes) 包括子函数执行使用的内存。 IMemUse% Incl.MemUse(bytes)的百分比 Excl.MemUse(bytes) 函数执行本身内存,以字节算 EMemUse% Excl.MemUse(bytes)的百分比 Incl.PeakMemUse(bytes) Incl.MemUse的峰值 IPeakMemUse% Incl.PeakMemUse(bytes) 的峰值百分比 Excl.PeakMemUse(bytes) Excl.MemUse的峰值 EPeakMemUse% EMemUse% 峰值百分比
Installation and simple usage of xhprof
xhprof is Facebook's open source lightweight PHP performance analysis tool. It can be installed directly through pecl in the Linux environment. For example, it only requires 3 lines of instructions under Ubuntu
pecl install xhprof-beta echo "extension=xhprof.so" > /etc/php5/fpm/conf.d/xhprof.ini service php5-fpm restart
How to use it specifically? The xhprof project has provided examples and a simple UI. Download the xhprof project to the web server. If it can be accessed through
http://localhost/xhprof/, then visit http://localhost/xhprof/examples/sample.phpYou can see some output, and you are prompted to access it by visitinghttp://
//开启xhprof并开始记录 xhprof_enable(); //运行一些函数 foo(); //停止记录并取到结果 $xhprof_data = xhprof_disable();
$xhprof_data中记录了程序单步运行过程中所有的函数调用时间及CPU内存消耗等,具体记录哪些指标可以通过xhprof_enable的入口参数控制,之后的处理已经与xhprof扩展无关,大致是编写了一个存储类XHProfRuns_Default,将$xhprof_data序列化并保存到某个目录,可以通过XHProfRuns_Default(__DIR__)将结果输出到当前目录,如果不指定则会读取php.ini配置文件中的xhprof.output_dir,仍然没有指定则会输出到/tmp。
xhprof_html/index.php将记录的结果整理并可视化,默认的UI里列出了:
•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%。因此在分析更大型的程序时,往往需要根据这几项指标分别排序,从不同的角度审视性能消耗。
在xhprof_html/index.php中还可以看到[View Full Callgraph]链接,点击后可以绘制出一张可视化的性能分析图,如果点击后报错的话,可能是缺少依赖graphviz,ubuntu可以通过apt安装
apt-get install graphviz
更好的注入方式
了解了上面这些,其实就已经可以将xhprof整合到任何我们已有的项目中去了。目前大部分MVC框架都有唯一的入口文件,只需要在入口文件的开始处注入xhprof的逻辑
//开启xhprof xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU); //在程序结束后收集数据 register_shutdown_function(function() { $xhprof_data = xhprof_disable(); //让数据收集程序在后台运行 if (function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } //保存xhprof数据 ... });
但是这样免不了要修改项目的源代码,其实php本身就提供了更好的注入方式,比如将上述逻辑保存为/opt/inject.php,然后修改php fpm配置文件
vi /etc/php5/fpm/php.ini
修改auto_prepend_file配置
auto_prepend_file = /opt/inject.php
这样所有的php-fpm请求的php文件前都会自动注入/opt/inject.php文件
如果使用Nginx的话,还可以通过Nginx的配置文件设置,这样侵入性更小,并且可以实现基于站点的注入。
fastcgi_param PHP_VALUE "auto_prepend_file=/opt/inject.php";

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
