Codeigniter - SPLessons

CodeIgniter Profiler

Home > Lesson > Chapter 20
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Profiler

CodeIgniter Profiler

shape Description

While developing any application, performance is the key to make it popular and useful. In order to measure the performance of the application some factors have to be taken into consideration like time taken for loading classes and methods in the controller, count of queries and time allotted for every query to execute. All these functions are done by CodeIgniter Profiler.

Enabling CodeIgniter Profiler

shape Description

For enabling profiling in an application, add the below code in any method of the controller before loading the view. [php]$this->output->enable_profiler(TRUE);[/php] Once the CodeIgniter Profiler is enabled, a report regarding how fast the page loaded, appears at the page bottom.

Enabling Profiler for multiple pages

Suppose to apply the concept of profiling for multiple pages or for entire application, such that the methods should not get ugly for putting the above code in every method seeking profiling. So, to overcome the problem, Hooks feature of CodeIgniter is used to enabled/disabled profiling for the whole applications depending on the configuration file value which will be TRUE/FALSE. For Example, Step-1 : Create a file hooks.classes.php in the folder path application/hooks and enter the below code. [php] class ProfilerHandler { function EnableProfiler() { $CI = &get_instance(); $CI->output->enable_profiler( config_item('enable_hooks') ); } } [/php] Step-2 : Create a file hooks.php if not present in application/config directory and enter the below code. [php] $hook['post_controller_constructor'][] = array( 'class' => 'ProfilerHandler', 'function' => 'EnableProfiler', 'filename' => 'hooks.classes.php', 'filepath' => 'hooks', 'params' => array() ); [/php] Step-3 : Then enable hooks by setting the flag of $config['enable_hooks'] to TRUE. This step is done in config/config.php. [php] $config['enable_hooks'] = TRUE; // TRUE or FALSE depending upon your choice, what you want enable/disable profiling [/php]

Enabling Profiler

shape Description

To disable profiling add the below code in the desired method of the application. [php]$this->output->enable_profiler(FALSE);[/php]

Summary

shape Key Points

  • CodeIgniter Profiler measures the performance of an application.
  • Hooks feature of CodeIgniter is used to enabled/disabled profiling for the whole applications.
  • To enable, set enable_profiler to TRUE and to disable set enable_profiler to FALSE.