PHP performance analysis tools xhprof

XHProfIt is facebookdeveloped by a phpextension for acquiring phpprogram each function of the performance overhead. Data acquisition comprising: memory consumption, CPUcomputing 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
2
3
4
5
cd xhprof-master/
cd extension/
sudo /opt/bksite/php/bin/phpize
sudo ./configure --with-php-config=/opt/bksite/php/bin/php-config --enable-xhprof
sudo make && make install

Change setting php.ini

1
2
3
[XHProf] 
Extension = xhprof.so
XHProf. output_dir storage location = / tmp / xhprof defined output file

use

Gracefully injection

At present most of the MVCframe has a unique entry documents, only need to be injected at the beginning of the file entry xhproflogic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
require_once "/tmp/xhprof-master/xhprof_lib/utils/xhprof_lib.php";
require_once "/tmp/xhprof-master/xhprof_lib/utils/xhprof_runs.php";


xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

// Register a function, when the end of program execution to execute it.
register_shutdown_function ( function () { // STOP Profiler $ xhprof_data xhprof_disable = ();



// flush (the flush) all data in response to the client IF (function_exists ( 'fastcgi_finish_request' )) { fastcgi_finish_request (); }




$xhprof_runs = new XHProfRuns_Default();

//save the run under a namespace "xhprof_foo"
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");
});

But this will inevitably have to modify the source code of the project, in fact, phpitself provides a better injection method, such as the above logic is saved /opt/bitnami/php/etc/xhprof-inject.php, and then modify phpthe configuration filephp.ini

1
auto_prepend_file = /opt/bitnami/php/etc/xhprof-inject.php

All such php-fpmrequests phpbefore the documents are automatically injected into the /opt/bitnami/php/etc/xhprof-inject.phpfile;

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-masterpackage xhprof_lib/utils/file is moved to Laravelthe root directory of the utilsfile folder;

1
cp /tmp/xhprof-master/xhprof_lib/utils/* utils/

Then, automatically loads the configuration file, the composer.jsonmodification:

1
2
3
4
5
6
7
8
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
2
3
4
5
6
7
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);

// Business code

$xhprof_data = xhprof_disable();
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_take_stock");

Web page view

Nginx Configuration; (BitNami Environment)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
listen 7777;
server_name localhost;

root /tmp/xhprof-master/xhprof_html;
index index.php index.html;

location ~* .php$ {
fastcgi_pass unix:/opt/bitnami/php/var/run/www.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
}

include "/opt/bitnami/nginx/conf/bitnami/phpfastcgi.conf";

include "/opt/bitnami/nginx/conf/bitnami/bitnami-apps-prefix.conf";
}

支持图表查看,得安装 graphviz插件;

1
2
yum install -y libpng
yum install -y graphviz

页面表格各列含义:

列名 描述
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 CPUtime (including sub-functions)
ICpu% Execution of cost function CPUof time (including sub-functions) accounted for
Excl.CPU(microsec) Function performs spent CPUtime (not including sub-functions)
ECpu% Execution of cost function CPUof 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 xhprofdata 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 xhprofdata 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的调用关系动态图

Guess you like

Origin www.cnblogs.com/wangziqiang123/p/11711102.html