Home > Backend Development > PHP Tutorial > Detailed explanation based on using PHP performance testing tool xhprof in production environment_PHP tutorial

Detailed explanation based on using PHP performance testing tool xhprof in production environment_PHP tutorial

WBOY
Release: 2016-07-21 15:09:40
Original
868 people have browsed it

xhprof is a PHP performance testing tool open sourced by Facebook. It can also be called a profile tool. I don’t know how to translate this word to make it more meaningful. Compared with the xdebug I have been using before, there are many similarities. There were some records of xdebug in the past that can be used for reference, but its disadvantage is that it has a great impact on performance. Even if the profiler_enable_trigger parameter is turned on, it is terrible when used in a production environment, and the CPU will immediately reach high.
xhprof is very lightweight, and whether to record profiles can be controlled by the program. Therefore, it is possible to use it in a production environment. You can see such a usage in its documentation:
Enable xhprof with a one-in-10,000 chance, and don’t shoot it quietly at ordinary times.

Copy code 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
Copy code 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 specify to save the xhprof information at the end of the program, thus eliminating the need to judge at the end and give it a rewrite Incomplete example:
Copy code The code is as follows:

if (mt_rand(1, 10000) == 1) {
xhprof_enable (XHPROF_FLAGS_MEMORY); For the time being, I use the most unsophisticated file format to save it, and just clear it regularly.
BTW: The graphical profile generated by xhprof is really cool. It is clear at a glance which piece of code has become the bottleneck.
by phpe: Here is an example:




Ideas on improving the use of xhprof

Since xhprof was used in the production environment last year, program debugging and performance optimization in the production environment have brought Lots of conveniences. However, there are still some details that need to be improved during use.

Problem

The profile log of xhprof is saved directly in the form of a file on the production server. It needs to be cleaned regularly, or collected and moved to a tool machine for viewing the log. Since the profile generated by xhprof is a large array, standard php serialize is used when saving it to the file. The log file is relatively large, and it is easy to occupy a lot of server disk space if you are not careful. When viewing the log list, it is difficult to click on each one to view them.
I have some small ideas regarding these issues.

Log storage


Deploy a central log server and use facebook's scribe to collect logs. The xhprof logs generated by the server in the production environment are written to the scribe client, and the client automatically synchronizes them to scribe on the central log server, without occupying local storage space. The changes in the code are also relatively small. Just implement an XhprofRuns class based on the iXHProfRuns interface and adjust the storage method of the save_run method.
Change the serialization method

xhprof defaults to processing the profile information using PHP’s native serialization method before saving it, and I compared igbinary vs two days ago The performance and number of bytes occupied by serialize vs json_encode. In this test, igbinary has certain advantages in all aspects, especially the storage space occupied will be greatly reduced, so I only need to change the serialization method to igbinary_serialize to get improvement.
Optimize list display

I am tired of looking at the big pictures of profile logs one by one. It is time-consuming and labor-intensive and not targeted. So what I am doing now is to directly output the overall execution time of the first 1000 logs in the list of profile logs to the list, and mark the logs with too long execution times in red boldface. After making this small change, when I want to check the operation status, I just click on the red links in the log list to take a look, which really saves time and effort. How to get execution time from xhprof log file? The simple code is as follows
Copy the code

The code is as follows:


/**
* Get execution time from xhprof log
*
* @param string $log file path of xhprof log
* @return int execution time
*/
function getSpentTime($log) {
$profile = unserialize(file_get_contents($log));
return $profile['main() ']['wt'] / 1000;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327204.htmlTechArticlexhprof is a PHP performance testing tool open sourced by Facebook. It can also be called a profile tool. I don’t know this word. How to translate it more expressively. Compared with the xdebug I have been using before,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template