CI framework source code reading notes 5 BenchMark test BenchMark. php

Source: Internet
Author: User

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. There is only one internal marker variable and three external interfaces: 1 Elapsed_time2 Mark3 Memory_usage. Let's look at them one by one: 1. the signature of the mark function 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, 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); where, function_test_start and function_test_end are used to record the results of the start and end time points of the function call respectively: to calculate the call time of the function, you need to use the second function elapsed_time 2 of the BenchMark component. the signature of the elapsed_time function is: function elapsed_time ($ point1 = '', $ Point2 ='', $ decimals = 4) all three parameters are optional (1 ). if $ point1 is empty, '{elapsed_time}' if ($ point1 = '') {return '{elapsed_time}';} Nanni is returned! 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: $ elapsed = $ BM-> elapsed_time ('total _ execution_time_start ', 'Total _ execution_time_end '); $ output = str_replace (' {elapsed_time} ', $ elapsed, $ output); that is, if no parameter is specified, actually, the 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 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. 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 Usage (MB). Like {elapsed_time}, {memory_usage} returned by this function will also be replaced in 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: copy the Code <? Php 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 fun Ction instead returns the {elapsed_time} pseudo-variable. this permits the full system * @ access public * @ 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 }';}}

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.