XHProf
It is facebook
developed by a php
extension for acquiring php
program each function of the performance overhead. Data acquisition comprising: memory consumption, CPU
computing time, and so long execution function.
download
1 |
sudo wget https://codeload.github.com/phacility/xhprof/zip/master -O xhprof.zip |
You can also from http://pecl.php.net/package/xhprof download here.
Note:
php5.4 and above can not be downloaded in pecl not supported. You need to download on GitHub https://github.com/facebook/xhprof.
In addition xhprof has not been updated for a long time, and so far does not support php7.
installation
1 |
cd xhprof-master/ |
Change setting php.ini
1 |
[XHProf] |
use
Gracefully injection
At present most of the MVC
frame has a unique entry documents, only need to be injected at the beginning of the file entry xhprof
logic
1 |
require_once "/tmp/xhprof-master/xhprof_lib/utils/xhprof_lib.php"; |
But this will inevitably have to modify the source code of the project, in fact, php
itself provides a better injection method, such as the above logic is saved /opt/bitnami/php/etc/xhprof-inject.php
, and then modify php
the configuration filephp.ini
1 |
auto_prepend_file = /opt/bitnami/php/etc/xhprof-inject.php |
All such php-fpm
requests php
before the documents are automatically injected into the /opt/bitnami/php/etc/xhprof-inject.php
file;
If Nginx, it can also Nginx profile settings, so that less invasive, and can be implemented based on the injection site.
1 |
fastcgi_param PHP_VALUE "auto_prepend_file=/opt/bitnami/php/etc/xhprof-inject.php"; |
Laravel topical project
First, the xhprof-master
package xhprof_lib/utils/
file is moved to Laravel
the root directory of the utils
file folder;
1 |
cp /tmp/xhprof-master/xhprof_lib/utils/* utils/ |
Then, automatically loads the configuration file, the composer.json
modification:
1 |
Large columns PHP profiling tool XHProf "String"> "the autoload" : { "Files" : [ ..., "utils / xhprof_lib.php" , "utils / xhprof_runs.php" ], ... } |
Execution composer dump-autoload
;
Finally, near the business code add the following code:
1 |
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY); |
Web page view
Nginx
Configuration; (BitNami Environment)
1 |
server { |
支持图表查看,得安装 graphviz
插件;
1 |
yum install -y libpng |
页面表格各列含义:
列名 | 描述 |
---|---|
Function Name |
函数名 |
Calls |
调用次数 |
Calls % |
调用次数占比 |
Incl.Wall Time(microsec) |
函数运行时间(包括子函数) |
IWall % |
函数运行时间(包括子函数)占比 |
Excl.Wall Time(microsec) |
函数运行时间(不包括子函数) |
EWall % |
Function of time (not including the sub-functions) Percentage |
Incl.CPU(microsec) |
Function performs spent CPU time (including sub-functions) |
ICpu % |
Execution of cost function CPU of time (including sub-functions) accounted for |
Excl.CPU(microsec) |
Function performs spent CPU time (not including sub-functions) |
ECpu % |
Execution of cost function CPU of time (not including sub-functions) accounted for |
Incl.MemUse(bytes) |
Function performs memory occupied (including sub-functions) |
IMemUse % |
Memory function performs occupied (including sub-functions) accounted for |
Excl.MemUse(bytes) |
Function performs memory occupied (not including sub-functions) |
EMemUse % |
Memory function performs occupied (not including sub-functions) accounted for |
Incl.PeakMemUse(bytes) |
Incl.MemUse Peak |
IPeakMemUse % |
Incl.MemUse Peak accounting |
Excl.PeakMemUse(bytes) |
Excl.MemUse Peak |
EPeakMemUse % |
Excl.MemUse Peak accounting |
xhprof data retention
After the injection, we also need to implement the code to save xhprof
data and display data UI
, sound seems to be a lot of work, there are ready-made wheels can be used?
After searching and comparison, looks like a better choice there xhprof.io and xhpgui .
Both projects have done something similar, provides xhprof
data storage function and a set of index data show UI
, here are some of the more
xhprof.io
✗ 年久失修
✗ 保存xhprof数据到MySQL
✓ 支持域名、URI等多个维度的数据索引
✓ 函数调用记录完整,内核级别函数都能显示
✗ 无法针对个别URI开启
✗ 注入被分割成两个文件,如果程序被强制中断时xhprof数据将无法收集
xhgui
✓ 保存xhprof数据到MongoDB
✗ 不支持域名索引
✗ 函数调用记录不完整,部分内核级别函数(如扩展内)无法显示
✓ 有配置文件可以控制开启条件
✓ 注入只有一个文件
✓ 狂拽酷炫的基于D3.js的调用关系动态图