Php performance monitoring module XHProf

Source: Internet
Author: User

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

  1. wget http://pecl.php.net/get/xhprof-0.9.2.tgz
  2. tar zxvf xhprof-0.9.2.tgz
  3. Cp./xhprof-0.9.2.tgz./www // xhprof itself comes with a web Analysis page, put under my web Server
  4. cd xhprof-0.9.2/extension
  5. /usr/local/php/bin/phpize
  6. ./configure --enable-xhprof --with-php-config=/usr/local/php/bin/php-config
  7. make && make install

2. Configuration

  1. [xhprof]
  2. extension=xhprof.so
  3. 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.

  1. <?php
  2. function bar($x){
  3. if($x >0){
  4. bar($x -1);
  5. }
  6. }
  7. function foo(){
  8. for($idx =0; $idx <5; $idx++){
  9. bar($idx);
  10. $x = strlen("abc");
  11. }
  12. }
  13. // Start xhprof
  14. xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
  15. // Call the foo function, which is also the function to be analyzed.
  16. foo();
  17. // Stop xhprof
  18. $xhprof_data = xhprof_disable();
  19. // Obtain statistics
  20. print_r($xhprof_data);
  21. $XHPROF_ROOT = realpath(dirname(__FILE__).'/..');
  22. include_once $XHPROF_ROOT ."/xhprof_lib/utils/xhprof_lib.php";
  23. include_once $XHPROF_ROOT ."/xhprof_lib/utils/xhprof_runs.php";
  24. // Save the statistics and generate the statistics ID and source name
  25. $xhprof_runs =newXHProfRuns_Default();
  26. $ Run_id = $ xhprof_runs-> save_run ($ xhprof_data, "xhprof_foo"); // The source Name Is xhprof_foo
  27. // A statistics window is displayed to view the statistics.
  28. echo "<script language='javascript'>window.open('../xhprof_html/index.php?run=". $run_id ."&source=xhprof_foo');</script>";
  29. ?>

The following are partial results:

  1. [foo==>bar]=>Array
  2. (
  3. [Ct] => 5 // bar () This function is called five times
  4. [Wt] => 63 // The time required to run bar () each time. I wonder if this is the average value.
  5. [Cpu] => 0 // bar () for each operation, cpu operation time
  6. [Mu] => 2860 // every time bar () is run, the memory used by php changes
  7. [Pmu] => 0 // every time bar () is run, php changes the memory usage when the memory usage is highest.
  8. )

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:

Related Article

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.