This article is a detailed analysis and introduction to the use of the PHP performance testing tool xhprof in a production environment. Friends who need it can refer to 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 express its meaning. Compared with the xdebug I have been using before, there are many similarities. In the past, there were some records of xdebug 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 becomes 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 usually do not shoot quietly.
Copy the codeThe code is as follows:
if (mt_rand(1, 10000) == 1) {
xhprof_enable(XHPROF_FLAGS_MEMORY);
$xhprof_on = true;
}
Copy the codeThe code is as follows:
if ($xhprof_on) {
// stop profiler
$xhprof_data = xhprof_disable();
// save $xhprof_data somewhere (say a central DB)
...
}
Copy code The code is as follows:
if (mt_rand(1, 10000) == 1) {
data;")) ;
}
Ideas about improving the use of xhprof
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, there are still some details that need to be improved during use.
Problem
The profile log of xhprof is saved directly on the production server in the form of a file. It needs to be cleaned regularly, or collected and moved to a tool machine for viewing logs. Since the profile generated by xhprof is a large array, standard php serialize is used when saving 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.
In response to these questions, I have some small ideas.
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 and then saving it. In the past two days, I compared the performance and occupancy of igbinary vs 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 codeThe code is as follows:
/**
* Obtain the execution time from the 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;
}