Use XHProf to find the PHP performance bottleneck instance, xhprofphp

Source: Internet
Author: User

Use XHProf to find the PHP performance bottleneck instance, xhprofphp

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

1. Install Xhprof Extension

// Download https://github.com/facebook/xhprofunzip xhprof-master.zip cd xhprof-master/extension // usr/local/php/bin/phpize on github. /configure -- with-php-config =/usr/local/php/bin/php-config -- enable-xhprofmake & make install

2. 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.

3. Move relevant files to the Project

// Xhprof download xhprof_html and unzip-r xhprof-master/xhprof_html/usr/local/nginx/html/xhprof/cp-r xhprof-master/xhprof_lib/usr/local/ nginx/html/xhprof/

Configure a domain name that the browser can access to the http://will.com/xhprof/xhprof_html/index.php

server{ listen 80; server_name will.com; location / {  root /usr/local/nginx/html;  index index.html; } location ~ \.php$ {  root html;  fastcgi_pass 127.0.0.1:9000;  fastcgi_index index.php;  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  include  fastcgi_params; } }

4. Install graphivz

// Graphviz needs to be installed. Otherwise, failed to execute cmd: "dot-Tpng". stderr: 'sh: dot: command not found 'yum-y install graphviz will be reported when you view the performance image.

5. Compile the test file

// Start position of the entry file xhprof_enable (XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU); business logic... // $ xhprof_data = xhprof_disable (); include_once "/usr/local/nginx/html/xhprof/xhprof_lib/utils/xhprof_lib.php "; include_once "/usr/local/nginx/html/xhprof/xhprof_lib/utils/xhprof_runs.php"; $ objXhprofRun = new XHProfRuns_Default (); // data is stored in php. xhprof. $ run_id = $ objXhprofRun-> save_run ($ xhprof_data, "test") in the directory set by output_dir ");

Complete code example (random full red packet reduction demo)

<? Phpxhprof_enable (XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU); function show ($ info) {echo "<pre>"; print_r ($ info );} // do not perform data verification $ rules = array (2 => array ('Min' => 1, 'max '=> 10, 'chance' => 30 ), // amount: cent probability: 100% (default value: 100%, less than calculated in the first step) array ('Min' => 11, 'max '=> 25, 'chance '=> 60), array ('Min' => 26, 'max' => 50, 'chance '=> 10 ), array ('Min' => 50, 'max '=> 80, 'chance' => 0), array ('Min' => 80, 'max '=> 100, 'chance '=> 0),); $ total_money = 1 0000; // total red packet amount $ res = array (); while ($ total_money> 0) {$ index = getLevel ($ rules); $ money = setMoney ($ rules, $ index); if ($ money> $ total_money) // insufficient amount {$ money = $ total_money; $ total_money = 0 ;}else {$ total_money-= $ money ;} $ res [] = ($ index + 1 ). "---". $ money;} echo show ($ res); echo $ total_money. "<br/>"; // 1. first, determine the grade function getLevel ($ rules) {$ level = array (); $ chance = 0; foreach ($ rules as $ k => $ v) {if ($ V ['chance ']> 0) {$ chance + = $ v ['chance'] * 100; // increase by 100 times $ level [$ k] = $ chance; }}$ index = 0; $ rand_num = mt_rand (1, 10000 ); foreach ($ level as $ k = >$ v) {if ($ rand_num <= $ v) {$ index = $ k; break ;}} return $ index ;} // 2. after determining the level, determine the amount function setMoney ($ rules, $ index) {$ money = mt_rand ($ rules [$ index] ['Min'] * 10000, $ rules [$ index] ['max '] * 10000)/10000; $ money = ceil ($ money); $ money> 1 & $ money = $ money -1; // prevent ticket-free return $ money;} $ xhprof_data = xhprof_disable (); include_once "/usr/local/nginx/html/xhprof/xhprof_lib/utils/xhprof_lib.php"; include_once "/usr/local/nginx/html/xhprof/xhprof_lib/utils/xhprof_runs.php "; $ objXhprofRun = new XHProfRuns_Default (); // data is stored in php. xhprof. output_dir Set directory to $ run_id = $ objXhprofRun-> save_run ($ xhprof_data, "test"); echo "http://will.com/xhprof/xhprof_html/index. Php? Run = $ run_id & source = test "; // The variable $ runId is the id of the analysis result generated in this request. Finally, we output a link address, you can use the address to view the analysis results of this request.

6. View analysis results

Run the business code first;

Then open the http://will.com/xhprof/xhprof_html/index.php in the browser and click the last generation xhprof File

NoticeView Full CallgraphLink, through which we can see the graphical analysis results

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.

In addition, the meaning of the xhprof Report field is attached:

Function Name: method Name.

CILS: the number of times a method is called.

CILS %: percentage of the number of method Calls in the total number of Calls to the same level.

Incl. Wall Time (microsec): The Time taken to execute a method, including the execution Time of sub-methods. (Unit: microseconds)

IWall %: percentage of time spent on method execution.

Excl. Wall Time (microsec): The Time taken to execute the method itself, excluding the execution Time of the sub-method. (Unit: microseconds)

EWall %: percentage of time spent on method execution.

Incl. CPU (microsecs): the CPU time used for method execution, including the execution time of sub-methods. (Unit: microseconds)

ICpu %: Percentage of CPU time consumed by method execution.

Excl. CPU (microsec): the CPU time consumed by the method itself, excluding the execution time of sub-methods. (Unit: microseconds)

ECPU %: Percentage of CPU time consumed by method execution.

Incl. MemUse (bytes): Memory occupied by method execution, including the memory occupied by sub-method execution. (Unit: bytes)

IMemUse %: Percentage of memory occupied by method execution.

Excl. MemUse (bytes): Memory occupied by method execution, excluding the memory occupied by sub-method execution. (Unit: bytes)

EMemUse %: Percentage of memory occupied by method execution.

Incl. PeakMemUse (bytes): the peak value of Incl. MemUse. (Unit: bytes)

IPeakMemUse %: Percentage of Incl. MemUse peaks.

Excl. PeakMemUse (bytes): Excl. MemUse peak value. Unit: (bytes)

EPeakMemUse %: Excl. MemUse peak percentage.

The above example of using XHProf to find the PHP performance bottleneck is all the content that I have shared with you. I hope to give you a reference and support for the help house.

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.