Use Xhprof to analyze your PHP program

Source: Internet
Author: User

Quote from original Http://blog.aboutc.net/php/17/php-profiler-xhprof

Brief introduction

Xhprof is a lightweight PHP performance analysis tool, similar to Xdebug, but more detailed than the XDEBUG analysis, including the number of calls to the function, the time spent (time spent on itself and the time it takes to include internal functions), the amount of memory/cpu, the peak of memory, and the percentage , why use is , because the source package in the xhprof_lib/display/xhprof.php file there is a Incl. User (microsecs) and Incl. Sys (microsec) did not show in the results, interested students can study.

Installation
$ wget HTTPS:Github.com/facebook/xhprof/tarball/master-o xhprof.tar.gz$ tar zxf xhprof.Tar.gz$ CD Facebook-Xhprof-B8c76ac/Extension/# phpize #./configure--with-php-config= '/path/to/php-config ' # make && make Install# make Test# vi/etc/php.d/xhprof.ini;extension = Xhprof . so; Note: output_dir  must be present and writable xhprof output_dir = /tmp/# service php-fpm restart or service httpd restart       span>                  

Of course you can also use yum install php-pecl-xhprof the installation, but the source package xhprof_html and xhprof_lib These two folders are needed, see Using the Web interface.

Simple to use

Now run the following example to see the effect:

$ php -f facebook-xhprof-b8c76ac/examples/sample.php

Output slightly.

Meaning in the output result:

ct 函数调用次数,wt 花费的时间,cpu 花费的 CPU 时间(微秒即百万分之一秒),mu 使用的内存(bytes),pmu 使用的内存峰值(bytes)。
Xhprof_enable parameters

You can also give xhprof_enable() incoming:

XHPROF_FLAGS_CPU 分析结果中添加 CPU 数据XHPROF_FLAGS_MEMORY 分析结果中添加内存数据XHPROF_FLAGS_NO_BUILTINS 跳过 PHP 内置函数

One or more of the three parameters, similar to this:

xhprof_enable(XHPROF_FLAGS_NO_BUILTINS);xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
Using the Web interface

However, this output is not meaningful for us when the program is large, xhprof 源码包 xhprof_html and xhprof_lib Two folders are provided in PHP, and the Web interface for displaying xhprof analysis results is more readable than the previous example output.

$ mkdir/Var/Www/Html/xhprof$ MV Xhprof_html/Xhprof_lib/ Examples//var/www/html/xhprof /$ cd /var/ www/html/$ VI xhprof< Span class= "pun" >/examples/sample.php   ' <xhprof-ui-address> '   Replace with   ' localhost/xhprof/xhprof_html '   

/var/www/htmlAs a DOCUMENT_ROOT directory.

To test, the browser runs:

http://localhost/xhprof/examples/sample.php

And the content of the browser output finally has a link address like this:

http://localhost/xhprof/xhprof_html/index.php?run=50ec187a72aa9&source=xhprof_foo

/tmp/xhprofA file similar to the one in the directory is 50ec187a72aa9.xhprof_foo.xhprof used to store the data information of the XHPROF analysis results as the data source for the above URL address.

Opening this link is the Web Analytics results page for xhprof. This page provides:

Calls: Number of calls to a functionIncl. Wall Time (Microsec) : time spent containing intrinsic functions, in microseconds excl.wall time  ( microsec): time spent without intrinsic functions, microseconds  Note: incl: including  included shorthand excl< Span class= "pun" >: for  excluding wall time : Meaning the clock time is the time that the task takes                

The following columns are simply explained based on this result page Function Name :

main():一个虚构的函数,程序根节点[email protected]:递归调用 2 次

Another load::filename and run_init::filename is to track PHP include/require operations.

In this result strlen and xhprof_disable for PHP built-in functions, if we want to skip built-in functions for analysis, and to count the CPU and memory usage information what to do, the above mentioned xhprof_enable parameter, modify the xhprof/examples/sample.php file to see:

将 xhprof_enable(); 行,替换为xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);

Refresh the next page, open the last URL of the page again, this time the information provided is very detailed.

Incl.Cpu(Microsecs): Contains intrinsic functionsCpuTime spent in microsecondsExcl.Cpu(Microsec): does not contain intrinsic functionsCpuTime spent in microsecondsIncl. Memuse (bytes): Contains internal function memory, Unit bytesExcl. Memuse (bytes): does not contain internal functions that occupy memory, unit bytesincl.peakmemuse  (bytesexcl. peakmemuse  (bytes can be considered as a total of three cases: 1. 2.  not including intrinsic functions or function itself 3.< Span class= "PLN" >  Percentage of total (time or memory use)           

peakMeaning peak, 这里的 CPU 花费时间为什么是 0 呢 and then modify the following xhprof/examples/sample.php file to see:

将 foo() 函数中的 for 循环条件 $idx < 5 改为 50

Take a look at the Web Analytics page results and the CPU time comes out.

Installing Graphviz

There is a very obvious link in the Analysis results page View Full Callgraph , which is the process of viewing the entire program's function call. Generated using Graphviz.

# yum install graphviz

To compile the installation, note that dependencies are installed first libpng .

Reference
net tuts+:http://net.tutsplus.com/tutorials/php/advanced-codeigniter-profiling-with-xhprof/源码包中的:xhprof_html/docs/index.htmlhttp://www.162cm.com/p/xhprofdoc.html
Reprint please indicate the source.
This article address:http://blog.aboutc.net/php/17/php-profiler-xhprof

Use Xhprof to analyze your PHP program

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.