Installation and use of Facebook tool xhprof-Analysis of PHP performance

Source: Internet
Author: User
Tags flock fpm zts

Source: http://www.cnblogs.com/wangtao_20/archive/2013/09/13/3320497.html

Download the source package URL

Http://pecl.php.net/package/xhprof



It says, the PHP version that each version applies to.


Planning (figuring out ideas beforehand)



One, this is a form of PHP expansion. Our installation gd2,curl are all extensions of PHP. It's just sometimes installed when you compile it.
Like operating a MySQL database, it is also an extension such as mysql.so, where extensions are installed to invoke mysql_query () functions.
To operate the Oracle database, there is also a corresponding Oracle extension added to the PHP engine.



Now add the xhprof extension to PHP.


It's been a long time since the Phpize installation has been extended. I've forgotten almost all of it myself. So I went back to my blog to find a previous article to review.

Http://www.cnblogs.com/wangtao_20/archive/2011/03/16/1986508.html




PS: I am also thinking, how can this thing be so easy to forget. I only know what his role is. But he did not remember the steps of his operation at all. To pay attention to the details.

Look, I have to find a way to understand it in a popular manner.





Ii. php.ini items that need to be configured


[Xhprof]
extension=xhprof.so;
; Directory used by default implementation of the Ixhprofruns
; Interface (namely, the Xhprofruns_default Class) for storing
; Xhprof runs.
;
;xhprof.output_dir=<directory_for_storing_xhprof_runs>
; Store the default directory for Xhprof run data
Xhprof.output_dir=/tmp/xhprof



With this extension, you can call this extended built-in function in your PHP code to do performance monitoring, like the following


Xhprof_enable (xhprof_flags_cpu + xhprof_flags_memory);

................ This is the code block to be monitored.


$data = Xhprof_disable ();

Include_once "xhprof_lib/utils/xhprof_lib.php";
Include_once "xhprof_lib/utils/xhprof_runs.php";
$objXhprofRun = new Xhprofruns_default ();//The data is saved in the directory Xhprof.output_dir set in php.ini.
$run _id = $objXhprofRun->save_run ($data, "test");


=====================================================

Several extension functions are as follows




Step implementation


1, first find my server on the installation of PHP directory, phpize is generally in the installation directory, as follows:

/usr/local/php/bin/phpize

2, to find out in which directory Php-config (below), my server in:

/usr/local/php/bin/php-config

Purpose: This file is used when compiling the following

./configure--with-php-config=/usr/local/php/bin/php-config



3, find my server on the PHP extension in which directory, I am not sure, I think to PHP.ini can also see, as follows


Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/" This path feels a bit long. No need to change the setting in php.ini, I don't think it is necessary (business and time cost, this part is not a constraint). Expand by the original continuation.




Now know the extended directory is:/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

In other words, I want to extract the Xhprof source package into this directory (after decompression will generate a new folder).


Then I'm going to go to this directory to run Phpize (so it's easy to generate configure files under this directory),

Phpize features: In a directory run Phpize, will be in a directory to generate configure.











Get the top path, shell command practice



cd/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

Decompression download to the XHPROF compression package (I did not download through the wget, I am the compression directly through FTP upload to the no-debug-non-zts-20060613 directory).



Tar zxf xhprof-0.9.3.tgz #解压后, inside there is a extension folder, into the inside to go (the purpose is to go inside to run phpize), after the unpacked directory structure as follows




CD xhprof-0.9.3/extension/#切换到这个扩展的源码目录去

Running phpize under this directory will generate a configure file in the extension directory (this is the phpize mechanism)


/usr/local/php/bin/phpize

To look at the extended directory, you will find that a configure file has been generated in the extension directory. Run it


======================================


./configure--with-php-config=/usr/local/php/bin/php-config #用到了前面找到的php-config file.

Make && make install

Make Test
======================================




After the success of the run, you will be prompted where the generated xhprof.so files are, prompting for information:

Libraries have been installed in:
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xhprof-0.9.3/extension/modules


A file already exists in this directory:
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xhprof.so



Just generated xhprof.so This module file, now to be loaded in php.ini just build the xhprof.so module to be effective:

[Xhprof]
extension=xhprof.so;
; Directory used by default implementation of the Ixhprofruns
; Interface (namely, the Xhprofruns_default Class) for storing
; Xhprof runs.
;
;xhprof.output_dir=<directory_for_storing_xhprof_runs>
Xhprof.output_dir=/tmp/xhprof






Smooth reload php.ini file:/usr/local/php/sbin/php-fpm Reload
Tips:

Reload Service PHP-FPM Done

Description of success.

Now go to Phpinfo to see if the xhprof extension is loaded successfully






Installation of drawing tools (optional, can be installed later)

Yum Install-y Graphviz





================================================

Practice of using tools

================================================

Code in index.php:

<?php
Error_reporting (-1);


Xhprof_enable (xhprof_flags_cpu + xhprof_flags_memory);
 
$begin _time = Microtime_float ();




///////////////statistic Execution Speed code
$end _time = Microtime_float ();

$exec _time  = $end _time-$begin _time;

//@save_stat ($exec _time);

for ($i =0 $i <10; $i + +) {


for ($j =0; $j <10; $j +) {

}

}



$data = xhprof _disable ();

Include_once "xhprof_lib/utils/xhprof_lib.php";//Copy from source package Xhprof_lib This folder comes directly to call
Include_once xhprof_ Lib/utils/xhprof_runs.php "; &NBSP
$objXhprofRun = new Xhprofruns_default ();//data will be saved in php.ini Xhprof.output_dir set directory to  
$run _id = $ Objxhprofrun->save_run ($data, "test"); The
//second parameter is the definition file name. If the name is "Xhprof", the file generated by the directory that is set up in Xhprof.output_dir: 522ab85f40229.xhprof.xhprof. The
//format is: "ID identification. Group name. Xhprof", id identification is the result of $run_id.

ID identification is to generate a new identity each time it is run, that is, to generate a new file that records each run of data





Var_dump ($run _id);

echo "http://www.xxxx.com/xhprof_html/index.php?run={$run _id}&source=test\n";//source value must be the name specified in Save_run. This is actually based on the number and name to locate the corresponding file "522ab85f40229.xhprof.xhprof"

This address is output so that you can view the results of the analysis directly.




function Save_stat ($time)
{
Static $call _count=1;
$call _limit = 10;
if (! $time) return;
$date = Date ("y-m-d");//The file is generated temporarily by day. Easy access
$exec _stat_file = './exec_stat '. Directory_separator. " Exec_stat_file-". $date.". TXT ";
$fp = fopen ($exec _stat_file, ' ab ');
if (Flock ($FP, LOCK_EX))
{
$s = ' access: '. Date (' y-m-d h:i:s '). ', Execute time: '. $time. ' s,request_url:http://'. $_server[' http_host '].$_server[' Request_uri ']. "| |". Php_eol;
Fwrite ($fp, $s);
Flock ($FP, lock_un);
}else{
Usleep (1000);
if ($call _count< $call _limit)
{
$call _count++;
Save_stat ($time);
}
}

@fcolse ($FP);
Var_dump ($FP);

}
function Microtime_float ()
{
List ($usec, $sec) = Explode ("", Microtime ());
Return ((float) $usec + (float) $sec);
}
?>

Bold Bold, is the analysis of the code used.

It uses the code in the Xhprof_lib in the source package, and the class "Xhprofruns_default" in the code is inside.

Once you have generated the analysis results, you can now view them using the Web version of the tools available in the source package.

Xhprof_html this folder anywhere, as long as the directory can be accessed through the web can be, but this will be used in the Xhprof_lib class, so or together to copy the past, with the source package to maintain the same structure is good. Xhprof_html and xhprof_lib should be kept parallel.

Me here, this tool is accessed in the following way: http://www.xxxx.com/xhprof_html/index.php?run=xxx&source=xxx\n

The value of source must be the name specified in Save_run. This is actually based on the number and name to locate the corresponding file "522ab85f40229.xhprof.xhprof"

Access to see the following results

In the picture, the red represents the most performance-consuming, and the yellow is second.

I know only so much. The concrete analysis results how to look, in the folder xhprof_html has a docs directory, inside is the instruction document. Said some of the definitions of special terms

Note: Xhprof.output_dir the set of directories in php.ini to create them manually. This tool will not be created automatically. The folder is not there. Will not be generated. In addition, under Linux also to ensure that the right to set up, I set the owner is www can be used.

Unit: 1s (sec) =1000ms (MS)

Official explanation inclusive Time (or subtree time): Includes time spent in the function as as is in descendant functions called A given function. Exclusive time/self time:measures time spent the function itself. Does not include the time in descendant functions. Wall time:a.k.a. Elapsed or Wall clock time. CPU time:cpu time in user spaces + CPU time in kernel spaces

Exclusive time represents the execution times of the function itself. This time does not contain the time-consuming function of calling other functions (in fact, it is time-consuming to remove other functions inside the function, because the functions called in the functions are also counted separately).

and inclusive time is to include the call of the child function in it (descendant functions)

So, my understanding is exclusive time<=inclusive time.

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.