PHP Performance analysis tool xhprof installation configuration usage Tutorial

Source: Internet
Author: User
Tags memory usage php code cpu usage

Xhprof is an extension of the test PHP performance developed by Facebook.

Installing XHPROF Extensions

$ wget http://pecl.php.net/get/xhprof-0.9.4.tgz
$ TAR-ZXVF xhprof-0.9.4.tgz
$ CD xhprof-0.9.4
$ CD extension/
$ phpize
$./configure
$ make
$ sudo make install

Modify PHP.ini

[Xhprof]
Extension=xhprof.so
Xhprof.output_dir=/tmp

The configuration xhprof.output_dir specifies the location of the generated profile file storage, which we designate AS/tmp.
Perform performance analysis on PHP
In the xhprof extension, four functions are provided for profiling PHP.
The Xhprof_enable/xhprof_sample_enable function is used to start the XHPROF performance analysis, the difference being that the former is more powerful, while the latter is a simple mode to start performance analysis (simple record of the function call stack information), the overhead is relatively small.
The Xhprof_disable/xhprof_sample_disable function is used to stop profiling and return the parsed data.
Functions that need to be specifically described are xhprof_enable, and other functions do not need to provide arguments, and the function can accept two optional arguments to change the behavior of the tool.
void xhprof_enable ([int $flags = 0 [, array $options]])

Flags This parameter is used to add additional information to the profiling result, the value of which uses the following macros, which are delimited if multiple values need to be supplied.
Xhprof_flags_no_builtins skip all the built-in functions
XHPROF_FLAGS_CPU Add to CPU usage analysis
Xhprof_flags_memory Add an analysis of memory usage
The options array provides optional arguments here to provide a function that the ignored_functions option needs to ignore
For example, the following example, the memory and CPU analysis, and ignore the Call_user_func and Call_user_func_array function analysis.
Xhprof_enable (
xhprof_flags_memory| XHPROF_FLAGS_CPU,
[
' Ignored_functions ' => [
' Call_user_func ',
' Call_user_func_array '
]
]
);

Here is the code part of the PHP code, such as business logic implementation, to be analyzed
....

$xhprofData = Xhprof_disable ();//$xhprofData is the result of an array form
Print_r ($xhprofData);

Note that if CPU usage is analyzed using the XHPROF_FLAGS_CPU option, it can result in a higher system load in a Linux environment, so it is not recommended, and it is recommended to use only xhprof_flags_memory, and the analysis of the memory does not cause too much load on the system.
Visualize and view the results of the analysis
After performing performance analysis using xhprof_disable and obtaining the analysis results, we usually do not output the results directly because the results are organized in an array format and are not visually intuitive, fortunately, Xhprof provides a web-based graphical interface to view the results of the analysis.
Before you use it, make sure the server has the Graphviz tool installed, or the following error occurs when you generate the monitor chart:
Failed to execute cmd: "Dot-tpng". stderr: ' Sh:dot:command not found '

This prompts you not to find the dot command, so you need to install Graphviz first

$ sudo yum install Graphviz

Because the viewing tool for profiling results is web-based, we need to place the xhprof_html and Xhprof_lib directories in the XHPROF installation package in the server's web directory so that the contents of the Xhprof_html directory are accessible to the outside world.
For example, my test server environment is a cent OS built using vagrant, and I've seen both directories placed in the/vagrant/xhprof directory:

[Vagrant@localhost xhprof]$ pwd
/vagrant/xhprof
[Vagrant@localhost xhprof]$ ls
Xhprof_html Xhprof_lib

The Web server uses Nginx, so the configuration file nginx.conf in Nginx is as follows:
server {
Listen 80;
server_name _;
Root/vagrant;
...

The Web server's root directory is/vagrant, so the access address is http://localhost/xhprof/xhprof_html/index.php.
Of course, after the environment is configured, we still don't get the results of the analysis because we don't save the analysis results in the code to the directory specified by Xhprof.output_dir.
Therefore, we need to modify our code to be able to store the analysis results in the directory specified by Xhprof.output_dir.
....
$xhprofData = Xhprof_disable ();
Require '/vagrant/xhprof/xhprof_lib/utils/xhprof_lib.php ';
Require '/vagrant/xhprof/xhprof_lib/utils/xhprof_runs.php ';

$xhprofRuns = new Xhprofruns_default ();
$runId = $xhprofRuns->save_run ($xhprofData, ' xhprof_test ');

Echo ' http://localhost/xhprof/xhprof_html/index.php?run= '. $runId. ' &source=xhprof_test ';

The variable $runId is the ID that generated the analysis result for this request, finally we output a link address, we can use the change address to see the analysis result of this request.
Notice the link in the middle View Full Callgraph , through which we can see the graphical results of the analysis.
The red section of the diagram is a relatively low, time-consuming part, and we can optimize the code for the system based on which functions are marked red

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.