Php class method online performance test

Source: Internet
Author: User
Two months ago a friend of a group asked a question, he said: "Now their company's project has a module performance is very poor, for a long time did not identify the problem, the boss rage let him all the class method execution time to the record analysis, and can not affect the current project performance." "The boss asked him to record the information in order to analyze where the performance was going to be, and where it would be removed for a period of time." This requirement leads to two questions, the first is how to listen to the module all the class method execution time, the second is how to do not affect the performance of the current project to complete (itself poor performance), the following two issues to analyze:

First, how to listen to this module execution time of all class methods

The first thing he thought about was that before all class methods were processed, add a piece of code to record the time, calculate the elapsed time before returning the data, and then log the logs. This method must be feasible, and the impact on performance is not very large, but ... Anyway, I will not do so, programmers are lazy, and are very smart, we are not yards, if so implemented, then to change the amount of code is too large, repeated for a long time no meaning no technical content of the work, followed by the deletion of the time again, I will run to collapse. Of course, a friend would not do so, so I know this wonderful demand.

How to solve, it was so difficult to solve the task, we can borrow __call () and __callstatic () to overload the class method, in php5.3 before static method can only add one by one, thanks to php5.3 added __callstatic () Magic method. Some people will ask this two magic methods are only useful when the class method does not exist, how can they achieve this demand? This problem will look at the code, the following analysis of the second question it.

Second, how can not affect the current project performance of the case to complete

Why do I say there is this problem? There was a time to analyze the requirements, but in answering the first question the solution was stated, and I think that using __call () and __callstatic () to overload the class method is relatively simple and has little impact on the performance of the existing project.

In this issue we mainly discuss some other methods, in fact, the implementation of the expansion of performance analysis is also a lot, xdebug, xhprof and so on, all know that xdebug performance loss is very large, not suitable for use in formal environment, xhprof performance loss is relatively small, can be used in the formal environment. Then why not xhprof it? There are three points: 1. The performance loss is slightly larger; 2. The logging format is not flexible, the analysis of the log is troubled; 3. Some functions are not statistically significant and can cause serious errors (such as: Call_user_func_array)

Now that we've identified the solution, let's start by looking at a demo.

/** * Class Method performance monitoring * * @author Bang  thinkercode@sina.com * @date2015 -05-31 22:09 */class Demo {/** * Normal class method * * @access Public * @return void */public function test1 () {for ($i =0; $i <100; + + $i) {}}/** * static method * * @access public * @return void */publ IC static function Test2 () {for ($i =0; $i <100; + + $i) {}}}

Look at the demo class above, where there are two methods, a common method, a static method, and the way our business layer is called is as follows:

 
  Test1 ();d emo::test2 ();

We guarantee that the principle is not to change the code outside of the class, just adjust the class to implement, I use __call () and __callstatic () to overload the class method.

   

In this code we add the __call () and __callstatic () methods, if only adding these two methods is useless, because the code of the previous business layer has not changed, the method of invocation is present, to make the calling method does not exist and only change the class itself, It is only possible to add an underscore to the method name (the rule itself), and then we call this two method to find out the execution time of the two methods.

This realization also found a few problems, the code is still a lot of changes, each class to add, very tired ...., in fact, the use of the tools at hand is very well implemented, using inheritance is a good way to write this two method into a base class, and then all classes inherit this base class. class method name substitution, in addition to the construction method and the destructor method, directly using the editor batch substitution can, later change back also.

Note: If you implement using inheritance, the __class__ of the __callstatic () method needs to be adjusted.

Here to add an underscore before the class method, so that the business layer cannot find the class method, in fact, this example can also adjust the method visibility to achieve, but the way of visibility implementation has the following drawbacks:

1. After the adjustment, I would like to adjust it back very likely to mistake the visibility of the class method, do not know which methods are adjusted.

2. Adjustment visibility is only valid for class public methods and is not available for protected and private methods

Of course, if you only record the performance of a class public method, you can use the change method visibility implementation, but you must remember to add annotations on the gaze the method is changed, and must be changed to private, because if the class inherits this class can not access this method.

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