jigniter™

jQuery & CodeIgniter – Perfect combination for web application

Profiling your CodeIgniter applications

no comments

The CI Output Class has enable_profiler() to set enable the profiler in CodeIgniter.

By using this operation, you can enable or disable the profiler in the output.


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

The profiler is very useful for debugging purposes, you can see a footer with information all the time while running your application in development phase.

  • URI String
  • Class and Method
  • Memory Usage
  • Simple Benchmarks
  • $_GET variable
  • $_POST variable
  • Database Queries

The class is loaded automatically so you can use it at anytime in your code. However, the good practice is to call it only one time. You can follow the recommendation settings described in Profiling Your Web Applications in CodeIgniter, A Web Developers Blog. By following this way, you can use a debug setting to tell the function if profiler should be enabled or disabled.

Another practice could be to separate between different configuration settings based on your development environment and your production environment. Then, you can choose to enable profiler only in your development environment.

Here is the output generated in my Agents project.

codeigniter-profiler1

So, with profiler enabled, I can see very useful information on the footer about the processing of the request and involved database queries:

codeigniter-profiler2 

Enabling Profiler using CodeIgniter Hooks

Enabling profiler using Hooks could be appropriate to keep it in one place. To do that, you need to configure hooks in your application/config/config.php


$config['enable_hooks'] = TRUE;

Then, go to applications/config/hooks.php and add the following entry point:


$hook['post_controller_constructor'] = array(
                                'class'    => '',
                                'function' => 'enable_profiler',
                                'filename' => 'profiler.php',
                                'filepath' => 'hooks',
                                'params'   => array()
                                );

The previous code is telling CodeIgniter to run a hook just after Post Controller Constructor. Notice we are calling a function, not a class instance, to keep it simple (and fast). So, then we need to create a profiler.php file under application/hooks. Here you’d need to get the CodeIgniter instance using get_instance().


function enable_profiler()
{
	$CI =& get_instance();
	$CI->output->enable_profiler(TRUE);
}

Then, run the application and see the Profiler in action.

Customizing the profiler

This Wiki entry explains how to customize the Profiler by showing the information in a popup window, not in the footer. Curiously it is using the Hooks method, too.

Related posts:

  1. How do I deploy my CodeIgniter applications using Subversion

Posted by admin

2009-08-14 at 10:47 am

Leave a Reply