Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Based on how to use the php performance testing tool xhprof in a production environment

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "based on how to use the php performance testing tool xhprof in the production environment", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "based on how to use the php performance testing tool xhprof in the production environment" this article.

Xhprof is an open source php performance testing tool from facebook, which can also be called profile tool. I don't know how to translate this word. Compared with xdebug, which has been used before, there are many similarities. In the past, there are some records of xdebug for reference, but its disadvantage is that it has a great impact on performance, even if the profiler_enable_trigger parameter is enabled, it is terrible to be used in a production environment, and cpu immediately reaches high.

On the other hand, xhprof appears to be very lightweight, and whether to record profile can be controlled by the program, so it is possible to use it in a production environment. You can see this usage in its documentation:

With a 1/10000 chance to enable xhprof, usually quietly do not shoot.

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

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 be saved at the end of the program, thus eliminating the end judgment and giving an incomplete example of rewriting:

The copy 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 the log, for the time being, I use the most corny file form to save it, which can be cleared regularly.

The graphics generated by BTW:xhprof profile is really cool, which piece of code has become a bottleneck, it is clear at a glance.

Tentative ideas on improving the use of xhprof

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

problem

Xhprof's profile logs are saved directly as files on the production server and need to be cleaned regularly or collected and moved to the tool machine where the logs are viewed.

Because the profile generated by xhprof is a large array, the standard php serialize is used when saving to the file, and the log file is too large, so it is easy to take up a lot of server disk space if you are not careful.

When viewing the list of logs, it is more difficult to click on them one by one.

In view of these problems, I have some 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 client of scribe, which automatically synchronizes to the scribe of the central log server and does not occupy local storage space. The change in the code is relatively small, as long as a XhprofRuns class is implemented based on the iXHProfRuns interface and the storage mode of the save_run method can be adjusted.

Change the serialization method

By default, xhprof saves the profile information after being processed with the native serialization method of php. I compared the performance and the number of bytes occupied by igbinary vs serialize vs json_encode two days ago. In this test, igbinary has certain advantages in all aspects, especially the storage space will be greatly reduced, so I only need to change the serialization method to igbinary_serialize to get improvement.

Optimize list display

I'm tired of looking at the big pictures of profile logs one by one, which is time-consuming and untargeted. So what I'm doing now is to output the overall execution time of the first 1000 logs directly to the list in the list of profile logs, and identify logs that take too long to execute in red and bold. After making this small change, when I want to check the operation, just click on the red links in the log list, which really saves time and effort.

How to get the execution time from the xhprof log file? The simple code is as follows

The copy code is as follows:

/ * *

* obtain the execution time from the xhprof log

*

* @ param string $log xhprof log file path

* @ return int execution time

, /

Function getSpentTime ($log) {

$profile = unserialize (file_get_contents ($log))

Return $profile ['main ()'] ['wt'] / 1000

}

The above is all the contents of the article "based on how to use the php performance testing tool xhprof in a production environment". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report