Xhprof Extended Installation and use

Source: Internet
Author: User
Tags save file yii

Directory

    • First, XHPROF expansion installation steps
    • Second, the use of xhprof
    • Summarize
    • Resources
First, XHPROF expansion installation steps

Xhprof is an extension of PHP, preferably also directly installed on the Graphviz graphical drawing tool (for xhprof analysis results to be displayed in an intuitive graphical manner), nonsense said, straight to the point.

1. Installation
    • Installation of the PHP5 version
wget http://pecl.php.net/get/xhprof-0.9.4.tgztar -zxvf xhprof-0.9.4.tgz cd xhprof-0.9.4cd extension/phpize./configuremakemake install
    • Installation of the PHP7 version

      Download Source Package XHPROF:GITHUB.COM/RUSTJASON/XHPROF/TREE/PHP7

unzip xhprof-php7.zip cd xhprof-php7/extension/phpize ./configure --with-php-config=/usr/local/php/bin/php-config makemake install
2. Modify the php.ini configuration file

Append the following configuration to the PHP.ini configuration file and create the directory/home/wwwroot/default/xhprof_data

[xhprof]extension = xhprof.so// xhprof分析结果文件存放根目录xhprof.output_dir = /home/wwwroot/default/xhprof_data
3. Add an environment variable XHPROF_ROOT_PATH

In order to use XHPROF for each project to perform performance analysis, it is recommended to add an environment variable to PHP, so that in any project code can easily call Xhprof to analyze performance bottlenecks, do the following:

vim /usr/local/php/etc/php-fpm.confenv[XHPROF_ROOT_PATH]=/usr/local/php/include/xhprof/
4, copy the Xhprof core source code to the above XHPROF_ROOT_PATHThe environment variable is specified under the directory
cp -r xhprof_lib /usr/local/php/include/xhprof/xhprof_lib
5. Copy the following two directories to xhprof_dataUnder the same sibling directory (preferably in the Web root directory)

Viewing the analysis results file is useful

//执行cp -r xhprof_html /home/wwwroot/default/xhprof_htmlcp -r xhprof_lib /home/wwwroot/default/xhprof_lib//改变xhprof_data目录拥有者,为了浏览器访问时能在xhprof_data目录下写入文件chown -R www:www xhprof_data
6. Access the Xhprof root directory

To configure the demo.com domain root directory /home/wwwroot/default/ , you can view the XHPROF results analysis root directory as shown in the following link http://demo.com/xhprof_data/ :

7, visualize the analysis results

With the graphical drawing tool installed, the following analysis results can be graphically displayed and more visually
To execute the installation command:yum install graphviz

Second, the use of xhprof 1, xhprof performance Analysis Small Demo

Here are three ways to implement factorial code

  <?phpxhprof_enable (xhprof_flags_cpu | xhprof_flags_memory), $n = 5;echo JC ($n), Echo ' <br> ', Echo JC ($n), Echo ' <br> ', Echo JC ($n); Echo ' <br> ';    function JC ($n) {if ($n = = 1) {return 1; } return $n * JC ($n-1);}    function Jc2 ($n) {$m = 1;    for ($i =1; $i <= $n; $i + +) {$m = $m * $i; } return $m;}    function Jc3 ($n) {$arr = [];        $arr [1] = 1;    for ($i = 2; $i <= $n; $i + +) {$arr [$i] = $i * $arr [$i-1]; } return $arr [$n];} $data = Xhprof_disable ();//$_server[' Xhprof_root_path '] This is the third step to add the environment variable include_once $_server[' Xhprof_root_path '. "xhprof_lib/utils/xhprof_lib.php"; include_once $_server[' Xhprof_root_path '. "xhprof_lib/utils/xhprof_runs.php"; $x = new Xhprofruns_default ();//stitching file name $xhproffilename = Date (' Ymd_his ');//print_r ($data);d ie;//The printed data here looks very intuitive, so you need to install the Yum install Graphviz graphical interface display, more intuitive $x->save_run ($data, $xhprofFilename);  

After execution of the above small demo, the Xhprof_data directory will generate an analysis of the results of saving files, web-side access to the results file, such as:

When I view the graphical analysis interface at a point, the [View Full Callgraph] problem arises, such as:

Fortunately, the reason is that the PHP configuration file has a disable_functions disabled function list, the inside can be proc_open removed.

The graphical analysis results are shown below

2. How to introduce xhprof in the actual project

Please refer to the following to introduce the idea (in the project controller base class construction method and the destruction method), the thinking technique is only for learning reference, such as:


Below is a list of the XHPROF codes I introduced under the project (in YII2 framework)

<?phpnamespace backend\component;use yii;use Common\component\basecontroller;class BackendBaseController extends    basecontroller{Public $layout = "/content";    Public $enableCsrfValidation = false;    public static $profiling = 0;                Public Function init () {parent::init (); Self:: $profiling = 1;//!        (Mt_rand ()% 9); if (self:: $profiling) {xhprof_enable (Xhprof_flags_cpu |        Xhprof_flags_memory);            }} Public Function __destruct () {if (self:: $profiling) {$data = Xhprof_disable (); $_server[' Xhprof_root_path '] This environment variable is include_once $_server[' Xhprof_root_path ' from the 3rd step.            "/xhprof_lib/utils/xhprof_lib.php"; Include_once $_server[' Xhprof_root_path '].            "/xhprof_lib/utils/xhprof_runs.php";            $x = new Xhprofruns_default ();            Current route $routeName = Yii:: $app->requestedroute; The route is empty, the description is the first page if (Empty ($routeName)) {$routeName = YII:: $app->defaultroute; }//Stitching XHPROF Analysis Results Save file name $xhprofFilename = Str_replace ('/', ' _ ', $routeName). '            _ '. Date (' Ymd_his ');        $x->save_run ($data, $xhprofFilename); }    }}
Summarize

Xhprof is a useful tool to analyze PHP code performance bottlenecks, improve PHP code efficiency, through XHPROF, you can see where the code is slow, where there is space for optimization and so on.

Finally share a xhprof good information about ipc2015-xhprof.pdf download link: pan.baidu.com/s/1epukunxli1gvmtlichycxw Password: 11p0

Resources

1. Use Xhprof to find PHP performance bottlenecks

2.PHP Performance Analysis Tool xhprof

3.xhprof installed Graphviz also reported error failed to execute cmd "dot-tpng"

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.