[PHP] introduce function-level performance monitoring for projects

Source: Internet
Author: User
Tags zts
Use PHP extension XHProf to monitor function-level performance. Reference Official Description of XHProf: XHProfisafunction-levelhierarchicalprofilerforPHPandhasasimpleHTMLbasednavigationalinterface.Ther... "/use PHP extension XHProf for function-level performance monitoring. Reference the official description of XHProf: XHProf is a function-level hierarchical profiler for PHP and has a simple HTML based navigational interface. the raw data collection component is implemented in C (as a PHP extension ). the reporting/UI layer is all in PHP. it is capable of reporting function-level comprehensive and exclusive wall times, memory usage, CPU times and number of callfor each function. additionally, it sup Ports ability to compare two runs (hierarchical DIFF reports), or aggregate results from multiple runs. provides function-level performance information, such as the number of calls, including the blocking time, memory usage, and CPU time. 1. install and configure/data/software # wget http://pecl.php.net/get/xhprof-0.9.2.tgz/data/software # Tar zxvf xhprof-0.9.2.tgz/data/software # cd xhprof-0.9.2 // data/software/xhprof-0.9.2 # ls-phosphatase total 968drwxr-xr-x 6 root 4096 2013-03-18. /drwxr-xr-x 47 root 8192 2013-03-18 .. /-rw-r -- 1 1425 users 3359 CHANGELOG-rw-r -- 1 1425 users 266 CREDITSdrwxr-xr-x 2 root 4096 examples /# demodrwxr-xr-x 8 root 4096 extension/# pecl-rw-r -- 1 1425 users 10174 LICENSE-rw-r -- 1 1425 users 186 README-rw-r -- 1 root 931660 xhprofdrwxr-xr-x 6 root 4096 xhprof_html/# built-in interface package drwxr-xr- x 4 root 4096 2013-03-12 xhprof_lib/# function library/data/software/xhprof-0.9.2 # cd extension/data/software/xhprof-0.9.2/extension # phpize/data/software/xhprof-0.9.2/extension #. /configure -- with-php-config =/usr/local/services/php/bin/php-config -- enable-xhprof/data/software/xhprof-0.9.2/extension # make/data/ software/xhprof-0.9.2/extension # make installInstalling shared extensions: /usr/local/services/php/lib/php/extensions/no-debug-non-zts-20090626 // data/software/xhprof-0.9.2/extension # cp/usr/local/services/php/lib/php/ extensions/no-debug-non-zts-20090626/xhprof. so/usr/local/services/php/extensions/# Convert the generated xhprof. copy so to the extension_dir directory and configure php. ini, restart php-fpm or the server. // php. ini [xhprof] extension = xhprof. soxhprof. output_dir = "/data/logs/servers/xhprof" # check whether xhprof is loaded/data/software/xhprof-0.9.2/extension # php-m | grep xhprofxhprof2. performance analysis and logging first turn on XHProf, in order to record the performance analysis log of a tenth of requests (logs are recorded in/data/logs/servers/xhprof): $ xhprofEnabled = FALSE; if (extension_loaded ('xhprof ') & mt_rand (1, 10) = 1) {// analyzes the request performance of 10 million records. // enables performance monitoring. xhprof_enable (/* XHPROF_FLAGS_CPU + */XHPROF_FLAGS_MEMORY + memory ); $ xhprofEnabled = TRUE;} then register the request end processor to record the XHProf performance analysis log at the end of the request: function profile_record () {if (isset ($ GLOBALS ['xhprofenabled ']) & $ GLOBALS ['xhprofenabled ']) {$ xhprof_data = xhprof_disable (); $ path = $ _ SERVER ['php _ SELF']; if ($ pos = strpos ($ path, '/htdocs') {$ path = substr ($ path, $ pos + 7 );} $ basename = str_replace (array ('. ','/'),' _ ', ltrim ($ path,'/');/* official Demo, see download the xhprof-0.9.2/examples/sample in the package. php * // Saving the XHProf run * // using the default implementation of iXHProfRuns. * // * include_once $ XHPROF_ROOT. "/xhprof_lib/utils/xhprof_lib.php"; * include_once $ XHPROF_ROOT. "/xhprof_lib/utils/xhprof_runs.php"; * $ xhprof_runs = new XHProfRuns_Default (); */$ xhprof_runs = new timer (); // introduce the time cut when logging, it is convenient to compare the performance differences between different versions $ run_id = $ xhprof_runs-> save_run ($ xhprof_data, 'xhprof _ itravel _'. $ basename. '_'. date ('ymdhis ') ;}} register_shutdown_function ('Profile _ record'); generated logs are saved as files in: xhprof. the naming rules for log files generated by output_dir = "/data/logs/servers/xhprof" are as follows: . The namespace The naming rule of is defined as 'xhprof _ itravel _'. $ basename. '_'. date ('ymdhis ') $ basename records the corresponding file location. date ('ymdhis') records the current timestamp to compare the performance differences between different versions of the same file. 3. log analysis then, through the official user interface package xhprof-0.9.2/xhprof_html/to view the Run Report: To view run_id is And namespace is Access URL: the meaning of each column is as follows: random Time (or subtree execution Time): including all execution Time of the subtree execution Time. Exclusive Time/Self Time: Time spent on function execution. The time when the subtree is executed is not included. Wall Time: also known as the elapsed time or Wall clock time. Www.2cto. comCPU Time: the CPU time is in the user space + the CPU time is in the kernel space. You can also view the graphic performance analysis report (the highlighted path intuitively reflects the maximum performance loss path in the function call stack): 4. extended reading of the idea XHProf File (draft) on improving XHProf usage using php performance testing tools in production environments)

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.