Introduction

Here we will see 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.

You may also like to read Profiling Codeigniter Application.

Prerequisites

Apache 2.4, PHP 7.0.15, Codeigniter 3.10.1

Example with Source Code

Creating Project Directory

Create a root project directory called codeIgniter-3.1.10-benchmarking under <apache 2.4 installation directory>/htdocs. Then extract your Codeigniter zip folder into the root project directory.

Creating Controller Class

Create a controller class called Benchmarking in codeIgniter-3.1.10-benchmarking/application/controllers/Benchmarking.php 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');
		
	}
}

Creating View File

Create a view file called benchmarking.php under codeIgniter-3.1.10-benchmarking/application/views/ 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>

Configuring Route

Edit codeIgniter-3.1.10-benchmarking/application/config/routes.php and change the default controller as below:

$route['default_controller'] = 'benchmarking';

Testing the Application

Run the application, you will see below output:

benchmarking codeigniter application

From the above output it is obvious the page rendered in 0.3065 seconds as highlighted in the above figure.

We have not activated benchmarking as it is by default activated.

Benchmarking

We will see in the subsequent sections how to use benchmarking.

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

Updating Controller Class

We will benchmark two points in the controller method to check the elapsed time. Here we will mark the starting point as ‘b_mark_start1’ and mark the ending point as ‘b_mark_end1’.

Update the above 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');
		
	}
}

Updating 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 run the application, you will see the following output:

benchmarking codeigniter application

In the above image we see how much time it was taken between two marking points inside the controller method.

Using in View File

Previously we have seen how to use benchmarking in controller class. Now we will see how to use benchmarking in view file.

Updating 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');
		
	}
}

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

benchmarking codeigniter

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.

You may also like to read Profiling Codeigniter Application.

Source Code

You can download source code.

Thanks for reading.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *