I have been using my latest project, VentStatus, as a an opportunity to learn a new framework: CodeIgniter. It has treated me well so far, most problems encountered only required a short workaround. Lately I’ve been concerned with the scale-ability of VentStatus, it’s my first project on this large of a scale. Luckily, CodeIgniter has some benchmarking classes built in, and it really helped me cut down my load times.

The first thing you are going to want to do is enable the CodeIgniter Profiler. To do this, simply insert the following code into any of your Controller functions.

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

Now load a page from that controller. There will be a small summary of how your loading time is being spent, as well as an output of every SQL statement made and the length of time it took. For example, mine looks something like this.

Loading Time Base Classes 0.0166
Controller Execution Time ( Servers / Status ) 8.0166
Total Execution Time 11.1874

Now you can basically ignore “Loading Time Base Classes.” It is most likely not going to be a large part of your execution time, and won’t offer much in terms of optimization. Just make sure you are only auto loading things you are actually making use of.

What we are focuing on is the “Total Execution Time.” This time is what you as the programmer are responsible for. Now, there are a million ways to optomize your code, but I’m going to just suggest the most obvious and time improving fix for a slow load time.

Scroll down and look through the list of all your queries. You want to find queries with large load times. There is no number for how much is “too much time” for your query to take. If you have 400 SELECT statements and they all take 0.005 seconds to complete, you are going to want to try to find a way to reduce that time so your page doesn’t take an extra 2 seconds to load. After I’ve got my site running nice, most of my queries take around 0.0006 seconds to execute.

If you’re still having trouble identifying what queries are effecting your load time, an easy way to find bottlenecks is by setting up benchmarking points. These will measure the time between two different points in your program. This will help you find out how long funtions or loops are taking to execute. Wrap the following codes around the loop or function you want to measure.


$this->benchmark->mark('my_mark_start');
// some code
$this->benchmark->mark('my_mark_end');

This will measure the amount of time it takes for the code “//some code” to be executed in milliseconds. The time will be outputted right below “Loading Time Base Classes .”

Remember, that you want to wrap these around the outside of loops, not the inside. Putting these on the inside will only output the time of the last SQL statemet to execute, so things look like they’re behaving normally when we’re actually only seeing a fraction of the time its taking for that loop.

My Mark 1.7263

Once you found out exactly what is causing a bottleneck, you can proceed to fix it. I found that most of the time, I was looking entire seconds because my SQL tables were not indexed correctly. Indexes can cause drastic increases in performance when used correctly. If you’re not sure about how to go about adding an index to your table, read up here.

After correctly indexing six of my tables, this is what my site’s profile looked like.

Loading Time Base Classes 0.0314
My Mark 0.8914
Controller Execution Time ( Servers / Status ) 1.7209
Total Execution Time 1.7525

If you find your site speedy and you’re done debugging, you can go ahead and turn the profiler off.

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

The benchmark points can be left as is for use in the future. If you’re still stuck with a slow site after the above, make sure you take a look into a million of the other ways you can speed up your site. Some popular fixes for slow sites are caching, optimizing, and minifying. Also, I would reccomend the extension YSlow fore Firebug if you’re running Firefox.