Installation use and precautions of PHP performance analysis tool xhprof

  • 2021-08-28 19:31:11
  • OfStack

Preface

xhprof is an PHP performance monitoring tool open source by facebook, which occupies very little resources and can even be deployed in production environment.

It can be used in conjunction with graphviz, and can intuitively show the time-consuming code execution in the form of pictures.

The following mainly talks about the installation and use process

1. Installation

(1) Download and unzip


wget http://pecl.php.net/get/xhprof-0.9.4.tgz
tar zxvf xhprof-0.9.4.tgz

(2) Compile and run


cd xhprof-0.9.4/extension/
phpize // This statement compiles PHP Extended tools, mainly according to the system information to generate the corresponding configure Documents, 1 As stored in /usr/local/php/bin/ Directory 
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
mkdir /tmp/xhprof

(3) Edit php. ini:


[xhprof]
extension = xhprof.so
xhprof.output_dir=/tmp/xhprof 

xhprof. output_dir is the save path for the analysis build log

(4) Installing plug-ins

Finally, the array is returned, which means that it is installed. Never mind what the values mean, because the configuration of UI is below. It will be very intuitive!


yum -y install libjpeg freetype freetype-devel libjpeg-devel liberation-sans-fonts.noarch

Automatic installation


yum -y install graphviz

(5) Insert code


// Find the code you want to analyze, add it at the beginning of the code, start profiling The memory usage will be counted 
xhprof_enable(XHPROF_FLAGS_MEMORY);
// Specific code 
// Add at the end of the code 
$xhprof_data = xhprof_disable(); // stop profiler, display raw xhprof data for the profiler run
include_once ("/usr/local/src/xhprof-0.9.4/xhprof_lib/utils/xhprof_lib.php"); #  Please pay attention to setting up the site  include_path  Authority 
include_once ("/usr/local/src/xhprof-0.9.4/xhprof_lib/utils/xhprof_runs.php");
$xhprof_runs = new \XHProfRuns_Default();
// Save the run under a namespace "xhprof_foo".
// **NOTE**:
// By default save_run() will automatically generate a unique
// run id for you. [You can override that behavior by passing
// a run id (optional arg) to the save_run() method instead.]
$xhprof_runs->save_run($xhprof_data, "xhprof_foo");

(6) View

Configure an accessible site for xhprof-0. 9.4/xhprof_html in (2), and use server built into php simply


cd xhprof-0.9.4/xhprof_html
php -S 0.0.0.0:8990

Then visit the ip + port to report.

2. Instructions for use

Function Name: Method name. Calls: The number of times the method was called. Calls%: The number of method calls as a percentage of the total number of method calls at the same level. Incl. Wall Time (microsec): The time taken for method execution, including the execution time of child methods. (In microseconds) IWall%: Percentage of time taken for method execution. Excl. Wall Time (microsec): The time taken to execute the method itself, excluding the execution time of child methods. (In microseconds) EWall%: Percentage of time spent executing the method itself. Incl. CPU (microsecs): The CPU time spent on method execution, including the execution time of child methods. (In microseconds) ICpu%: Percentage of CPU time spent on method execution. Excl. CPU (microsec): CPU time spent executing the method itself, excluding the execution time of child methods. (In microseconds) ECPU%: Percentage of CPU time spent executing the method itself. Incl. MemUse (bytes): Memory consumed by method execution, including memory consumed by child method execution. (In bytes) IMemUse%: Percentage of memory used for method execution. Excl. MemUse (bytes): Memory consumed by method execution, excluding memory consumed by child method execution. (In bytes) EMemUse%: Percentage of memory used by the method itself for execution. Incl. PeakMemUse (bytes): Incl. MemUse peak. (In bytes) IPeakMemUse%: Incl. MemUse peak percentage. Excl. PeakMemUse (bytes): Excl. MemUse peak. Unit: (bytes) EPeakMemUse%: Excl. MemUse peak percentage.

Note:

1. Before it is officially activated, 1 must confirm that it will not affect the normal data output. Confirm that the output content is the same, and then go online.

2. Do not set max_time of each url too small.

3. xhprof will affect the performance of online services, so it is best to monitor on only one machine, or modify xhprof. php code to randomly monitor requests.

Summarize


Related articles: