Introduction
Here I will show you how to do benchmarking Codeigniter application. The benchmark information may help you during development for debugging and optimization for the application code. CodeIgniter has a Benchmarking class that is always active (class is initialized automatically), enabling the time difference between any two marked points to be calculated.
Doc says, the benchmark is always started the moment the framework is invoked, and ended by the output class right before sending the final view to the browser, enabling a very accurate timing of the entire system execution to be shown.
Related Posts:
Prerequisites
Apache 2.4, PHP 7.4.3, Codeigniter 3.1.11
Project Directory
Create a root project directory called codeIgniter-benchmark under Apache server’s htdocs directory. Then extract your Codeigniter zip folder into the root project directory.
I may not mention the project root directory in subsequent sections and I will assume that I am talking with respect to the project root directory.
Controller Class
Create a controller class called Benchmarking in a file Benchmarking.php under application/controllers/ folder with the below source code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Benchmarking extends CI_Controller {
public function index() {
$this->load->view('benchmarking');
}
}
View File
Create a view file called benchmarking.php under application/views/ folder with below source code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter Benchmarking</title>
<style type="text/css">
::selection { background-color: #E13300; color: white; }
::-moz-selection { background-color: #E13300; color: white; }
body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 19px;
font-weight: normal;
margin: 0 0 14px 0;
padding: 14px 15px 10px 15px;
}
code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#body {
margin: 0 15px 0 15px;
}
p.footer {
text-align: right;
font-size: 11px;
border-top: 1px solid #D0D0D0;
line-height: 32px;
padding: 0 10px 0 10px;
margin: 20px 0 0 0;
}
#container {
margin: 10px;
border: 1px solid #D0D0D0;
box-shadow: 0 0 8px #D0D0D0;
}
</style>
</head>
<body>
<div id="container">
<h1>Welcome to CodeIgniter Benchmarking</h1>
<div id="body">
<p>The benchmark information may help you during development for debugging and optimization.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>
</body>
</html>
Configure Route
Edit file application/config/routes.php and change the default controller as below:
$route['default_controller'] = 'benchmarking';
Testing the Application
Run the application and hit the URL http://localhost/codeIgniter-benchmarking/ in a browser you will see below output:

From the above output it is obvious the page rendered in 0.0413 seconds as highlighted in the above figure.
I have not activated benchmarking as it is by default activated.
Benchmarking
You will see in the subsequent sections how to use benchmarking in Codeigniter applications.
Using Benchmarking Class
The Benchmark class can be used within your controllers, views, or your models. The process for usage is this:
- Mark a start point
- Mark an end point
- Run the “elapsed time” function to view the results
Using in Controller
Controller Class
I will benchmark two points in the controller method to check the elapsed time. Here I will mark the starting point as b_mark_start1
and mark the ending point as b_mark_end1
.
Update the controller class Benchmarking as follows:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Benchmarking extends CI_Controller {
public function index() {
$this->benchmark->mark('b_mark_start1');
$a=5;
$b=10;
$c = $a * $b * 100000000000097987979879879798797790000000;
$b=$b/$a/$a;
for($i=1;$i<1000;$i++){
for($j=1;$j<$i/2;$j++){
if($j%2==0){
break;
}
}
}
$this->benchmark->mark('b_mark_end1');
$this->load->view('benchmarking');
}
}
View File
Update the view file to display the elapsed time between two marks (b_mark_start1
and b_mark_end1
).
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter Benchmarking</title>
<style type="text/css">
::selection { background-color: #E13300; color: white; }
::-moz-selection { background-color: #E13300; color: white; }
body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 19px;
font-weight: normal;
margin: 0 0 14px 0;
padding: 14px 15px 10px 15px;
}
code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#body {
margin: 0 15px 0 15px;
}
p.footer {
text-align: right;
font-size: 11px;
border-top: 1px solid #D0D0D0;
line-height: 32px;
padding: 0 10px 0 10px;
margin: 20px 0 0 0;
}
#container {
margin: 10px;
border: 1px solid #D0D0D0;
box-shadow: 0 0 8px #D0D0D0;
}
</style>
</head>
<body>
<div id="container">
<h1>Welcome to CodeIgniter Benchmarking</h1>
<div id="body">
<p>The benchmark information may help you during development for debugging and optimization.</p>
<p><?php echo 'Elapsed time between b_mark_start1 and b_mark-end1: '. $this->benchmark->elapsed_time('b_mark_start1', 'b_mark_end1');?></p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>
</body>
</html>
Testing the Application
Now re-run the application, you will see the following output:

In the above image you see how much time it was taken between two marking points inside the controller method.
Using in View File
Previously I have shown you how to use benchmarking in controller class. Now you will see how to use benchmarking in view file.
Controller Class
Update the controller class to remove mark points.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Benchmarking extends CI_Controller {
public function index() {
$this->load->view('benchmarking');
}
}
View File
Update the view file to mark the points.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter Benchmarking</title>
<style type="text/css">
::selection { background-color: #E13300; color: white; }
::-moz-selection { background-color: #E13300; color: white; }
body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 19px;
font-weight: normal;
margin: 0 0 14px 0;
padding: 14px 15px 10px 15px;
}
code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#body {
margin: 0 15px 0 15px;
}
p.footer {
text-align: right;
font-size: 11px;
border-top: 1px solid #D0D0D0;
line-height: 32px;
padding: 0 10px 0 10px;
margin: 20px 0 0 0;
}
#container {
margin: 10px;
border: 1px solid #D0D0D0;
box-shadow: 0 0 8px #D0D0D0;
}
</style>
</head>
<body>
<div id="container">
<h1>Welcome to CodeIgniter Benchmarking</h1>
<div id="body">
<p>The benchmark information may help you during development for debugging and optimization.</p>
<?php
$this->benchmark->mark('b_mark_start1');
$a=5;
$b=10;
$c = $a * $b * 100000000000097987979879879798797790000000;
$b=$b/$a/$a;
for($i=1;$i<1000;$i++){
for($j=1;$j<$i/2;$j++){
if($j%2==0){
break;
}
}
}
$this->benchmark->mark('b_mark_end1');
?>
<p><?php echo 'Elapsed time between b_mark_start1 and b_mark-end1: '. $this->benchmark->elapsed_time('b_mark_start1', 'b_mark_end1');?></p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>
</body>
</html>
Testing the Application
Run the application on browser, you will see below output:

From the above output it is obvious to know the elapsed time between two mark points on view file.
You can also use the benchmarking on model class in the same way you have seen in the above example.
Source Code
Thanks for reading.