Analyze PHP performance bottlenecks with Xhprof

Source: Internet
Author: User

Installing Xhprof
wget http://pecl.php.net/get/xhprof-0.9.4.tgztar zxf xhprof-0.9.4.tgzcd xhprof-0.9.4/extension/sudo phpize./configuresudo makesudo make installcd ../

Configure PHP.ini

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

Note: Xhprof does not support PHP7. Need to download https://github.com/phacility/xhprof.git on GitHub.

Configuring the XHPROF Environment

The two directories in the Xhprof compressed package need to be copied to the specified directory (if defined /work/xhprof/ ):

mkdir /work/xhprof/cp -a xhprof_html/ /work/xhprof/cp -a xhprof_lib/ /work/xhprof/

Then add the entry file to the application framework:

Xhprof_enable(xhprof_flags_memory|xhprof_flags_cpu);register_shutdown_function(function(){$xhprof _data= Xhprof_disable();    if (function_exists(' Fastcgi_finish_request ')){Fastcgi_finish_request();}include_once "/work/xhprof/xhprof_lib/utils/xhprof_lib.php";    include_once "/work/xhprof/xhprof_lib/utils/xhprof_runs.php";    $xhprof _runs=NewXhprofruns_default();    $run _id=$xhprof _runs->save_run($xhprof _data, ' Xhprof ');});

xhprof_enableAnd xhprof_disable is in pairs appear, one is the code to run the front, one is the most behind. The middle is the code to parse.

After the above configuration, we subsequently request the project interface, XHPROF will analyze the request process of CPU, memory, time-consuming and other content. The log is saved in the xhprof.output_dir directory.

Configure the Web

Configured, how to view the log? We can build a simple server:
Xhprof.test.com.conf

server {    listen       80;    server_name  xhprof.test.com;    root /work/xhprof/xhprof_html;    index  index.html index.php;    location ~ \.php$ {        fastcgi_pass   127.0.0.1:9000;        fastcgi_index  index.php;        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        include        fastcgi_params;    }}

Then configure the virtual host xhprof.test.com. Restart Nginx, open xhprof.test.com can see the effect:


Non-intrusive introduction of XHPROF

Earlier we implemented the analysis by adding code in the Portal file. The more elegant way is to create an additional file xhprof.inc.php, which is saved in the /work/xhprof/ directory:

Xhprof_enable(xhprof_flags_memory|xhprof_flags_cpu);register_shutdown_function(function(){$xhprof _data= Xhprof_disable();    if (function_exists(' Fastcgi_finish_request ')){Fastcgi_finish_request();}include_once "/work/xhprof/xhprof_lib/utils/xhprof_lib.php";    include_once "/work/xhprof/xhprof_lib/utils/xhprof_runs.php";    $xhprof _runs=NewXhprofruns_default();    $run _id=$xhprof _runs->save_run($xhprof _data, ' Xhprof ');});

Use PHP's auto-load function to inject this file before executing the code and edit the php.ini:

auto_prepend_file = /work/xhprof/xhprof.inc.php

Then restart the PHP service. This will take effect for all use of the PHP environment.

Or you can write to the Nginx configuration of the specified project:

location ~ \.php$ {                fastcgi_pass   127.0.0.1:9000;        fastcgi_index  index.php;        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;    fastcgi_param PHP_VALUE "auto_prepend_file=/work/xhprof/xhprof.inc.php";        include        fastcgi_params;    }

Then restart the Nginx service. This will only take effect for the project.

Files contained by Auto_prepend_file and Auto_append_file are parsed in this mode, but some limitations, such as functions, must be defined before they are called.

Modify sampling frequency

By default, Xhprof runs every time, and the online environment is set up to have performance implications.

xhprof.inc.php

<?php$profiling= !(Mt_rand()%9); if($profiling)Xhprof_enable(xhprof_flags_memory|xhprof_flags_cpu);register_shutdown_function(function()  Use($profiling){if($profiling){$xhprof _data= Xhprof_disable();        if (function_exists(' Fastcgi_finish_request ')){Fastcgi_finish_request();}include_once "/work/xhprof/xhprof_lib/utils/xhprof_lib.php";        include_once "/work/xhprof/xhprof_lib/utils/xhprof_runs.php";        $xhprof _runs=NewXhprofruns_default();        $xhprof _runs->save_run($xhprof _data, ' Xhprof ');}});
Reference

1, the Xhprof configuration and use Method-Pinterest
https://www.jianshu.com/p/38e3ae81970c
2. Use Xhprof to find PHP performance bottlenecks-Program ape Growth Program-Segmentfault No
1190000003509917
3, PHP performance Tracking and analysis tools Xhprof installation and use-Ma Sing's technical blog-Segmentfault think No
1190000007288664
4, Tideways and Xhgui build php non-intrusive monitoring platform | I'm a big bear.
http://blog.it2048.cn/article-tideways-xhgui/

Analyze PHP performance bottlenecks with Xhprof

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.