One way to analyze PHP performance bottlenecks using Xhprof

Source: Internet
Author: User

Installing the xhprof Extension

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 has not been updated for a long time, so far does not support PHP7,PHP7 can use the https://github.com/phacility/....

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 in the project 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 = new Xhprofruns_default ();    $run _id = $xhprof _runs->save_run ($xhprof _data, ' xhprof ');});

Code parsing:
$xhprof_dataAll the function call time and CPU memory consumption are recorded in the program running, which can be controlled by xhprof_enable parameters, and the parameters currently supported are:

    • HPROF_FLAGS_NO_BUILTINSSkips all built-in (internal) functions.

    • XHPROF_FLAGS_CPUAdd CPU data to the output performance data.

    • XHPROF_FLAGS_MEMORYAdd the memory data to the output performance data.

The subsequent processing has nothing to do with the xhprof extension, which is to write a storage class XHProfRuns_Default , $xhprof_data serialize and save to a directory, you can output the XHProfRuns_Default(__DIR__) results to the current directory, if not specified will be read php.ini in the configuration file xhprof.output_dir , still not specified will be output to /tmp.

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 Web server:

Xhprof.test.com.conf

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


The default UI lists the following:

    • Funciton Name: Name of function

    • Calls: Number of calls

    • Incl. Wall time (MICROSEC): function runtime (including child functions)

    • iwall%: Function run time (including sub-function) percentage

    • Excl. Wall time (MICROSEC): function runtime (excluding child functions)

    • ewall%: Function run time (excluding child functions)

In the Web, you can also see the [View full CallGraph] link, click to draw a visual performance analysis diagram, if you click on the error, may be missing dependent Graphviz. Graphviz is a tool for drawing graphics that gives you a more intuitive view of performance bottlenecks. If necessary, you can install:

Yum install-y Libpngyum install-y Graphviz

Effect:

Non-intrusive introduction of XHPROF

Earlier we implemented the analysis by adding code in the project 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 = new Xhprofruns_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:
Jifen.cc.conf

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 = new Xhprofruns_default ();        $xhprof _runs->save_run ($xhprof _data, ' xhprof ');        });

Summarize

In this article, we show you how to analyze PHP performance based on the xhprof extension, log in to the logs, and finally use the Xhprof extended UI to present it on the web. Key points of knowledge:

    • Installing the xhprof Extension

    • Inject xhprof into your application

    • Analysis results based on nginx display

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.