PHP Lightweight Performance analysis tool Xhprof installation using _php tips

Source: Internet
Author: User
Tags fpm phpinfo

First, the preface

Useful things or record it, but also convenient for later inquiries; this time, record the installation and use of xhprof;

Xhprof is a PHP lightweight performance analysis tool for Facebook, which is similar to Xdebug but has a lower performance cost

It can also be used in a production environment or by a program switch to control whether profile is performed.

Second, the 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 version of the installation of the Phpize file, can be viewed according to Phpinfo, so the PHP version is different, generate phpize is also different, this step is mainly generated configure file)
  ./configure– With-php-config=/usr/bin/php-config 
 (Php-config's path, also a file generated after PHP installation) 
  make 
  sudo make install 

(automatically copies the generated extension files to the extended directory/usr/lib64/php/modules)

Of course the specific PHP file directory, everyone is different, can be based on phpinfo query

Third, php.ini configuration

Find the Extension_dir directory according to Phpinfo
(/etc/php.d/xhprof.ini)

Add Content:

extension=xhprof.so
xhprof.output_dir=/tmp/xhprof//xhprof analysis Log

Four, restart the service

 Sudo/etc/init.d/http restart

See if Phpinfo is installed successfully

V. Methods of Use

Opening:
xhprof_enable ();//Open monitoring 
//xhprof_enable (xhprof_flags_no_builtins); Do not record built-in functions 
//xhprof_enable ( XHPROF_FLAGS_CPU + xhprof_flags_memory); Simultaneous analysis of CPU and MEM overhead 

//The code
to test ... ...
...

End:
$xhprof _data = xhprof_disable ()//Stop monitoring, return run data
$xhprof _root = '/(xhprof Virtual Host directory)/'; 
 Introduce files that were originally installed in the Xhprof virtual host directory
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's virtual host domain name)/xhprof_html/index.php?run= '. $run _id. ' &source=xhprof "target=" _blank ">xhprof statistics </a>"; 

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

Copy the xhprof_html and Xhprof_lib folders from the source pack to the virtual directory you created

Cp-r xhprof_html xhprof_lib/xxx/xhprof/(The purpose here is to create a data analysis directory that can be configured as virtual host access)

After running, statistics click Back to the XHPROF statistics link, you can.

Vi. issues of attention and interpretation of nouns

In the statistics page displayed, the point [View full CallGraph] is graphically displayed (the maximum performance problem is marked in red, followed by yellow);

After you click, you may be prompted with an error message, and execute the following command

 Yum install-y graphviz
 yum Install GRAPHVIZ-GD

noun explanation

Function name Function name
 Calls number of calls
 calls% call percent
 incl. Wall time (MICROSEC) calls include child functions all spent in microseconds (one out of 10,000 seconds)
 Iwall % of all spent time including child functions
 excl. Wall time (microsec) function to execute itself, excluding subtree execution time, in microseconds (one out of 10,000 seconds) ewall% the percentage of time spent in the
 function execution itself, Excluding subtree execution time
 incl. CPU (MICROSECS) calls include all the CPU time that is spent on the child functions. Minus incl. Wall time is excl to wait for the CPU
 . Wall time is the percentage of the waiting CPU
 icpu% incl. CPU (MICROSECS)
 excl. CPU (MICROSEC) function to perform the CPU time it spends, excluding subtree execution time, in microseconds ( One out of 10,000 seconds).
 percentage
 of ecpu% excl. CPU (MICROSEC) Incl.memuse (bytes) includes memory used by child functions.
 percentage
 of imemuse% incl.memuse (bytes) The Excl.memuse (bytes) function executes its own memory, in bytes
 ememuse% excl.memuse (bytes) percent
 incl.peakmemuse (bytes) Incl.memuse Peak
 Peak percentage
 of ipeakmemuse% incl.peakmemuse (bytes) Peak
 epeakmemuse% ememuse% Peak percentage of excl.peakmemuse (bytes) excl.memuse

Installation and simple usage of xhprof

Xhprof is Facebook's Open-source lightweight PHP profiling tool, which can be installed directly via PECL in Linux, such as 3 lines under Ubuntu

PECL Install Xhprof-beta
echo "extension=xhprof.so" >/etc/php5/fpm/conf.d/xhprof.ini
Service php5-fpm Restart

You can then check to see if the extension has been loaded by phpinfo ().

How to use it, XHPROF project has provided examples and a simple UI, download Xhprof project to the Web server, assuming you can access through http://localhost/xhprof/, then access http://localhost/xhprof /examples/sample.php can see some output, and prompts by accessing/index.php?run=xxx&source=xhprof_foo ">http:// /index.php?run= Xxx&source=xhprof_foo View the results. The next visit to http://localhost/xhprof/xhprof_html/is to see the saved results, listing the calls to all functions and the time spent.

To analyze the sample code sample.php, there are only 2 lines in the key section:

Open Xhprof and start recording
xhprof_enable ();
Run some functions
foo ();
Stop recording and fetch the result
$xhprof _data = xhprof_disable ();

$xhprof _data records all the function call time and CPU memory consumption in the process of step running, and records which indexes can be controlled by xhprof_enable entry parameters, after which the processing has nothing to do with xhprof expansion, Basically, a storage class Xhprofruns_default is written, $xhprof_data is serialized and saved to a directory that can be output to the current directory through Xhprofruns_default (__dir__). If not specified, the Xhprof.output_dir is read in the php.ini configuration file and is output to/TMP if it is still not specified.

Xhprof_html/index.php the results of the records and visualize them, which are listed in the default UI:
Funciton Name: Name of function
calls: Number of calls
incl. Wall time (MICROSEC): function runtime (including child functions)
iwall%: Function run time (including child functions) accounted for
excl. Wall time (MICROSEC): function runtime (excluding child functions)
ewall%: Function run time (excluding child functions)

Each item should be easy to understand, as an example of the sample.php of the project, a main () function is written in the example, and some child functions such as foo (), bar () are called in the main () function for a bit of character processing. The main () function runs only once throughout the program, and because the main () function includes all the logic, the main () function has a iwall% ratio of 100%, but because the function of the main () function is implemented by a child function, main () The ewall% of the function is only 0.3%, and the Foo () function completes the main work, ewall% 98.1%. Therefore, in the analysis of larger programs, often need to be sorted according to these indicators, from different angles to look at performance consumption.

You can also see the [View full CallGraph] link in xhprof_html/index.php, click to draw a visual performance analysis diagram, if the error after the click, may be a lack of dependency graphviz, Ubuntu can be installed via apt
Apt-get Install Graphviz

Better way to inject

Knowing the above, we can actually integrate Xhprof into any project we already have. Most MVC frameworks now have unique portal files that simply inject xhprof logic at the beginning of the portal file

Open Xhprof
xhprof_enable (xhprof_flags_memory | XHPROF_FLAGS_CPU);
Collects data register_shutdown_function after the program finishes
(function () {
  $xhprof _data    = xhprof_disable ();

  Let the data collector run
  if (function_exists (' fastcgi_finish_request ')) {
    fastcgi_finish_request ()
  }

  in the background; Save Xhprof Data
  ...
});

But this is unavoidable to modify the source code of the project, in fact, PHP itself provides a better way to inject, such as the above logic to save the/opt/inject.php, and then modify the PHP fpm configuration file

Vi/etc/php5/fpm/php.ini

Modify Auto_prepend_file Configuration

Auto_prepend_file =/opt/inject.php

So all PHP-FPM requested PHP files will be automatically injected with/opt/inject.php files before

If you use Nginx, you can also set it up through Nginx configuration files, which is less intrusive and enables site-based injection.

Fastcgi_param php_value "auto_prepend_file=/opt/inject.php";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.