Online performance testing of php Methods
A friend from a group asked a question two months ago. He said, "Now their company's project has a module with poor online performance and has not found the problem for a long time, in anger, the boss asked him to analyze the execution time of all class methods for the record, and could not affect the current project performance." The boss asked him to record the information in order to analyze the specific places that affect performance and remove it after the project runs for a period of time. This requirement leads to two problems. The first is how to monitor the execution time of all class methods in this module, second, how can we achieve this without affecting the performance of the current project (its performance is poor)? Let's analyze these two problems:
1. How to monitor the execution time of all class methods in this module
In this case, he first thought of adding a code record time before processing all class methods, calculating the running time before returning the data, and then recording the log. This method must be feasible and has little impact on performance, ...... I will not do this anyway. Programmers are both lazy and intelligent. We are not coders. If so, the amount of code to be modified is too large, repeat the work for a long time without any technical significance. I will try again when I delete it later. Of course, a friend would not do the same, so I realized this wonderful demand.
How can we solve this problem? It is really difficult to solve this problem. We can use _ call () and _ callStatic () to reload class methods, before php5.3, the static method can only be added one by one. Thanks to php5.3 for adding the _ callStatic () magic method. Some people may ask how these two magic methods are useful when the class methods do not exist? This issue will be followed by the Code. Let's analyze the second issue below.
2. How can we achieve this without affecting the performance of the current project?
Why do I say this is the case? I did have some requirements before, but I have already mentioned the solution when answering the first question. In my opinion, we use _ call () and _ callStatic () it is easy to implement and has little impact on the performance of existing projects.
We will mainly discuss other methods in this issue. In fact, there are also many extensions for performance analysis. xdebug and xhprof all know that xdebug has a high performance loss and is not suitable for use in formal environments, the performance loss of xhprof is relatively small and can be used in the official environment. Why not use xhprof? There are three points: 1. performance Loss is slightly higher; 2. the format of log records is not flexible, causing log analysis problems. 3. some functions cannot be counted, and serious errors may occur (such as: call_user_func_array)
Now that you have determined the solution, let's take a look at a demo first.
/*** Class method performance listener ** @ author Qi silver thinkercode@sina.com * @ date2015-05-31 */class demo {/*** common class method ** @ access public * @ return void */public function test1 () {for ($ I = 0; $ I <100; ++ $ I) {}}/*** static method ** @ access public * @ return void */public static function test2 () {for ($ I = 0; $ I <100; ++ $ I ){}}}
Take a look at the demo class above. There are two methods, one common method and one static method. The Calling method at the business layer is as follows:
test1();demo::test2();
We guarantee that the principle is not to change the code other than the class, but to adjust the class for implementation. Below I will use _ call () and _ callStatic () to overload the class method.
In this Code, we added the _ call () and _ callStatic () methods. It is useless to add only these two methods because the code at the business layer has not changed, the called method exists. To ensure that the called method does not exist and only changes the class itself, you must add an underscore before the method name (this rule is set by yourself ), then, when we call these two methods, we find that the execution time of the two methods is output.
In this way, several problems are also discovered. There are still a lot of changed code, and each class needs to be added. It is very tired ............, In fact, it is good to make good use of the tools at hand. Using inheritance is a good way to write these two methods into a base class, and then all classes inherit this base class. Class Method Name replacement, in addition to the constructor and destructor, you can directly use the editor to replace them in batches. It will also be changed back later.
Note: If you use the inheritance method, _ CLASS _ of the __callstatic () method needs to be adjusted.
Here, we add an underline to the class method so that the business layer cannot find the class method. In fact, this example can also adjust the method visibility. However, the visibility implementation method has the following drawbacks:
1. After the adjustment, it is very likely that the class method visibility will be wrong and you do not know which methods have been adjusted.
2. Adjusting visibility is only effective for public-class methods and unavailable for protected and private methods
Of course, if you only record the performance of public-class methods, you can change the method visibility. However, you must remember that adding the Annotation on the gaze method has changed, and the public method must be changed to private, because if a class inherits this class, you cannot access this method.