CI Framework Source Reading Note 5 Benchmark test benchmark.php,cibenchmark.php
Previous blog (CI framework source reading Note 4 boot file codeigniter.php), we have seen: CI in the core process of the core functions are done by different components. These components are similar to a single module, different modules to complete different functions, the modules can be called each other, together constitute the core skeleton of CI.
From the beginning, will be further analysis of the implementation of the details of the components, in-depth CI core black box inside (after the study, in fact, it should be a white box, only for the application, it should be considered black box), so as to better understand and grasp the framework.
By convention, before we begin, we stick to the incomplete core component diagram in CI:
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.
Look at the class diagram first:
The component structure is simpler, with only one marker internal variable and three external interfaces:
1 Elapsed_time 2 Mark 3 Memory_usage
The following unfolds to see:
1. Mark
The signature of the function is:
function mark ($name)
This function accepts a string parameter, and the implementation is simpler, with only one sentence:
$this->marker[$namemicrotime();
That is, this function is only used to record the point in time of the function call.
It is worth noting that due to 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);
whereFunction_test_start and function_test_end are used to record the point in time at which function calls start and end, respectively
Print out the results:
Now to calculate the call time for the function, you need to use the second function of the benchmark component Elapsed_time
2. Elapsed_time
The signature of the 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, and we'll take a moment to 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);
In other words, without specifying a parameter, invoking the function actually gets the time difference between this point of time and the point at which the Total_execution_time_start is total_execution_time_end . 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 and returns null directly:
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->marker[$point 2]); return Number_format (($em + $es)-($SM + $ss), $decimals);
Also look at the previous example, where 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, like {elapsed_time} , the {memory_usage} returned by this function is also replaced in output:
$memory function_exists round (Memory_get_usage ()/1024/1024, 2). ' MB '; $output Str_replace $memory $output);
Since the benchmark component itself is simpler, we do not make more explanations.
Finally, paste the source code for this component:
PhpclassCi_benchmark {/** * List of all benchmark markers and when they were added * * @var array*/ var $marker=Array(); /** Set A benchmark marker * * @access public * @param string $name name of the marker * @return void*/ functionMark$name) { $this->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 "full" system * @access public * @param string a particular marked point * @param s Tring a particular marked point * @param integer the number of the decimal places * @return Mixed */ functionElapsed_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. */ functionMemory_usage () {return' {memory_usage} '; }}
How do you usually do unit testing using the CI framework of PHP? Is it better to use CI or phpunit?
PHPUnit
CI Framework for MySQL version what to use MYSQL55 will error
Not the newest mysql.ci is currently used very normally.
http://www.bkjia.com/PHPjc/907043.html www.bkjia.com true http://www.bkjia.com/PHPjc/907043.html techarticle CI Framework Source reading Note 5 benchmark benchmark.php,cibenchmark.php on a blog (CI framework source reading Note 4 boot file codeigniter.php), we have seen: CI kernel ...