Advertisement

Google Ad Slot: content-top

CodeIgniter Application Profiling


Application profiling in CodeIgniter is a debugging and performance analysis tool.

It helps developers see benchmarking results, database queries, memory usage, POST/GET data, and more.

The profiler is very useful when you want to:

  • Check how long each part of your code takes.
  • Debug SQL queries.
  • Monitor memory consumption.
  • Optimize slow applications.

🔹 Enabling the Profiler

To enable the profiler for a controller method:

class Welcome extends CI_Controller {

    public function index()
    {
        $this->output->enable_profiler(TRUE);

        $this->load->view('welcome_message');
    }
}

✅ Now, when you load the page, you’ll see profiling information at the bottom of the page.

🔹 Disabling the Profiler

You can disable it by passing FALSE:

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

🔹 Enable Profiler for the Whole Application

If you want profiler data on all pages, set it in the controller’s constructor:

class MY_Controller extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->output->enable_profiler(TRUE);
    }
}

🔹 What Does the Profiler Show?

The profiler outputs:

  • Benchmarks → Time taken to execute different parts of your code.
  • Queries → All SQL queries run with execution time.
  • POST Data → Data submitted via forms.
  • GET Data → Data passed in URL.
  • URI String → Current URI details.
  • Memory Usage → Amount of memory consumed.
  • Config Variables → Loaded config values.
  • Session Data → Current session variables (if session library is enabled).

🔹 Selectively Display Profiler Sections

You can choose which profiler sections to show or hide:

$sections = array(
    'benchmarks' => TRUE,
    'queries'    => TRUE,
    'memory_usage' => TRUE,
    'post'       => FALSE,
    'config'     => FALSE
);

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

✅ This will only show benchmarks, queries, and memory usage, while hiding others.

🔹 Example

class Products extends CI_Controller {

    public function index()
    {
        $this->output->enable_profiler(TRUE);

        $data['products'] = $this->db->get('products')->result();
        $this->load->view('product_list', $data);
    }
}

👉 Now, when you load the Products page, you’ll see all executed queries and page performance details at the bottom.