Use XHProf to find PHP performance bottlenecks

Source: Internet
Author: User
Using XHProf to find PHP performance bottlenecks XHProf is an extension developed by facebook to test php performance. This article records how to use XHProf to optimize PHP performance and find performance bottlenecks in PHP applications.

Install 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$ ./configure$ make$ sudo make install

Modify php. ini

[xhprof]extension=xhprof.soxhprof.output_dir=/tmp

In the configuration, xhprof. output_dir specifies the location of the generated profile file storage. we will specify it as/tmp.

Performance analysis of PHP

In the XHProf extension, four functions are provided for PHP performance analysis.

The xhprof_enable/xhprof_sample_enable function is used to start XHProf performance analysis. The difference is that the former is more powerful, while the latter is to start performance analysis in simple mode (the call stack information of the function is simply recorded ), low overhead.

The xhprof_disable/xhprof_sample_disable function is used to stop performance analysis and return the analyzed data.

The xhprof_enable function is used to change the behavior of the tool. Other functions do not need to provide parameters. However, this function can accept two optional parameters.

void xhprof_enable ([ int $flags = 0 [, array $options ]] )
  • FlagsThis parameter is used to add additional information for the analysis results. The values of this parameter are separated by the following macros. if you need to provide multiple values, use the |.

    • XHPROFFLAGSNO_BUILTINS skips all built-in functions

    • XHPROFFLAGSCPU addition analysis of CPU usage

    • XHPROFFLAGSMEMORY add memory usage analysis

  • OptionsOptional parameters are provided in the array form. functions to be ignored are provided in the ignored_functions option.

For example, in the following 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 ']); // PHP code is used here, for example, the business logic implementation and other code to be analyzed .... $ xhprofData = xhprof_disable (); // $ xhprofData is an analysis result in the form of arrays print_r ($ xhprofData );

Note: If you use the XHPROF_FLAGS_CPU option to analyze CPU usage, it will cause high system load in Linux. Therefore, it is not recommended that you use XHPROF_FLAGS_MEMORY, memory analysis does not cause too much load on the system.

Visually view analysis results

After xhprof_disable is used to perform performance analysis and obtain the analysis results, we usually do not directly output the results, because the results are organized in arrays and appear not intuitive. Fortunately, xhprof provides a web-based graphical interface to view the analysis results.

Before using graphviz, make sure that the graphviz tool is installed on the server. Otherwise, the following error occurs when a monitoring chart is generated:

failed to execute cmd: " dot -Tpng". stderr: `sh: dot: command not found '

The dot command is not found, so you need to install graphviz first.

$ sudo yum install graphviz

Because the analysis result viewing tool is web-based, we needXhprofhtmlAndXhproflibDirectory to the web Directory of the server, so that the content in the xhprof_html directory can be accessed externally.

For example, in my testing server environment, Cent OS is built using vagrant. I have seen these two directories put in/Vagrant/xhprofDirectory:

[vagrant@localhost xhprof]$ pwd/vagrant/xhprof[vagrant@localhost xhprof]$ lsxhprof_html  xhprof_lib

The web server uses Nginx. Therefore, the configuration in the Nginx configuration file nginx. conf is as follows:

server {    listen       80;    server_name  _;    root /vagrant;    ...

The root directory of the web server is/vagrant. Therefore, the access address is http: // localhost/xhprof/xhprof_html/index. php.

Of course, after the environment is configured, we still cannot obtain the analysis results, because the analysis results are not saved to the directory specified by xhprof. output_dir in the code.

Therefore, we need to modify our code 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 of the analysis result generated for this request. at last, we output a link address. you can use the address to view the analysis result of this request.

The View Full Callgraph link in the middle shows the graphic analysis result.

In the figure, the red part is a relatively low-performance and time-consuming part. we can optimize the system code based on which functions are marked as 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.