Home >Backend Development >PHP Tutorial >Installation and use of php lightweight performance analysis tool xhprof_php tips

Installation and use of php lightweight performance analysis tool xhprof_php tips

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 20:08:341216browse

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 
(The generated extension file will be automatically copied to the extension directory /usr/lib64/php/modules)

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)

Add some content:

extension=xhprof.so
xhprof.output_dir=/tmp/xhprof //xhprof的分析日志

4. Restart the service

 sudo /etc/init.d/http restart

Check if phpinfo is installed successfully

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&#63;run='.$run_id.'&source=xhprof" target="_blank">xhprof统计</a>'; 

The above code uses the method of setting a virtual host for xhprof.

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

Explanation of terms

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
You can later check whether the extension has been loaded through phpinfo().

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://3150ebf02b54f746533a622e2a0f4d32/index.php?run=XXX&source =xhprof_fooView the results. Next, visit http://localhost/xhprof/xhprof_html/ to see the saved results, listing all function calls and the time spent.

Analyze the sample code sample.php, the key part is only 2 lines:


//开启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";

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn