Xhprof is an extension of testing PHP performance developed by Facebook, and this document describes how to use Xhprof for PHP performance optimization in PHP applications to find performance bottlenecks.
Installing the xhprof Extension
$ 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$./co nfigure$ make$ sudo make install
Modify PHP.ini
[Xhprof]extension=xhprof.soxhprof.output_dir=/tmp
In the configuration, Xhprof.output_dir specifies the location where the generated profile file is stored and we designate it AS/tmp.
Perform performance analysis on PHP
In the xhprof extension, a total of four functions were provided for performance analysis of PHP.
The Xhprof_enable/xhprof_sample_enable function is used to start xhprof performance analysis, except that the former is more powerful, while the latter is to start performance analysis in a simple mode (which simply records the function's call stack information), which is less expensive.
The Xhprof_disable/xhprof_sample_disable function is used to stop profiling and return the parsed data.
The function that needs to be specifically described is xhprof_enable, and other functions do not need to provide parameters, and the function can accept two optional parameters 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 macro, and if multiple values need to be provided, use | to delimit.
Xhprofflagsno_builtins Skip all built-in functions
Xhprofflagscpu Adding an analysis of CPU usage
Xhprofflagsmemory Adding an analysis of memory usage
Options Array form provides optional parameters, where the ignored_functions option is required to ignore the function
For example, the memory and CPU are analyzed at the same time, and the analysis of Call_user_func and Call_user_func_array functions is ignored.
Xhprof_enable ( xhprof_flags_memory| XHPROF_FLAGS_CPU, [ ' ignored_functions ' = [ ' Call_user_func ', ' Call_user_func_array ' ] ]);/ /Here is the code part of PHP code, such as business logic implementation, etc. to be parsed .... $xhprofData = Xhprof_disable ();//$xhprofData is an array-form analysis result Print_r ($xhprofData);
Note that if CPU usage is also analyzed using the XHPROF_FLAGS_CPU option, it will 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 memory analysis will not cause too much load on the system.
Visualize the results of a view analysis
After performing profiling using xhprof_disable and obtaining the results of the analysis, we usually do not output the results directly, as the results are organized in an array form and do not look intuitive, and fortunately, Xhprof provides a web-based graphical interface for viewing the results of the analysis.
Before you use it, make sure that the server has the Graphviz tool installed, otherwise the following error occurs when you generate the monitoring chart:
Failed to execute cmd: "Dot-tpng". stderr: ' Sh:dot:command not found '
The dot command is not found here, so you need to install Graphviz first
$ sudo yum install Graphviz
Because the viewing tools for the analysis results are web-based, we need to place the xhprofhtml and xhproflib directories in the XHPROF installation package in the server's web directory, so that xhprof_ The contents of the HTML directory can be accessed externally.
For example, my test server environment is a cent OS built using vagrant, and I've seen both directories in the /vagrant/xhprof directory:
[Vagrant@localhost xhprof]$ pwd/vagrant/xhprof[vagrant@localhost xhprof]$ lsxhprof_html xhprof_lib
The Web server is using Nginx, so the configuration in Nginx configuration file nginx.conf is as follows:
server { listen ; server_name _; Root/vagrant; ...
The root directory of the Web server is/vagrant, so the access address is http://localhost/xhprof/xhprof_html/index.php.
Of course, after we've configured the environment, we're still not getting the results of the analysis because we didn't save the results in the code to the directory specified in Xhprof.output_dir.
Therefore, we need to modify our code so that it can store the results of the analysis 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 of this request to generate the analysis result, finally we output a link address, using the change address can see the analysis result of this request.
Notice the middle view full callgraph link, through which we can see the graphical analysis results.
The red part of the graph is a relatively low-performance, time-consuming part, and we can optimize the system's code based on which functions are marked red.