CI framework Source Code Reading Notes 5 BenchMark test BenchMark. php, cibenchmark. php_PHP tutorial

Source: Internet
Author: User
CI framework Source Code Reading Notes 5 BenchMark test BenchMark. php, cibenchmark. php. CI framework Source Code Reading Notes 5 BenchMark test BenchMark. php, cibenchmark. php previous blog (CI framework source code reading Note 4 guide file CodeIgniter. php), we have seen: CI core 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:

 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.

BenchMark. php, cibenchmark. php in the previous blog (CI framework source code reading Note 4 guide file CodeIgniter. php), we have seen: CI nuclear...

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.