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
cd xhprof-0.9.4$ cd extension/$ phpize$ ./configure$ make$ sudo make install
Modifyphp.ini
[xhprof]extension=xhprof.soxhprof.output_dir=/tmp
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.
xhprof_enable/xhprof_sample_enable
The function is used to start xhprof performance analysis, except that the former is more powerful, while the latter is to start profiling in a simple mode (simply recording the call stack information for a function), which is less expensive.
xhprof_disable/xhprof_sample_disable
The function is used to stop profiling and return the parsed data.
The function that needs to be specifically explained is xhprof_enable
that 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 for the profiling result, the value of which uses the following macro, and if multiple values need to be provided, the use |
is delimited.
XhprofFLAGSno_builtins Skip all built-in functions
XhprofFLAGSCPU added analysis of CPU usage
XhprofFLAGSMemory Add an analysis of the use of RAM
Options Array form provides optional parameters, where you can provide ignored_functions
the function to ignore
For example, the memory and CPU are analyzed at the same time, and the analysis of the pairs 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‘ ] ]);// 这里是PHP代码,比如业务逻辑实现等要被分析的代码部分....$xhprofData = xhprof_disable();// $xhprofData是数组形式的分析结果print_r($xhprofData);
Note that if you use the XHPROF_FLAGS_CPU
option to analyze CPU usage, it will result in a higher system load in a Linux environment, so it is not recommended to use, and the XHPROF_FLAGS_MEMORY
memory analysis does not cause too much load on the system.
Visualize the results of a view analysis
With the completion of profiling xhprof_disable
and obtaining the results of the analysis, we usually do not output the results directly, because the results are organized in an array, and it does not seem intuitive, fortunately, Xhprof provides a web-based graphical interface to view the results of the analysis.
Before using, make sure that the server has the graphviz
tools installed, otherwise the following error occurs when generating the monitoring chart:
failed to execute cmd: " dot -Tpng". stderr: `sh: dot: command not found ‘
The command is not found here dot
, so you need to install it firstgraphviz
$ 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:
pwd/vagrant/xhprof[[email protected] xhprof]$ lsxhprof_html xhprof_lib
The Web server is using Nginx, so the configuration in Nginx configuration file is nginx.conf
as follows:
server { listen 80; 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 to xhprof.output_dir
the specified directory in the code.
Therefore, we need to modify our code so that it can store the results of the analysis xhprof.output_dir
in the specified directory.
....$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, 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 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.
Using Xhprof to find PHP performance bottlenecks