CI framework source code reading notes 5 BenchMark test BenchMark. php, cibenchmark. php
In the previous blog (CI framework Source Code Reading Note 4 guide file CodeIgniter. php), we have seen that the core functions of the CI process are completed by different components. These components are similar to a single module. Different modules implement different functions, and each module can call each other to form the core skeleton of CI.
Starting from this article, we will further analyze the implementation details of each component and go deep into the black box of the CI core (after research, it should actually be a white box, just for the application, it should be regarded as a black box) to better understand and grasp this framework.
By convention, before we start, we paste the incomplete Core Component diagram in CI:
As BenchMark is the first core component loaded in CI, our analysis begins with this component. The meaning of BenchMark is very clear, and those who have used the BenchMark tool should be clear. This is a BenchMark component. Since it is BenchMark, We can boldly guess that the main function of the BM component is to record the running time, memory usage, cpu usage, and so on of the program.
First look at the class diagram:
This component has a simple structure, with only one internal marker variable and three external interfaces:
1 Elapsed_time2 Mark3 Memory_usage
Let's look at them one by one:
1. mark
The function signature is:
function mark($name)
This function accepts a string-type parameter, which is simpler to implement. There is only one sentence:
$this->marker[$name] = microtime();
That is to say, this function is only used to record the time point at which the function is called.
It is worth noting that, due to special processing in the Controller (We will explain in detail later), you can use $ this-> benchmark-> mark ($ name) in your application Controller ); for example:
$this->benchmark->mark("function_test_start");$this->_test();$this->benchmark->mark("function_test_end");print_r($this->benchmark);
Function_test_start and function_test_end are used to record the start time and end time of the function call respectively.
Output result:
To calculate the call time of a function, you need to use the second function elapsed_time of the BenchMark component.
2. elapsed_time
The function signature is:
function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
All three parameters are optional.
(1). If $ point1 is null, '{elapsed_time}' is returned }'
if ($point1 == '') { return '{elapsed_time}';}
Nana! It is clear that the returned time is actually a string, and it is so strange (like the smarty tag ). In fact, {elapsed_time} will be replaced in the Output component. Let's take a look at the replacement method for the moment:
$elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');$output = str_replace('{elapsed_time}', $elapsed, $output);
That is to say, if no parameter is specified, the actual time difference between the time point total_execution_time_start and total_execution_time_end is obtained by calling this function. Furthermore, since total_execution_time_start is the first mark point set after the BM is loaded (total_execution_time_end is not defined, and the current time point is returned), the function returns the actual loading and running time of the system.
(2). If an unknown mark is called. The result is unknown and the returned result is null:
if ( ! isset($this->marker[$point1])){ return '';}
(3) If the mark of $ point2 is not set, set the mark of $ point2 to the current time point.
if ( ! isset($this->marker[$point2])){ $this->marker[$point2] = microtime();}
(4) time difference between the last two mark points returned:
list($sm, $ss) = explode(' ', $this->marker[$point1]);list($em, $es) = explode(' ', $this->marker[$point2]);return number_format(($em + $es) - ($sm + $ss), $decimals);
Let's also look at the previous example. Here we can call:
echo $this->benchmark->elapsed_time("function_test_start","function_test_end");
Obtain the execution time of the function.
3. memory_usage
This function returns the system memory usage (MB). Like {elapsed_time}, the {memory_usage} returned by this function will also be replaced in the 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 is relatively simple, we will not explain it more.
Finally, paste the source code of this component:
<?phpclass CI_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 */ function mark($name) { $this->marker[$name] = microtime(); } /** * Calculates the time difference between two 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 marked point * @param integer the number of decimal places * @return mixed */ function elapsed_time($point1 = '', $point2 = '', $decimals = 4) { if ($point1 == '') { return '{elapsed_time}'; } if ( ! isset($this->marker[$point1])) { return ''; } if ( ! isset($this->marker[$point2])) { $this->marker[$point2] = microtime(); } list($sm, $ss) = explode(' ', $this->marker[$point1]); list($em, $es) = explode(' ', $this->marker[$point2]); return number_format(($em + $es) - ($sm + $ss), $decimals); } /** * Memory Usage * This function returns the {memory_usage} pseudo-variable. */ function memory_usage() { return '{memory_usage}'; }}
How do I perform unit tests using the php CI framework? Is phpunit better if I use CI?
Phpunit
Why does the ci framework need to use mysql? Why does mysql55 report an error?
Not the latest mysql. ci is currently used normally.