CI Framework Source Reading Note 5 Benchmark benchmark.php_php tutorial

Source: Internet
Author: User
Tags benchmark

CI Framework Source Reading Note 5 Benchmark test benchmark.php


Since benchmark is the first core component to be loaded in CI, our analysis begins with that component first. The meaning of benchmark is very clear, the students who have used the benchmark tool should be more clear, this is a benchmark component. Since it is a benchmark, we can boldly assume that the main function of the BM component is to log the program running time, memory usage, CPU usage and so on.  The structure of this component is simpler, there is only one marker internal variable and three external interfaces: 1 elapsed_time2 Mark3 memory_usage under each expansion: 1. The signature of the Mark function is: function mark ($name), which takes a string parameter, and the implementation is simpler, with only one sentence: $this->marker[$name] = Microtime (); That is, this function is only used to record the point in time of the function call. It is important to note that because of the special handling in the controller (which we will explain in detail later), you can use $this->benchmark->mark ($name) in your application controllers to add a running point in time, for example: $this- >benchmark->mark ("Function_test_start"), $this->_test (); $this->benchmark->mark ("Function_test_ End ");p Rint_r ($this->benchmark), where Function_test_start and Function_test_end are used to record the results of a point in time at which the function call begins and ends:  Now to calculate the call time for the function, you need to use the second function of the benchmark component Elapsed_time 2. The signature of the Elapsed_time function is: function elapsed_time ($point 1 = ", $point 2 =", $decimals = 4) 3 parameters are optional (1). If $point1 is empty, return ' {elapsed_time} ' if ($point 1 = = ') {return ' {elapsed_time} ';} Nani! Clearly should return is the time, how instead of return is a string, and so strange (like Smarty label). In fact, in the output component, {elapsed_time} is replaced, we temporarilyTake a look at how to replace it: $elapsed = $BM->elapsed_time (' Total_execution_time_start ', ' total_execution_time_end '); $output = Str_ Replace (' {elapsed_time} ', $elapsed, $output); that is, if you do not specify a parameter, calling the function actually gets total_execution_time_start this point in time to Total_ Execution_time_end the time difference at this point. Further, since Total_execution_time_start is the first mark Point set after the BM is loaded (Total_execution_time_end is not defined and returns the current point in time), The actual return of the function is the load and run time of the system. (2). If the call is an unknown mark point. The result is unknown, directly returning null: if (! isset ($this->marker[$point 1])) {return ';} (3). If the mark point for $point2 is not set, the $point2 's Mark Point is set to the current point in time. if (! isset ($this->marker[$point 2])) {$this->marker[$point 2] = Microtime ();} (4). The time difference between the last two mark points returned: List ($SM, $ss) = Explode (', $this->marker[$point 1]), List ($em, $es) = Explode (", $this->ma rker[$point 2]); Return Number_format (($em + $es)-($SM + $ss), $decimals); also look at the previous example, here we can call by: Echo $this->benchmark->elapsed_time ("Function_test_start", "Function_test_end"), gets the execution time of the function. 3. Memory_usage This function returns the memory usage (MB units) of the system, and {Elapsed_time}, the returned {memory_usage} of this function is also replaced in output: $memOry = (! function_exists (' memory_get_usage '))? ' 0 ': Round (Memory_get_usage ()/1024/1024, 2). ' MB '; $output = Str_replace (' {memory_usage} ', $memory, $output); Because the benchmark component itself is simpler, we do not explain more. Finally, paste the source code of this component: copy marker[$name] = Microtime (); }/** * Calculates the time difference between and marked points. * If The first parameter is empty this function instead returns the {elapsed_time} pseudo-variable. This permits the full system * @access public * @param string a particular marked point * @param string a particular Marke D Point * @param integer the number of the decimal places * @return Mixed */function elapsed_time ($point 1 = ", $point 2 =", $decimals = 4) {if ($point 1 = = ") {return ' {elapsed_time} ';} if (! isset ($this->marker[$point 1]) {return ';} if (! isset ($this->marker[$point 2])) {$this->marker[$point 2] = Microtime ();} list ($SM, $ss) = Explode ("', $this ->marker[$point 1]); List ($em, $es) = Explode (", $this->marker[$point 2]); Return Number_format (($em + $es)-($SM + $ss), $decimals); }/** * Memory Usage * This function returns the {memory_usage} pseudo-variable. */function Memory_usage () {return ' {memory_usage} ';}}

http://www.bkjia.com/PHPjc/907367.html www.bkjia.com true http://www.bkjia.com/PHPjc/907367.html techarticle CI Framework Source reading Note 5 Benchmark benchmark.php since BenchMark is the first core component to be loaded in CI, our analysis begins with that component first. The meaning of benchmark is very clear ...

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