Part 5 – Benchmarking and profiling your Codeigniter application with built in tools

Codeigniter HMVC

The benchmarking and profiling tools available with Codeigniter are very useful for any developer. It not only tells you what is the total execution time for that particular controller or overall CI execution time; but also gives you insights on useful data like queries, post data, session data etc. And whenever we are making an application such tools makes us confident about the performance of the application.

Setting up the controller with the data

I am going to use a very simple query object which is fetching some records from the products table in my Northwind database and inside the view, I am printing them out in a list.

public function index() {
  // loading assets
  $this->load->model('products_model', 'products');
  
  // setting the data for the view
  $data['header']['page_title'] = 'Profiling a page inside Code Igniter'; // title for the page
  $data['content']['view_name'] = 'profiling_view'; // name of the partial view to load
  $data['content']['view_data'] = array(
    'products' => $this->products->get_products(1)
  ); // data coming inside the view
  
  // calling the view and sending data
  $this->load->view('main_page_view',$data);
}

Benchmarking the total execution time and memory usage

For benchmarking there are two options. One is to check the total execution time and total memory usage by PHP to load up the page. But there are situation also when we would like to profile only a small portion of code — a loop may be to know if there are any performance issues/bottlenecks.

So, the first one is something which we can straight away print inside a view which goes like:

<div class="span12">
    <h3>List of products coming below</h3>
    <ul>
        <?php foreach ($products as $product): ?>
        <li><?php echo $product->ProductName; ?></li>
        <?php endforeach; ?>
    </ul>
</div>
<?php echo 'Elapsed time: ' . $this->benchmark->elapsed_time();?>
<br>
<?php echo 'Memory Usage: ' . $this->benchmark->memory_usage();?>

This will show the execution time and memory usage.

Benchmarking the controller only

Now let’s say we want to check only how much time the controller is taking to get all the data and does all the logic stuff. For this we will add points and then print out the execution time. Here is the controller code with the points and then printing out the total value.

public function index() {
  $this-&gt;benchmark-&gt;mark('code_start');
  
  // loading assets
  $this-&gt;load-&gt;model('products_model', 'products');
  
  // setting the data for the view
  $data['header']['page_title'] = 'Profiling a page inside Code Igniter'; // title for the page
  $data['content']['view_name'] = 'profiling_view'; // name of the partial view to load
  $data['content']['view_data'] = array(
    'products' =&gt; $this-&gt;products-&gt;get_products(1)
  ); // data coming inside the view
  
  // calling the view and sending data
  $this-&gt;load-&gt;view('main_page_view',$data);
    
  $this-&gt;benchmark-&gt;mark('code_end');
  echo '
 Elapsed time in the controller: ' . $this-&gt;benchmark-&gt;elapsed_time('code_start', 'code_end');
}

If you check this code, it’s same as the previous one. The only difference is adding three extra lines – the first line and the last two lines.

Profiling the page

Now that we can see the total execution and also check the time taken for a small part of the code; we take the next step and print our profiling information to get an idea about the different resources on the page and the time taken for these.

Codeigniter’s Output class will help us print out information. One option is to set this inside a controller or inside the construct function which will print this out on all pages inside that controller

$this->output->enable_profiler(TRUE);

But if you see there is a setting of TRUE / FALSE that you have to control to view this information. Now this will then become difficult to control on a production server. What I do is write a helper function which checks for a configuration value as TRUE / FALSE and then checks an IP and if the two conditions fulfil, it prints the profiling information.

Setting up a config file to control the profiling on development/production server

So, here I will show you how you can get the value as TRUE / FALSE from a config file. I created a php file inside the config folder “developer.php”. Inside this, I have set an array with index “developer_profiling” as TRUE.

$config['developer_profiling'] = TRUE;

To use this configuration file, we have to either manually load this configuration file using codeigniter load functionality, or the best is to autoload this file by mentioning the file name in the autoload.php file.

Once done, I call the profiling function in my constructor and use the config file setting to control whether to display it or not.

$this->output->enable_profiler($this->config->item('developer_profiling'));