


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
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>
phpunit
Mysql.ci, which is not the latest, is currently being used normally.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Evaluating the cost/performance of commercial support for a Java framework involves the following steps: Determine the required level of assurance and service level agreement (SLA) guarantees. The experience and expertise of the research support team. Consider additional services such as upgrades, troubleshooting, and performance optimization. Weigh business support costs against risk mitigation and increased efficiency.

The lightweight PHP framework improves application performance through small size and low resource consumption. Its features include: small size, fast startup, low memory usage, improved response speed and throughput, and reduced resource consumption. Practical case: SlimFramework creates REST API, only 500KB, high responsiveness and high throughput

Writing clear and comprehensive documentation is crucial for the Golang framework. Best practices include following an established documentation style, such as Google's Go Coding Style Guide. Use a clear organizational structure, including headings, subheadings, and lists, and provide navigation. Provides comprehensive and accurate information, including getting started guides, API references, and concepts. Use code examples to illustrate concepts and usage. Keep documentation updated, track changes and document new features. Provide support and community resources such as GitHub issues and forums. Create practical examples, such as API documentation.

Choose the best Go framework based on application scenarios: consider application type, language features, performance requirements, and ecosystem. Common Go frameworks: Gin (Web application), Echo (Web service), Fiber (high throughput), gorm (ORM), fasthttp (speed). Practical case: building REST API (Fiber) and interacting with the database (gorm). Choose a framework: choose fasthttp for key performance, Gin/Echo for flexible web applications, and gorm for database interaction.

The learning curve of a PHP framework depends on language proficiency, framework complexity, documentation quality, and community support. The learning curve of PHP frameworks is higher when compared to Python frameworks and lower when compared to Ruby frameworks. Compared to Java frameworks, PHP frameworks have a moderate learning curve but a shorter time to get started.

In Go framework development, common challenges and their solutions are: Error handling: Use the errors package for management, and use middleware to centrally handle errors. Authentication and authorization: Integrate third-party libraries and create custom middleware to check credentials. Concurrency processing: Use goroutines, mutexes, and channels to control resource access. Unit testing: Use gotest packages, mocks, and stubs for isolation, and code coverage tools to ensure sufficiency. Deployment and monitoring: Use Docker containers to package deployments, set up data backups, and track performance and errors with logging and monitoring tools.

A few days ago, Google officially pushed the Android 15 Beta 4 update to eligible Pixel smartphone and tablet users. This marks that the Android 15 operating system has entered the platform stable stage, indicating that its stable version will be officially released with global users in the next few days. Meet. At the same time, this development also injects new vitality into Samsung Electronics' Galaxy device series to accelerate the development process of its OneUI7.0 version. 1.[Android15Beta4 promotes Samsung OneUI7.0 stable build](https://www.cnbeta.com/articles/tech/1427022.htm) With Android15Bet

When choosing a Go framework, key performance indicators (KPIs) include: response time, throughput, concurrency, and resource usage. By benchmarking and comparing frameworks' KPIs, developers can make informed choices based on application needs, taking into account expected load, performance-critical sections, and resource constraints.
