Home >Backend Development >PHP Tutorial >CI framework source code reading notes 5 Benchmark test BenchMark.php, cibenchmark.php_PHP tutorial

CI framework source code reading notes 5 Benchmark test BenchMark.php, cibenchmark.php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:001141browse

CI framework source code reading notes 5 benchmark test BenchMark.php, cibenchmark.php

In the previous blog (CI framework source code reading notes 4 boot file CodeIgniter.php), we It has been seen that the core functions of the core processes in CI are completed by different components. These components are similar to individual modules. Different modules complete different functions. Each module can call each other and together 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, but only for applications, it should be regarded as a black box). In order to better understand and grasp this framework.

As usual, before we start, we paste the incomplete core component diagram in CI:

Since BenchMark is the first core component loaded in CI, our analysis starts with this component first. The meaning of BenchMark is very clear. Students who have used the BenchMark tool should know that 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, etc. of the program.

Look at the class diagram first:

The structure of this component is relatively simple, with only one marker internal variable and three external interfaces:

<span>1</span> <span>Elapsed_time
</span><span>2</span> <span>Mark
</span><span>3</span> Memory_usage

Expand each one below:

1.mark

The signature of the function is:

<span>function</span> mark(<span>$name</span>)

This function accepts a string type parameter, and the implementation is simpler, with only one sentence:

<span>$this</span>->marker[<span>$name</span>] = <span>microtime</span>();

In other words, this function is only used to record the time point at which the function is called.

It is worth noting that due to the special processing in Controller (we will explain it in detail later), you can use $this->benchmark->mark($name); to add running time points, for example:

$this->benchmark->mark("function_test_start");
$this->_test();
$this->benchmark->mark("function_test_end");
print_r($this->benchmark);

Among them, function_test_start and function_test_end are used to record the start and end time points of function calls respectively

Printed results:

Now to calculate the function call time, you need to use the second function elapsed_time of the BenchMark component

2. elapsed_time

The signature of the function is:

<span>function</span> elapsed_time(<span>$point1</span> = '', <span>$point2</span> = '', <span>$decimals</span> = 4)

All three parameters are optional

(1). If $point1 is empty, return ‘{elapsed_time}’

if ($point1 == '') {
     return '{elapsed_time}';
}

Nani! It should obviously return time, but instead it returns a string, and it’s so strange (similar to smarty tags). In fact, in the Output component, {elapsed_time} will be replaced. Let’s take a look at the replacement method for now:

$elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
$output = str_replace('{elapsed_time}', $elapsed, $output);

In other words, when no parameters are specified, what is actually obtained by calling this function is the time difference from the time point of total_execution_time_start to the time point of total_execution_time_end. Furthermore, 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 time point), what this function returns is actually the loading and running time of the system.

(2). If an unknown mark point is called. The result is unknown and empty is returned directly:

if ( ! isset($this->marker[$point1]))
{
    return '';
}

(3). If the mark point of $point2 is not set, the mark point of $point2 is set to the current time point.

if ( ! isset($this->marker[$point2]))
{
    $this->marker[$point2] = microtime();
}

(4). The 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);

Looking at the previous example, here we can call:

<span>echo</span> <span>$this</span>->benchmark->elapsed_time("function_test_start","function_test_end");

Get the execution time of the function.

3. memory_usage

This function returns the memory usage of the system (MB unit). Like {elapsed_time}, the {memory_usage} returned by this function will also be replaced in Output. :

<span>$memory</span>  = ( ! <span>function_exists</span>('memory_get_usage')) ? '0' : <span>round</span>(memory_get_usage()/1024/1024, 2).'MB'<span>;
</span><span>$output</span> = <span>str_replace</span>('{memory_usage}', <span>$memory</span>, <span>$output</span>);

Since the BenchMark component itself is relatively simple, we will not explain it further.

Finally, paste the source code of this component:

<?<span>php

</span><span>class</span><span> CI_Benchmark {

    </span><span>/*</span><span>*
     * List of all benchmark markers and when they were added
     *
     * @var array
     </span><span>*/</span>
    <span>var</span> <span>$marker</span> = <span>array</span><span>();

    </span><span>/*</span><span>*
     * Set a benchmark marker
     *
     * @access    public
     * @param    string    $name    name of the marker
     * @return    void
     </span><span>*/</span>
    <span>function</span> mark(<span>$name</span><span>)
    {
        </span><span>$this</span>->marker[<span>$name</span>] = <span>microtime</span><span>();
    }

    </span><span>/*</span><span>*
     * 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
     </span><span>*/</span>
    <span>function</span> elapsed_time(<span>$point1</span> = '', <span>$point2</span> = '', <span>$decimals</span> = 4<span>)
    {
        </span><span>if</span> (<span>$point1</span> == ''<span>)
        {
            </span><span>return</span> '{elapsed_time}'<span>;
        }

        </span><span>if</span> ( ! <span>isset</span>(<span>$this</span>->marker[<span>$point1</span><span>]))
        {
            </span><span>return</span> ''<span>;
        }

        </span><span>if</span> ( ! <span>isset</span>(<span>$this</span>->marker[<span>$point2</span><span>]))
        {
            </span><span>$this</span>->marker[<span>$point2</span>] = <span>microtime</span><span>();
        }

        </span><span>list</span>(<span>$sm</span>, <span>$ss</span>) = <span>explode</span>(' ', <span>$this</span>->marker[<span>$point1</span><span>]);
        </span><span>list</span>(<span>$em</span>, <span>$es</span>) = <span>explode</span>(' ', <span>$this</span>->marker[<span>$point2</span><span>]);

        </span><span>return</span> <span>number_format</span>((<span>$em</span> + <span>$es</span>) - (<span>$sm</span> + <span>$ss</span>), <span>$decimals</span><span>);
    }

    </span><span>/*</span><span>*
     * Memory Usage
     * This function returns the {memory_usage} pseudo-variable.
     </span><span>*/</span>
    <span>function</span><span> memory_usage()
    {
        </span><span>return</span> '{memory_usage}'<span>;
    }

}</span>

How to do unit testing using PHP CI framework? Is it better to use the one that comes with CI or phpunit?

phpunit

The ci framework has requirements for the mysql version. Why will an error be reported when using mysql55?

Mysql.ci, which is not the latest, is currently being used normally.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/907043.htmlTechArticleCI framework source code reading notes 5 Benchmark test BenchMark.php, cibenchmark.php Previous blog (CI framework source code reading Note 4 Boot file CodeIgniter.php), we have seen: CI core...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn