Use Xhprof to find instances of PHP performance bottlenecks _php instances

Source: Internet
Author: User
The following small series for everyone to share an article on the use xhprofFind examples of PHP performance bottlenecks, xhprofis an extension of the test PHP performance developed by Facebook, which documents the use in PHP applications xhprofPerform performance optimizations on PHP to find a way to performance bottlenecks. Have a good reference value, hope to help you. Follow the small series together to see it!

First, install the xhprof extension


GitHub Downloads https://github.com/facebook/xhprofunzip xhprof-master.zip CD xhprof-master/extension//usr/local/php/ Bin/phpize./configure--with-php-config=/usr/local/php/bin/php-config--enable-xhprofmake && make install


Ii. Modification of 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.

Iii. moving the relevant files into the project


Xhprof download xhprof_html and Xhprof_libcp-r xhprof-master/xhprof_html/usr/local/nginx/html/xhprof/cp-r in a compressed package xhprof-master/xhprof_lib/usr/local/nginx/html/xhprof/


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


server{listen; 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;}}


Iv. installation of Graphivz


Need to install Graphviz otherwise viewing the performance picture will be reported failed to execute cmd: "Dot-tpng". stderr: ' Sh:dot:command not found ' yum-y install Graphviz


V. Preparation of test documents


Start position of the portal file Xhprof_enable (xhprof_flags_memory | XHPROF_FLAGS_CPU) business logic ...//business logic ends $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 will be saved in php.ini xhprof.output_dir set directory go $run _id = $objXhprofRun->save_ Run ($xhprof _data, "test");


Full code example (random full bonus Demo)


<?phpxhprof_enable (Xhprof_flags_memory | XHPROF_FLAGS_CPU), function Show ($info) {echo "<pre>"; Print_r ($info);} Do not make data check $rules = Array (2=>array (' min ' =>1, ' Max ' =>10, ' chance ' =>30),//Amount: Split probability: Percent (default is 100%, less than 100% according to first file calculation) 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 = 10000;// Red Envelope Total 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 [' ch Ance ']>0) {$chance + = $v [' Chance ']*100;//expanded 100 times 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 grade, then determine the amount of function Setmoney ($rules, $index) {$money = Mt_rand ($rules [$index] [' min ']*10000, $rules [$index] [' Max ']*10000)/10000; $money = Ceil ($money); $money > 1 && $money = $money -1;//to prevent the return $money of a free order;} $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 will be saved in php.ini xhprof.output_dir set directory go $run _id = $objXhprofRun->save_ Run ($xhprof _data, "test"); echo "http://will.com/xhprof/xhprof_html/index.php?run= $run _id&source=test";//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 results of this request.


Vi. Viewing analysis results

Run the business code first;

Then the browser opens http://will.com/xhprof/xhprof_html/index.php and clicks the last time that you generated the Xhprof file

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.

Also attached, xhprof report field meaning:

Function Name: Method name.

Calls: The number of times the method was called.

calls%: The percentage of the number of method invocations that are called in the total numbers of sibling methods.

Incl.wall Time (microsec): How long the method takes to execute, including the execution time of the child method. (Unit: microseconds)

iwall%: Percentage of time spent on method execution.

Excl. Wall time (microsec): How long the method itself takes to execute, excluding the execution time of the child method. (Unit: microseconds)

ewall%: The percentage of time that the method itself takes to execute.

Incl. CPU (MICROSECS): The CPU time spent by the method execution, including the execution time of the child method. (Unit: microseconds)

icpu%: Percentage of CPU time spent by method execution.

Excl. CPU (MICROSEC): The method itself executes the CPU time that is spent, excluding the execution time of the child method. (Unit: microseconds)

ecpu%: The percentage of CPU time spent by the method itself execution.

Incl.memuse (bytes): The memory used by the method to execute, including the memory occupied by the child method execution. (Unit: bytes)

Imemuse%: Percentage of memory consumed by method execution.

Excl.memuse (bytes): The method itself executes the occupied memory, excluding the memory occupied by the child method execution. (Unit: bytes)

Ememuse%: The percentage of memory consumed by the method itself.

Incl.peakmemuse (bytes): Incl.memuse Peak. (Unit: bytes)

Ipeakmemuse%:incl.memuse peak percentage.

Excl.peakmemuse (bytes): Excl.memuse Peak. Unit: (bytes)

Epeakmemuse%:excl.memuse peak percentage.

The above this article uses Xhprof to find PHP performance bottleneck of the example is small to share all the content of everyone, hope to give you a reference!!

Related recommendations:

Use Xhprof to find instances of PHP performance bottlenecks

How PHP integrates snappy into xhprof

PHP7 xhprof the installation and use of the Profiling Tools tutorial

Related Article

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.