How to configure the PHP analysis tool locally Xhprof

Source: Internet
Author: User

Test environment:

LinuxMint + nginx1.4.6+mysql5.5+php5.5

What is Xhprof?

Xhprof is a layered PHP performance analysis tool. It reports the number of requests at the function level and various metrics, including blocking time, CPU time, and memory usage. The overhead of a function, which can be subdivided into the cost of the caller and the callee, xhprof the data collection phase, which records the number of calls to trace and an inclusive indicator of the arc in the dynamic callgraph of a program.

One, how to install?

1. Download the latest Xhprof package

Website address: https://pecl.php.net/package/xhprof

Download it down:

wget https://pecl.php.net/get/xhprof-0.9.4.tgz

Extract:

TAR-XVF xhprof-0.9. 4. tgz

2, compile and install the PECL extension Library

Reference Address: http://php.net/manual/zh/install.pecl.phpize.php

CD xhprof-0.9. 4/extension/phpize. /Configuremakesudo make install

After the compilation succeeds, there will be a xhprof.so file in the extension library directory

Extension Library Directory Address:/usr/lib/php5/20121212/

3. Modify the PHP configuration file

Since my computer is using php-cgi, I need to add a configuration.

Cd/etc/php5/cgi/conf.d

NEW: Xhprof.ini File

Extension=xhprof.so

4, restart PHP, restart php-cgi

To restart PHP:

sudo service php5-fpm restart

Restart php-cgi

Kill-917445

Php-cgi-b 127.0.0.1:9000&

5. Copy the xhprof to the Web site root directory

For example, my root directory is:/data/webroot/

You can copy two directories:

Cp-r xhprof_html//data/webroot/-R xhprof_lib//data/webroot/

6 to see if the local environment is properly installed Xhprof

New file: info.php

<? Phpecho phpinfo ();

If you can search the page for xhprof, the xhprof is installed correctly:

xhprofxhprof     0.9. 4CPU num    4

Second, how to use?

Xhprof is relatively simple to use.

In the downloaded directory there is a file examples/sample.php file

<?PHP function Bar ($x) {if($x >0) {Bar ($x-1); }} function foo () { for($idx =0; $idx <5; $idx + +) {bar ($IDX); $x= Strlen ("ABC"); }} //Start Profilingxhprof_enable ();//Run Programfoo ();//Stop Profiler$xhprof _data =xhprof_disable ();//Display Raw xhprof data for the profiler runPrint_r ($xhprof _data); $XHPROF _root= Realpath (DirName (__file__).'/..'); include_once $XHPROF _root. "/xhprof_lib/utils/xhprof_lib.php"; include_once $XHPROF _root."/xhprof_lib/utils/xhprof_runs.php"; //Save raw data for this profiler run using default//implementation of Ixhprofruns.$xhprof _runs =NewXhprofruns_default ();//Save the run under a namespace "Xhprof_foo"$run _id = $xhprof _runs->save_run ($xhprof _data,"Xhprof_foo");

echo "---------------\ n".
"Assuming you has set up the HTTP based UI for \ n".
"Xhprof at some address, you can view run at \ n".
"Http://localhost/xhprof/index.php?run= $run _id&source=xhprof_foo\n".
"---------------\ n";

Viewing this file in a browser displays the following code:

---------------Assuming you has set up the HTTP based UI for xhprof at some address, you can view run at Http://localhos T/xhprof/index.php?run=562614ece2e2d&source=xhprof_foo---------------

At this point access: http://localhost/xhprof/index.php will appear several files:

No Xhprof runs specifiedinchThe URL. Existing runs:562614ece2e2d.xhprof_foo.xhprof --Ten- -  -: -: -562614e0b76b2.xhprof_foo.xhprof --Ten- -  -: -: ,562614a33f6ec.xhprof_foo.xhprof --Ten- -  -: -: -5626146be4aaf.xhprof_foo.xhprof --Ten- -  -: -: One5626146324cdd.xhprof_foo.xhprof --Ten- -  -: -:Geneva

Click to enter one:

Http://localhost/xhprof/index.php?run=562614ece2e2d&source=xhprof_foo

Clicking [View full CallGraph] may cause an error

Failed to execute cmd: "Dot-tpng". stderr: ' sh:1: Dot:not found '

Because no graphical tools are installed

sudo apt-get Install Graphviz

Once the installation is complete, open it and you'll see the view.

noun: 1 . Inclusive time: Includes all execution times for child functions.  2. Exclusive time/Self time: How long it takes the function to execute itself, not including subtree execution time.  3. Wall time: Spent or time on a wall clock.  4. CPU time: The user consumes 5times the kernel consumes time. Inclusive CPU: Includes 6 of CPU occupied by child functions. Exclusive CPU: The CPU occupied by the function itself

How to configure the PHP analysis tool locally 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.