Detailed explanation of xhprof, a php performance testing tool, in the production environment _ PHP Tutorial

Source: Internet
Author: User
Based on the detailed description of using the php performance testing tool xhprof in the production environment. Xhprof is a php performance testing tool open-source by facebook. It can also be called the profile tool. it doesn't know how to translate it to make it more satisfactory. Compared with xdebug, xhprof is an open-source php performance testing tool developed by facebook. It can also be called the profile tool. the word "xhprof" is easy to translate. There are many similarities with xdebug. In the past, there were some records of xdebug for reference, but its disadvantage was that it had a great impact on performance. even if the profiler_enable_trigger parameter was enabled, it was terrible to use it in the production environment, cpu hits high immediately.
Xhprof is very lightweight, and whether or not to record the profile can be controlled by a program makes it possible to use it in a production environment. You can see this usage in its document:
Xhprof is enabled with a probability of one thousandth in a row, and you do not need to shoot a gun.

The code is as follows:


If (mt_rand (1, 10000) = 1 ){
Xhprof_enable (XHPROF_FLAGS_MEMORY );
$ Xhprof_on = true;
}


Call the method at the end of the program to save the profile

The code is as follows:


If ($ xhprof_on ){
// Stop profiler
$ Xhprof_data = xhprof_disable ();

// Save $ xhprof_data somewhere (say a central DB)
...
}


You can also use the register_shutdown_function method to save the xhprof information at the end of the program. This removes the need to judge the end of the program and provides an incomplete example of rewriting:

The code is as follows:


If (mt_rand (1, 10000) = 1 ){
Xhprof_enable (XHPROF_FLAGS_MEMORY );
Register_shutdown_function (create_funcion (''," $ xhprof_data = xhprof_disable (); save $ xhprof_data ;"));
}


As for logs, I am currently using the most earthy file format for storage. clear the logs regularly.
BTW: The graphic profile generated by xhprof is really cool. which code segment becomes the bottleneck and is clear at a glance.
By phpe: The following is an example:



Ideas on improving xhprof usage
Since xhprof was used in the production environment last year, it has brought a lot of convenience to program debugging and performance optimization in the production environment. However, some details need to be improved during use.
Problem
The profile log of xhprof is directly stored on the production server as a file, which needs to be cleaned regularly or collected to the tool machine for viewing logs.
Because the profile generated by xhprof is a large array, the standard php serialize is used for saving to the file. the log file is too large, so it is easy to occupy a lot of server disk space without leaving your mind.
It is difficult to view the log list one by one.
I have some small ideas to address these issues.
Log storage
Deploy a central log server and use facebook's scribe to collect logs. Xhprof logs generated by servers in the production environment are written to the scribe client and automatically synchronized from the client to the scribe of the central log server without occupying local storage space. The changes to the code are also relatively small. you only need to implement an XhprofRuns class based on the iXHProfRuns interface and adjust the storage method of the save_run method.
Change serialization method
By default, xhprof saves profile information after being processed using php native serialization methods. I compared the performance and occupied bytes of igbinary vs serialize vs json_encode two days ago, in this test, igbinary has some advantages in all aspects, especially the storage space usage will be greatly reduced, so I only need to change the serialization method to igbinary_serialize to improve.
Optimized list display
I'm tired of looking at the big picture of profile logs, which is time-consuming, laborious, and not targeted. So my current practice is to directly output the overall execution time of the first 1000 logs to the list in the profile log list, and mark the logs that have been executed for too long in red bold. After this small change, when I want to check the running status, I just need to click on the red links in the log list to see it, which saves time and effort.
How can I obtain the execution time from the xhprof log file? The simple code is as follows:

The code is as follows:


/**
* The execution time is obtained by The xhprof log.
*
* @ Param string $ log xhprof file path
* @ Return int execution time
*/
Function getSpentTime ($ log ){
$ Profile = unserialize (file_get_contents ($ log ));
Return $ profile ['main () '] ['WT']/1000;
}


Http://www.bkjia.com/PHPjc/327204.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327204.htmlTechArticlexhprof is facebook open-source php performance testing tool, can also be called profile tool, the word does not know how to translate is more satisfactory. Compared with xdebug ,...

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.