Profiling Your CI (Codeigniter) Application

Category:
PHP
Tags:
Codeigniter

Krunal Patel
6 months ago

Introduction

When developing a website, optimizing site performance is a key concern. This involves monitoring various factors such as page load times, memory usage, and overall resource consumption. Additionally, it's essential to inspect internal data such as session details, POST and GET data, configuration variables, and query outputs for debugging and optimization purposes. Fortunately, viewing benchmark details is straightforward and can be accomplished in just a few steps.



Enabling the Profiling:

Include the following line in your controller method to enable profiler output:

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

Here's an example snippet for reference:


<?php
class Profiling extends CI_Controller{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Profiling_model','user');  
    }

    public function index(){
        $data['users'] = $this->user->get_data();

        $this->load->view('profiling', $data);
        $this->output->enable_profiler(TRUE);
    }
}



After hitting your route, you will find the generated report displayed at the bottom of the page.

Below is the screenshot of the generated report:



Disabling the Profiling:

To disable profiling in your application, simply add the following command:

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

Enabling / Disabling Profiling Section:
Each section can be enabled or disabled using boolean configuration values in your `application/config/profiler.php` file.

Example:

$config['config']          = FALSE;
$config['queries']         = FALSE;

You can override the defaults and configuration file values using the `set_profile_sections()` method:

$sections = array(
            'config'  => TRUE,
            'queries' => TRUE
        	);
$this->output->set_profiler_sections($sections);

Example:

public function index(){
        $data['users'] = $this->user->get_data();

        $this->load->view('profiling', $data);
        $this->output->enable_profiler(TRUE);
        $sections = array(
            'config'  => TRUE,
            'queries' => TRUE
        );
   
        $this->output->set_profiler_sections($sections);
 }


Good Luck.

DevZone
Made by developer, Made for developers