Install xhprof extension in php5.4

Source: Internet
Author: User

Php5.3 or earlier versions can go to PECL (HTTP:// Pecl.php.netDownload the xhprof extension installation.

However, the PECL version does not support php5.4.

You can go to the xhprof library on GitHub to download: https://github.com/facebook/xhprof

Download and unzip the package.

 
1CD xhprof-master/extension/2 Phpize3./Configure-- Enable-xhprof4 Make5 Sudo Make Install

Change PHP. ini

1 [Xhprof]2Extension = xhprof.So3Xhprof. output_dir = "/document/gbyukg/www/test/xhprof"

Restart Apache.

Easy to use:

  1  ///   start profiling   2   xhprof_enable ();   3   4  ///   run Program   5  ......  6   7   //   stop profiler   8  xxhprof_data  = xhprof_disable (); 

Xhprof_enable () allows you to receive some parameters. By default, only the number of times and time consumed by the method are counted (Xhprof_flags_cpu + xhprof_flags_memory) Enable statistics on CPU and memory usage (CPU statistics will be disabled in the actual production environment because it occupies a lot of resources), you can also passXhprof_flags_no_builtinsTo disable statistics on PHP built-in functions, or even to disable statistics on specified functions by passing the second parameter, for example:

 
1 //Ignore builtin functions and call_user_func * During profiling2 $ Ignore=Array('Call _ user_func ', 'Call _ user_func_array');3Xhprof_enable (0,Array('Ignored _ functions' =>$ Ignore));

xhprof interface tool
the page tool is stored in the Source Code directory xhprof_html folder, the library files in the xhprof_lib folder are also required. Put the two folders in the directory (which should be in the Web Server Directory) generated by the report specified in the PHP. ini file ).
Create a header and tail file:

1<?PHP2 If(Extension_loaded('Xhprof')){3Include_once'/Usr/local/lib/PHP/xhprof_lib/utils/xhprof_lib.php';4Include_once'/Usr/local/lib/PHP/xhprof_lib/utils/xhprof_runs.php';5Xhprof_enable (xhprof_flags_cpu +Xhprof_flags_memory );6}

 

 1   If (Extension_loaded ('Xhprof' )){  2       $ Profiler_namespace = 'Myapp '; //  Namespace for your application  3       $ Xhprof_data = Xhprof_disable ();  4       $ Xhprof_runs = New  Xhprofruns_default ();  5      $ Run_id = $ Xhprof_runs -> Save_run ( $ Xhprof_data , $ Profiler_namespace  );  6    7       //  URL to the xhprof UI libraries (change the Host Name and Path)  8       $ Profiler_url = Sprintf ('HTTP: // myhost.com/xhprof/xhprof_html/index.php? Run = % S & source = % s ', $ Run_id ,$ Profiler_namespace  );  9       Echo '<A href = "'. $ Profiler_url . '"Target =" _ blank "> profiler output </a>' ;  10 }

 

Final configuration.Make the htaccess file automatically call the above two files

 
1Php_value auto_prepend_file/Document/gbyukg/Www/xhprof/Header.PHP2Php_value auto_append_file/Document/gbyukg/Www/xhprof/footer. php

 

Then access the index. php file under the xhprof_html directory.

 
//Start profilingXhprof_enable ();//Profile CPU time and/or memory usageXhprof_enable (xhprof_flags_cpu +Xhprof_flags_memory );//Run program......//Stop profiler$ Xhprof_data= Xhprof_disable ();
 1   //  Start profiling  2  Xhprof_enable ();  3   4   //  Run program  5 .... 6   7   //  Stop profiler  8   $ Xhprof_data = Xhprof_disable ();  9   10   // 11   // Saving the xhprof run  12   // Using the default Implementation of ixhprofruns.  13   //  14   Include_once   $ Xhprof_root . "/Xhprof_lib/utils/xhprof_lib.php" ;  15   Include_once   $ Xhprof_root . "/Xhprof_lib/utils/xhprof_runs.php" ; 16   17   $ Xhprof_runs = New  Xhprofruns_default ();  18   19   //  Save the run under a namespace "xhprof_foo ".  20   //  21   // ** Note **:  22   // By default save_run () will automatically generate a unique 23   // Run ID for you. [You can override that behavior by passing  24   // A run ID (optional Arg) to the save_run () method instead.]  25   //  26   $ Run_id = $ Xhprof_runs -> Save_run ( $ Xhprof_data , "Xhprof_foo ");

In the generated Statistical table, you can sort the data by different statistical columns, such:
Number of cballs: number of calls to a method
Memory usage: memory usage
Peak memory usage: Memory peak
Cup time: CPU usage time (including kernel usage time and userCodeCall time)
Wall time: execution time (if a remote call is executed, the time occupied by the CPU to access the remote server, parse the response, wait for the response, and other resources will be included)

Memory usage and CPU usage time are further divided into random and exclusive
Implicit Time includes the time occupied by the method and the total resources or time occupied by all the child methods of the method.
Exclusive time only contains the resources or time occupied by this method.


Graphic Interface

You can click the view full callgraph link at the top of the page to open the generated image model. Note that before using this function, ensure that graphviz has been installed in the system. You can run the following command to install it in ubunt:

 
1Apt-GetInstallGraphviz

To optimize the current project, we first need to sort the CPU to find out the method for optimizing the most CPU usage time, and then sort by the optimized memory usage, optimize the method with the highest memory usage, and then sort and optimize by wall time.

Difference and Statistical Report

Xhprof allows you to compare the two statistical results:

1HTTP://% Xhprof-ui-address %/index. php? Run1 = xxx & amp; run2 = YYY & amp; Source = MyApp

 

Xxx and YYY are run IDs,MyApp is the namespace, that is, the second parameter of save_run.

You can also combine multiple statistical results to generate a statistical report:

 
1HTTP://% Xhprof-ui-address %/index. php? Run = xxx, YYY, zzz & amp; Source = MyApp

 

Tip:
When optimizing a project, you must first know where optimization is required. For example, if you use Zend framewor in a project, you must first know the resources and time required to run the ZF framework, if ZF needs to occupy 2.5 MB of memory during running, it is impossible to occupy less than MB of memory for the entire project.
At the same time, you do not need to generate a statistical report every time you run it. You can modify the header file to run it 1000 times to generate a report.

Header. php

 1   $ Xhprof_on =False  ;  2   If ( Mt_rand (1, 10000) = 1 ){  3       $ Xhprof_on = True  ;  4       If ( Extension_loaded ('Xhprof' )){  5           Include_once '/Usr/local/lib/PHP/xhprof_lib/utils/xhprof_lib.php' ;  6           Include_once '/Usr/local/lib/PHP/xhprof_lib/utils/xhprof_runs.php' ;  7 Xhprof_enable (xhprof_flags_cpu + Xhprof_flags_memory );  8   }  9 }

 

Footer. php

 1   If ( $ Xhprof_on &&Extension_loaded ('Xhprof' )){  2       $ Profiler_namespace = 'Myapp '; //  Namespace for your application  3       $ Xhprof_data = Xhprof_disable ();  4       $ Xhprof_runs = New  Xhprofruns_default ();  5      $ Run_id = $ Xhprof_runs -> Save_run ( $ Xhprof_data , $ Profiler_namespace  );  6    7       //  URL to the xhprof UI libraries (change the Host Name and Path)  8       $ Profiler_url = Sprintf ('HTTP: // myhost.com/xhprof/xhprof_html/index.php? Run = % S & source = % s ', $ Run_id ,$ Profiler_namespace  );  9       Echo '<A href = "'. $ Profiler_url . '"Target =" _ blank "> profiler output </a>' ;  10 }

 

 

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.