CI framework Source Code Reading Notes 5 benchmarking BenchMark. php_PHP tutorial

Source: Internet
Author: User
CI framework Source Code Reading Notes 5 BenchMark test BenchMark. php. CI framework source code reading Note 5 BenchMark test BenchMark. php since BenchMark is the first core component loaded in CI, our analysis begins with this component. BenchMark meaning is very clear CI framework Source Code Reading Notes 5 BenchMark test BenchMark. php
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.

This component has a simple structure, with only one internal marker variable and three external interfaces:

1 Elapsed_time

2 Mark

3 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:

Class 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 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 participant marked point

* @ Param string a participant 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 }';

}

}

BenchMark. php since BenchMark is the first core component loaded in CI, our analysis starts from this component. The meaning of BenchMark is very clear...

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.