CI Framework Source Reading Note 5 Benchmark benchmark.php

Source: Internet
Author: User
Tags array benchmark empty explode return string variable access
Since benchmark is the first core component to be loaded in CI, our analysis begins with the component first. The meaning of benchmark is very clear, students who have used benchmark tools should be more aware that this is a benchmark component. Since it is benchmark, we can boldly guess, BM components of the main function is to record the running time of the program, memory usage, CPU use and so on.      The component structure is simpler, with only one marker internal variable and three external interfaces:   1 elapsed_time 2 Mark 3 memory_usage under each start look:   1. Mark   function is signed by:   function mark ($name) It takes a string type of argument, and the implementation is simpler, with one sentence:   $this->marker[$name] = Microtime (); That is, this function is just a point in time when the function call is recorded.   Note that because of the special processing in controller (which we'll explain later), you can use $this->benchmark->mark ($name) in your application controller to add run-time points, such as:   $this->benchmark->mark ("Function_test_start"); $this->_test (); $this->benchmark->mark ("Function_test_end"); Print_r ($this->benchmark); Where Function_test_start and function_test_end are used to record the start and end of a function call at the point of time   printout results:       Now calculates 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 parameters   (1).   If $point1 is empty, return ' {elapseD_time} '   if ($point 1 = = ') {     return ' {elapsed_time} ';} Nani! It should be time to return, how to return is a string, and so strange (similar to the Smarty label). In fact, in the output component, {Elapsed_time} will be replaced, we temporarily look at the way to replace:   $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, the call to the function actually gets the time difference between this total_execution_time_start point and the total_execution_time_end. Further, since Total_execution_time_start is the first mark Point set after BM is loaded (Total_execution_time_end is not defined and returns the current point of time), The actual return of the function is the loading and running time of the system.   (2). If the call is an unknown mark point. The result is unknown, and returns null directly:   if (! isset ($this->marker[$point 1])) {    return ';} (3). If you do not set the $point2 Mark Point, set the mark point of the $point2 to the current point in time.   IF (! isset ($this->marker[$point 2]) {    $this->marker[$point 2] = Microtime ();} (4). Time difference of the last two mark points returned:   List ($SM, $ss) = Explode (', $this->marker[$point 1]); List ($em, $es) = Explode (', $this->marker[$point 2]);   Return Number_format ($em + $es)-($SM + $ss), $decimals); Also look at the previous example where we can invoke:   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 system memory usage (MB unit), and as with {Elapsed_time}, the {memory_usage} returned by 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); Since the benchmark component itself is simpler, we do not do more explaining.   Finally, post the source code of this component:   Copy code <?php   class Ci_benchmark {     /**      * List All benchmark markers and then they were added      *      * @var array     &NB sp;*/    var $marker = array ();      /**      * Set A benchmark marker      *      * @access    public      * @param    string     $name    name of the Marker &nbsp    * @return    void      */    function mark ($name)     {        $this->marker[$name] = Microtime ();    }      /**      * calculates the time difference between two marked points.      * If The parameter is empty this function instead returns the {elapsed_time} pseudo-variable. This is permits the full system      * @access    public      * @param    s Tring    a Particular marked point      * @param    string    a particular MA rked Point      * @param    integer    the number of decimal places     &NBSP ; * @return    mixed      */    function elapsed_time ($point 1 = ', $point 2 = ', $deci Mals = 4     {        if ($point 1 = "]   &NBSp     {            return ' {elapsed_time} ';           &N Bsp       if (! isset ($this->marker[$point 1])         {        &N Bsp   return ";        }           if (! isset ($this->marker[$point 2]))     &N Bsp   {          $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 th e {memory_usage} pseudo-variable.      */    function Memory_usage ()     {        return ' {memory_usage} ';    }  }

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.