Performance analysis of XHProf

1. Install XHProf

# page : https://pecl.php.net/package/xhprof
wget http://pecl.php.net/get/xhprof-2.2.0.tgz
tar -zxvf xhprof-2.2.0.tgz
cd xhprof-2.2.0/extension
cd xhprof-master/extension/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-xhprof
make && make install

2. Install graphivz

#Install graphviz to view performance pictures
yum -y install graphviz

3. Configure php

[xhprof]
extension=xhprof.so
# Specifies the location where the generated profile file is stored
xhprof.output_dir=/tmp

# Restart PHP service PHP FPM restart

4. Supplement required documents

Put the two directories in the folder in step 1 xhprof_html and xhprof_ Move lib to project

//Xhprof download xhprof in the compressed package_ HTML and xhprof_lib
cp -r xhprof-2.2.0/xhprof_html /usr/local/nginx/html/xhprof/
cp -r xhprof-2.2.0/xhprof_lib /usr/local/nginx/html/xhprof/

Nginx and configure the domain name to access the corresponding directory http://xhprof.alice.show

server{
     listen 80;
     server_name xhprof.alice.show;
     location / {
          root /usr/local/nginx/html/demo/xhprof;
          index index.html;
     }
     location ~ \.php$ {
          root /usr/local/nginx/html/demo/xhprof;
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include  fastcgi_params;
     }
 }

5. Test demo

xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);

#################This is used to start business logic

for ($i = 0; $i <= 10000; $i++) {
    $a = $i * $i;
}
################This is used for the end of business logic
$XHPROF_ROOT = __DIR__;
include_once __DIR__."/xhprof_lib/utils/xhprof_lib.php";
include_once __DIR__."/xhprof_lib/utils/xhprof_runs.php";
# //The data will be saved in PHP Xhprof.ini output_ Directory set by dir 
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "test");

echo "http://".$_SERVER['HTTP_HOST']."/xhprof_html/index.php?run={$run_id}&source=test\n";

6. Analysis results

Run the business code first;

Then the browser opens http://xhprof.alice.show/ , click generate xhprof file for the last time

Notice the View Full Callgraph link in the middle. Through this link, we can see the graphical analysis results

The red part in the figure is the part with low performance and long time-consuming. We can optimize the system code according to which functions are marked red

In addition, the meaning of xhprof report field is attached:

  • Function Name: method name.

  • Calls: the number of times the method was called.

  • Calls%: the percentage of method calls in the total number of method calls at the same level.

  • Incl.Wall Time(microsec): the time spent on method execution, including the execution time of sub methods. (in microseconds)

  • IWall%: the percentage of time spent executing the method.

  • Excl. Wall Time(microsec): the time spent executing the method itself, excluding the execution time of sub methods. (in microseconds)

  • EWall%: the percentage of time spent executing the method itself.

  • Incl. CPU(microsecs): CPU time spent on method execution, including execution time of sub methods. (in microseconds)

  • ICpu%: the percentage of CPU time spent executing the method.

  • Excl. CPU(microsec): the CPU time spent on the execution of the method itself, excluding the execution time of sub methods. (in microseconds)

  • ECPU%: the percentage of CPU time spent executing the method itself.

  • Incl.MemUse(bytes): memory occupied by method execution, including memory occupied by sub method execution. (in bytes)

  • Imeuse%: the percentage of memory occupied by method execution.

  • Excl.MemUse(bytes): the memory occupied by the execution of the method itself, excluding the memory occupied by the execution of sub methods. (in bytes)

  • Emeuse%: the percentage of memory occupied by the execution of the method itself.

  • Incl.PeakMemUse(bytes): peak value of Incl.MemUse. (in bytes)

  • IPeakMemUse%: peak percentage of Incl.MemUse.

  • Excl.PeakMemUse(bytes): peak value of Excl.MemUse. Unit: (bytes)

  • EPeakMemUse%: Excl.MemUse peak percentage.

The above example of using XHProf to find PHP performance bottlenecks is all that Xiaobian shared with you. I hope it can give you a reference and support Beiming fish.

Reference resources

Tags: PHP

Posted by adaykin on Wed, 25 May 2022 19:36:07 +0300