Php lightweight performance analysis tool xhprof installation and use _ php skills

Source: Internet
Author: User
I have been using Xdebug-based PHP for performance analysis. it is enough for the local development environment. However, if it is an online environment, xdebug consumes a lot and the configuration is not flexible enough, therefore, xhprof is recommended for tracking and analyzing PHP performance in the online environment. I. Preface

Record the useful items and facilitate future queries. this time, record the installation and use of xhprof;

Xhprof is a lightweight php performance analysis tool open-source by facebook. it is similar to Xdebug, but has lower performance overhead,

It can also be used in the production environment, or it can be enabled by the program to control whether the profile is performed.

II. 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 (phpize file generated after php version installation, can be viewed according to phpinfo, so php version is different, the generated phpize is also different. this step mainly generates the configure file ). /configure-with-php-config =/usr/bin/php-config (php-config path, also the file generated after php installation) make sudo make install

(The generated extension file is automatically copied to the extension directory/usr/lib64/php/modules)

Of course, the directories of the specific PHP files vary with each other. you can query the files by phpinfo.

III. php. ini configuration

Find the extension_dir directory based on phpinfo
(/Etc/php. d/xhprof. ini)

Add the following content:

Extension = xhprof. soxhprof. output_dir =/tmp/xhprof // xhprof analysis log

4. restart the service

 sudo /etc/init.d/http restart

Check whether phpinfo is successfully installed.

5. usage

Start with xhprof_enable (); // enable monitoring // xhprof_enable (enabled); do not record built-in functions // xhprof_enable (XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY ); analyze CPU and Mem overhead at the same time // code to be tested ......... end: $ xhprof_data = xhprof_disable (); // stop monitoring and return running data $ xhprof_root = '/(xhprof's VM directory )/'; // introduce the file include_once $ xhprof_root that was originally installed in the xhprof virtual host directory. "xhprof_lib/utils/xhprof_lib.php"; include_once $ xhprof_root. "xhprof_lib/utils/issue"; $ xhprof_runs = new release (); $ run_id = $ xhprof_runs-> save_run ($ xhprof_data, "xhprof"); echo 'xhprof Statistics ';

The above code is used to set the virtual host for xhprof.

Copy the xhprof_html and xhprof_lib folders in the source code package to your own virtual directory.

Cp-r xhprof_html xhprof_lib/xxx/xhprof)

After running, click the returned xhprof statistics link.

VI. notes and glossary

On the displayed Statistics page, click [View Full Callgraph] for graphical display (the biggest performance problem is marked in red, followed by yellow );

After you click it, an error message may be displayed. run the following command.

 yum install -y graphviz yum install graphviz-gd

Glossary

Function Name: number of Calls to the CILS % percentage of Calls to the Incl. wall Time (microsec) calls include the percentage of Time consumed by all sub-functions in microseconds (1 million in one second) IWall % calls including all sub-functions Excl. the Time consumed by the Wall Time (microsec) function execution, excluding the sub-tree execution Time. the percentage of Time consumed by the function execution in microseconds (1 million in one second) EWall % is calculated, the execution time of the subtree is not included in Incl. the CPU (microsecs) calls the cpu time of all sub-functions. Subtract Incl. wall Time is the Time to wait for the cpu to subtract Excl. the Wall Time is the wait Time for the cpu ICpu % Incl. the percentage of CPU (microsecs) Excl. the CPU time consumed by the execution of the cpu (microsec) function, excluding the sub-tree execution time, measured in microseconds (one thousandth second ). The percentage of ECPU % Excl. CPU (microsec) Incl. MemUse (bytes) includes the memory used for sub-function execution. IMemUse % Incl. percentage of MemUse (bytes) Excl. memUse (bytes) function execution memory, in bytes calculated as EMemUse % Excl. percentage of MemUse (bytes) Incl. peakMemUse (bytes) Incl. memUse peak IPeakMemUse % Incl. peakMemUse (bytes) peak percentage Excl. peakMemUse (bytes) Excl. memUse peak EPeakMemUse % EMemUse % peak percentage

Installation and simple usage of xhprof

Xhprof is Facebook's open-source lightweight PHP performance analysis tool. it can be directly installed using pecl in Linux. for example, in Ubuntu, only three lines of commands are required.

pecl install xhprof-betaecho "extension=xhprof.so" > /etc/php5/fpm/conf.d/xhprof.iniservice php5-fpm restart

Then, you can use phpinfo () to check whether the extension has been loaded.

How to use it? the xhprof project has provided examples and a simple UI to download the xhprof project to the web server. assume that the project can be accessed through http: // localhost/xhprof, access http: // localhost/xhprof/examples/sample. php can see some output and prompt to access http :// /Index. php? Run = XXX & source = xhprof_foo. Next, visit http: // localhost/xhprof/xhprof_html/to view the saved results and list all function calls and consumed time.

Analyze the sample code sample. php. The key part is only two lines:

// Enable xhprof and start recording xhprof_enable (); // run some functions foo (); // stop the record and obtain the result $ xhprof_data = xhprof_disable ();

$ Xhprof_data records all the function call times and CPU memory consumption during the single-step operation of the program. it records which indicators can be controlled through the xhprof_enable entry parameters, the subsequent processing has nothing to do with the xhprof extension. it is roughly to write a storage class XHProfRuns_Default, serialize $ xhprof_data and save it to a directory. you can use XHProfRuns_Default (_ DIR __) output the result to the current directory. If this parameter is not specified, php is read. xhprof. output_dir, which is output to/tmp if not specified.

Xhprof_html/index. php sorts and visualizes the record results, which are listed in the default UI:
• Funciton name: function name
• Cballs: number of calls
• Incl. Wall Time (microsec): function runtime (including subfunctions)
• IWall %: Percentage of function running time (including sub-functions)
• Excl. Wall Time (microsec): Function running Time (excluding subfunctions)
• EWall %: Function running time (excluding subfunctions)

Each item should not be hard to understand. php is used as an example. In this example, a main () function is compiled. some subfunctions, such as foo () and bar (), are called in the main () function to process some characters. During the entire program running, the main () function only runs once, and because the main () function contains all the logic, the IWall % percentage of the main () function is 100%, however, because the functions of the main () function are implemented by subfunctions, the EWall % of the main () function is only 0.3%, and the foo () function completes the main work, EWall % has 98.1%. Therefore, when analyzing larger programs, you often need to sort these indicators separately to view performance consumption from different perspectives.

In xhprof_html/index. in php, you can also see the [View Full Callgraph] link. after clicking it, you can draw a Visual Performance Analysis Diagram. If an error is reported after clicking it, the dependency graphviz may be missing, and ubuntu can be installed through apt.
Apt-get install graphviz

Better injection methods

After learning about the above, we can integrate xhprof into any of our existing projects. Currently, most MVC frameworks have unique entry files. you only need to inject xhprof logic at the beginning of the entry file.

// Enable xhprofxhprof_enable (XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU); // collect data after the program ends register_shutdown_function (function () {$ xhprof_data = xhprof_disable (); // run the data collection program in the background if (function_exists ('fastcgi _ finish_request ') {fastcgi_finish_request ();} // save xhprof data ...});

However, php itself provides a better injection method, for example, saving the above logic as/opt/inject. php, and then modifying the php fpm configuration file.

vi /etc/php5/fpm/php.ini

Modify auto_prepend_file configuration

auto_prepend_file = /opt/inject.php

In this way, all php-fpm requests will automatically inject the/opt/inject. php file before the php file.

If Nginx is used, you can also set it through the Nginx configuration file, which is less invasive and can implement 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.