Php performance monitoring module XHProf
1. What is XHProf?
XHProf is a hierarchical PHP performance analysis tool. It reports the number of requests and various metrics at the function level, including the blocking time, CPU time, and memory usage. The overhead of a function can be subdivided into the overhead of callers and callers. In the XHProf data collection phase, it records the tracing of call times and the inclusive indicator arc in a dynamic callgraph program. It is unique in the report/post-processing phase of data computing. During data collection, XHProfd processes recursive function calls through a detection loop, and avoids an endless loop by giving a useful name to calls in every depth in a recursive call. The XHProf analysis report helps you understand the structure of the code to be executed. It has a simple HTML User Interface (written in PHP ). The browser-based performance analysis user interface is easier to view or share the results with colleagues. The call relationship diagram can also be drawn.
2. Install the XHProf extension module
1. Install
wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar zxvf xhprof-0.9.2.tgz
Cp./xhprof-0.9.2.tgz./www // xhprof itself comes with a web Analysis page, put under my web Server
cd xhprof-0.9.2/extension
/usr/local/php/bin/phpize
./configure --enable-xhprof --with-php-config=/usr/local/php/bin/php-config
make && make install
2. Configuration
[xhprof]
extension=xhprof.so
Xhprof. output_dir =/home/yicheng/xhprof // If the directory is not added, it is placed under/tmp by default.
Iii. XHProf Testing
As we mentioned earlier, XHProf itself comes with a web version of the testing tool, which contains a small example. Let's take a look at this example. I made some modifications and comments.
<?php
function bar($x){
if($x >0){
bar($x -1);
}
}
function foo(){
for($idx =0; $idx <5; $idx++){
bar($idx);
$x = strlen("abc");
}
}
// Start xhprof
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
// Call the foo function, which is also the function to be analyzed.
foo();
// Stop xhprof
$xhprof_data = xhprof_disable();
// Obtain statistics
print_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 the statistics and generate the statistics ID and source name
$xhprof_runs =newXHProfRuns_Default();
$ Run_id = $ xhprof_runs-> save_run ($ xhprof_data, "xhprof_foo"); // The source Name Is xhprof_foo
// A statistics window is displayed to view the statistics.
echo "<script language='javascript'>window.open('../xhprof_html/index.php?run=". $run_id ."&source=xhprof_foo');</script>";
?>
The following are partial results:
[foo==>bar]=>Array
(
[Ct] => 5 // bar () This function is called five times
[Wt] => 63 // The time required to run bar () each time. I wonder if this is the average value.
[Cpu] => 0 // bar () for each operation, cpu operation time
[Mu] => 2860 // every time bar () is run, the memory used by php changes
[Pmu] => 0 // every time bar () is run, php changes the memory usage when the memory usage is highest.
)
I personally think that this tool is used by changing people. Who has actually tested it and php code execution efficiency? I don't think so. As long as you don't write code as you like, the difference is not very big. It's better to think about how to improve the processing capability of the database if you spend time on it. If you think you have done a good job in other aspects, it doesn't matter if you do this.
This article permanently updates the link address: