views:

1258

answers:

5

A site I built with Kohana was slammed with an enormous amount of traffic yesterday, causing me to take a step back and evaluate some of the design. I'm curious what are some standard techniques for optimizing Kohana-based applications?

I'm interested in benchmarking as well. Do I need to setup Benchmark::start() and Benchmark::stop() for each controller-method in order to see execution times for all pages, or am I able to apply benchmarking globally and quickly?

I will be using the Cache-library more in time to come, but I am open to more suggestions as I'm sure there's a lot I can do that I'm simply not aware of at the moment.

+1  A: 

Profile code with XDebug.

Use a lot of caching. If your pages are relatively static, then reverse proxy might be the best way to do it.

porneL
+1  A: 
Alix Axel
A: 

I totally agree with the XDebug and caching answers. Don't look into the Kohana layer for optimization until you've identified your biggest speed and scale bottlenecks.

XDebug will tell you were you spend the most of your time and identify 'hotspots' in your code. Keep this profiling information so you can baseline and measure performance improvements.

Example problem and solution: If you find that you're building up expensive objects from the database each time, that don't really change often, then you can look at caching them with memcached or another mechanism. All of these performance fixes take time and add complexity to your system, so be sure of your bottlenecks before you start fixing them.

Ozten
+34  A: 
Pascal MARTIN
It's almost silly how thorough of an answer this is, especially compared to the other posts.
While adding new servers might be cheaper than having a developer work for 5 days, don't forget that your software might not work properly when ran from multiple servers (you might have to share files across servers somehow - NFS can be a pain, are you using sessions? better move them to the DB,etc). and that in itself will require the developer to work on things aswell.
MathieuK
Great explanation! Do you have a blog I can subscribe to? :-)
Cd-MaN
@dnh828 : I wrote it hoping to re-use it for some other occasions (I actually already did) ;; @MathieuK : definitly true (about sessions, though, instead of DB, you could envisage using memcache too) ;; @Cd-MaN : Thanks! I actually do have a blog, but it's in french and I really don't blog often... still, if you are interested : http://blog.pascal-martin.fr/
Pascal MARTIN
+1: Great explanation, this applies to all projects. Thanks
Sarfraz
Appreciate the link love. Thanks.
Till
A: 

Kohana is out of the box very very fast, except for the use of database objects. To quote Zombor "You can reduce memory usage by ensuring you are using the database result object instead of result arrays." This makes a HUGEE performance difference on a site that is being slammed. Not only does it use more memory, it slows down execution of scripts.

Also - you must use caching. I prefer memcache and use it in my models like this:

public function get($e_id)
{
 $event_data = $this->cache->get('event_get_'.$e_id.Kohana::config('config.site_domain'));

 if ($event_data === NULL)
 {
  $this->db_slave
   ->select('e_id,e_name')
   ->from('Events')
   ->where('e_id', $e_id);

  $result = $this->db_slave->get();
  $event_data = ($result->count() ==1)? $result->current() : FALSE;

  $this->cache->set('event_get_'.$e_id.Kohana::config('config.site_domain'), $event_data, NULL, 300); // 5 minutes
 }

 return $event_data;
}

This will also dramatically increase performance. The above two techniques improved a sites performance by 80%.

If you gave some more information about where you think the bottleneck is, I'm sure we could give some better ideas.

Also check out yslow (google it) for some other performance tips.

ae