Performance on Rails

Post on 06-May-2015

2892 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Presentation about the several caching mecanisms in Rails, optimization, performance which I gave on Barcamp PT 08.

Transcript

Ruby on Rails Performance

Tips

Barcamp Coimbra 2008 Pedro Sousa

Your application is slow

Caching in RoR

Before you begin

You need to define where to place your cache (db, memory, file)

You need to tweak you development environment to test caching

Setup up your environment

<= Rails 2.0

> Rails 2.1

Types of Cache Store

development.rb

Setup up your environment

3 Types of Caching

Page Caching

will always use File Store

Action Caching & Fragment Caching

Will use the cache store you defined

Page CachingTransforms your views into pure HTML

Best for pages with common content for all your users or very static pages

Example

Action Caching

Works the same as way as page caching ...

but goes through dispatcher to run filters and callbacks

For example you can use it with pages that require authentication

Example

More options

Fragment Caching

More flexible

Great for dynamic content directed at specific users or conditions (ex: your friends latest blog posts)

Transforms portions of your views into rendered HTML

Examples of Caching

Example No caching

Fragment Caching

Oops, I forgot about the controller

the fragment

Cleaning upindividual cache files

Cleaning up (brute force)

Cleaning up with memcached

soon...

DB Model Cache

It’s built into Rails, you don’t have to do nothing

good for not making the same query twice

limited... only works on the same request

DB Cache (stupid) example

Memcached

New in Rails 2.0

Simple to use

It’s like a Hash in memory, has a key/value way of working

MethodsRails.cache.read

Rails.cache.write

Rails.cache.fetch

Rails.cache.delete

Rails.cache.exist?

Rails.cache.increment

Rails.cache.decrement

example

example with dataModel

Controller

View (first time)

View (second time, it’s read from the cache)

example with data

Tips for Optimization

Write Eficient SQL Queries

Bring only what you need

Eager Loading

Sometimes it’s best to bring all data at once

Prevents N+1 problem

Optimized Eager Loading in Rails 2.1

Create indexes on DBIt really makes a difference

Don’t exagerate

Use TransactionsGroup Transactions when possible

Minimizes errors

Less effort on the Database

Reduce http requests

Too many javascript and css files makes your page slow

You can package all you files into only one

SolutionsBuilt-in mechanism in Rails 2.0 but without js minification

Alternative plugin - Bundle-FU

http://code.google.com/p/bundle-fu/

Bundling Example Before/After

Too many images?Use CSS Sprites Technique

Less http requests

More hard to manage

Tools

Firebug & Yslow

Track your page performance

Easy to install and run

Download from

http://developer.yahoo.com/yslow/

Examples

More examples

Other Small Tips

Use LibXML-Ruby for processing XML:

http://libxml.rubyforge.org/

Log only what you need

Use a CDN (ex: Amazon CloudFront)

Patch the RUBY GC:

http://rubyforge.org/projects/railsbench/

That’s it! Have fun!

Don’t be afraid to experiment!

Tweak until your satisfied!

If all fails, invest in hardware. :)

The End

Thank you

[blog] http://www.reinventar.com[work] http://www.thinkorange.pt

[linkedin] http://www.linkedin.com/psousa

Pedro Sousa

top related