Based on the xhprof of the PHP Performance test tool in a production environment. Performance test Tool Web performance test Tool network performance tester

Source: Internet
Author: User
This article is a detailed analysis of the use of PHP performance testing tools in the production environment xhprof, the need for a friend reference

Xhprof is a PHP performance testing tool that is open source for Facebook, and it can also be called the Profile tool, a word that doesn't know how to translate. There are many similarities compared to the xdebug that have been used before. Previously there are some records of xdebug can be used for reference, but its disadvantage is that the performance impact is too large, even if the Profiler_enable_trigger parameters are opened, used in the production environment is also appalling, the CPU immediately soared to high.
And Xhprof is very light, whether the record profile can be controlled by the program, therefore, in the production environment is also a possibility. You can see a usage of this in its documentation:
One out of 10,000 chance to enable xhprof, usually quietly do not shoot.

Copy the Code code as follows:


if (Mt_rand (1, 10000) = = 1) {
Xhprof_enable (xhprof_flags_memory);
$xhprof _on = true;
}


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

Copy the Code code 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 specify that the XHPROF information is saved at the end of the program, thus eliminating the end of judgment and giving an incomplete example of rewriting:

Copy the Code code 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 the log, I temporarily use the most soil file form to save, regular cleanup can be.
Btw:xhprof generated graphical mode profile is really cool, which code becomes a bottleneck, at a glance.
By PHPE: Here is an example diagram:


Ideas on improving the use of xhprof
Since last year's use of xhprof in the production environment, the production environment of the program debugging, performance optimization has brought a lot of convenience. However, there are still some details that need to be improved in the course of use.
Problem
XHPROF profile logs are stored directly on the production server as files, need to be cleaned up periodically, or collected to be moved to the tool machine where the logs are viewed.
Because the xhprof generated by the profile is a large array, so saved to the file using the standard PHP serialize, log file is large, one is not careful to occupy a lot of server disk space.
When viewing a list of logs, it's hard to see each point.
In response to these questions, I have a few small ideas.
Log storage
Deploy a central log server that uses Facebook's scribe to collect logs. The xhprof logs generated by the server in the production environment are written to the scribe client, which is automatically synchronized by the client to the scribe on the central log server, and does not occupy local storage space. Changes in the code is also relatively small, as long as the implementation of a Xhprofruns class based on the Ixhprofruns interface, adjust the Save_run method of storage.
Replacing serialization methods
Xhprof By default is to save the profile with the PHP native serialization method, and I compare the performance and number of bytes of igbinary vs Serialize vs Json_encode in the first two days. In this test igbinary has certain advantages in all aspects, especially the storage space will be greatly reduced, so I just need to change the serialization method for Igbinary_serialize can be improved.
Optimize list Display
I'm tired of looking at the big picture of the profile log, which is time-consuming and not targeted. So what I do now is that the overall execution time of the first 1000 logs is output directly to the list in the list of the profile log, and the log that takes too long to execute is identified in bold red. After making this little change, when I want to inspect the operation, I will take a look at all the red links in the log list, and the real saving time and effort.
How do I get the execution time from the Xhprof log file? The simple code is as follows

Copy the Code code as follows:


/**
* Execution time is obtained by xhprof log
*
* @param string $log The file path of the Xhprof log
* @return int Execution time
*/
function Getspenttime ($log) {
$profile = Unserialize (file_get_contents ($log));
return $profile [' main () ' [' wt ']/1000;
}

The above describes the use of PHP performance testing tools based on the xhprof in the production environment, including the XHPROF, performance testing tools, I hope to be interested in PHP tutorial friends helpful.

  • 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.