Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter: Profiling and Performance

I'm new to app development and CI as a whole, so I've got an oodle of questions.

What is profiling? How is it used? How does it work? What is considered a "long" time?

More importantly,

How do I use it to improve performance?

The reason I'm asking is b/c my app is really sluggish right now.

like image 764
Kevin Brown Avatar asked May 07 '10 13:05

Kevin Brown


2 Answers

As should become clear when you use profiling it's used to see how fast aspects of the page are. Put this line in the main controller near the start (eg in the constructor right after you call the parent's constructor:

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

This will print a lot of profiling information at the bottom of your resulting page. This will include all the database queries, how long the take and how long was spent in the controllers (PHP time as opposed to database query time).

If something is slow start by enabling the profiler and checking if it's the controller or the queries (or both). If it's the database queries, then you need to improve them and that's a whole topic in itself. If it's the controllers then you need to find out what code specifically is causing the slowdown.

Have a read of https://www.codeigniter.com/user_guide/libraries/benchmark.html and start placing the benchmarking start and stop tags where you think it's likely to cause slow speeds, loops and any recursive functions are the first places you should check. Once you find a slow segment of code you need to find how to optmise it, which again is a whole can of worms on its own.

It is possible that slow speeds are due to bad hardware, a busy server, a slow connection or a ton of other issues, those are outside the scope of this question though.

Edit: Just want to add that you don't use CI's profiler or benchmarking functionality to improve the speed, only to find where the speed needs to be improved. I know it's a minor thing, but just thought I should point it out.

like image 189
Sid Avatar answered Sep 21 '22 15:09

Sid


In performance tuning, the bottom line is how long does the program take, in wall-clock time, to finish the given task. I know that sounds like DUH, but that's the one concept everyone can agree on.

There is a very firmly held belief that in order to find the code that you can fruitfully optimize to speed up the program, you do it my measuring time taken by various functions and counting how many times they are called.

Even hardware engineers, who I would have thought would know better, have offered to provide me with nice timer and counter chips, when they were building a board set that I was trying to program, thinking that would help me tune the software.

NO THANK YOU, I told them. All I need is a way to stop it at random and ask what the heck is it doing, and why is it doing it. It's the drop-in-on-your-worker-at-random-and-ask-what-he's-doing approach. If I do this several times, and a good chunk of the time is being spent less than productively, the chance I won't catch it is basically nil.

Here is more on the subject.

like image 44
Mike Dunlavey Avatar answered Sep 18 '22 15:09

Mike Dunlavey