To measure the performance of a web site we need to get loading time of all classes and number of queries and execution time of each query etc.
In CodeIgniter it can be done very easy way. A very good part of CodeIgniter is its ability to give you the ‘profiling’ information for page loads. That is, if you add:

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

In your controller before you load a view, CodeIgniter will give you information regarding how fast the page loaded, how many SQL queries executed, the content of each query, and the running time of each query. This is incredibly useful when you are trying to debug your application, or simply see how quickly things are loading.

There’s only one problem: To enable profiling, that line of code above must be present. What if you want to profile several pages, or even your whole web application? In that case, you have to start thinking:

I could put that line in the constructor of my controller, and then of of that controller’s methods will be profiled.
I could put that line in each method I want to profile.

These methods start to get ugly. And of course, you don’t want to comment out each profiling line when you don’t need them.

I will suggest to tackle this problem using CodeIgniter’s Hooks feature to enable or disable profiling for the entire web applications based on a value in the configuration file. In the end, I can turn on profiling for my entire website via a config value by setting it to true or false.

Here is the steps:

1. Create a folder in your application directory named hooks (If it does not already exist). Inside it, create a file named hooks.classes.php. Inside it, put this:

class ProfilerHandler
{
	function EnableProfiler()
	{
		$CI = &get_instance();
		$CI->output->enable_profiler( config_item('enable_hooks') );
	}
}

2. Create a file in the config directory named hooks.php if it does not already exist. Inside it, place:


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

3. Finally, enable hooks in your CodeIgniter Application by going to config/config.php end setting the flag to true:


$config['enable_hooks'] = TRUE;	// TRUE or FALSE depending upon your choice, what you want enable/disable profiling

Setting this value to true will enable profiling across your entire website.

That All.

References:
http://ellislab.com/codeigniter/user-guide/general/profiling.html
http://ellislab.com/codeigniter/user-guide/general/hooks.html
http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-profiling-benchmarking-hooks/
https://github.com/EllisLab/CodeIgniter/wiki/Customizing-the-Profiler

Advertisements