Top Banner
Ruby on Rails Performance Tips Barcamp Coimbra 2008 Pedro Sousa
49

Performance on Rails

May 06, 2015

Download

Technology

Pedro Sousa

Presentation about the several caching mecanisms in Rails, optimization, performance which I gave on Barcamp PT 08.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Performance on Rails

Ruby on Rails Performance

Tips

Barcamp Coimbra 2008 Pedro Sousa

Page 2: Performance on Rails

Your application is slow

Page 3: Performance on Rails

Caching in RoR

Page 4: Performance on Rails

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

Page 5: Performance on Rails

Setup up your environment

<= Rails 2.0

> Rails 2.1

Page 6: Performance on Rails

Types of Cache Store

Page 7: Performance on Rails

development.rb

Setup up your environment

Page 8: Performance on Rails

3 Types of Caching

Page Caching

will always use File Store

Action Caching & Fragment Caching

Will use the cache store you defined

Page 9: Performance on Rails

Page CachingTransforms your views into pure HTML

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

Page 10: Performance on Rails

Example

Page 11: Performance on Rails

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

Page 12: Performance on Rails

Example

Page 13: Performance on Rails

More options

Page 14: Performance on Rails

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

Page 15: Performance on Rails

Examples of Caching

Page 16: Performance on Rails

Example No caching

Page 17: Performance on Rails

Fragment Caching

Page 18: Performance on Rails

Oops, I forgot about the controller

Page 19: Performance on Rails

the fragment

Page 20: Performance on Rails

Cleaning upindividual cache files

Page 21: Performance on Rails

Cleaning up (brute force)

Page 22: Performance on Rails

Cleaning up with memcached

soon...

Page 23: Performance on Rails

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

Page 24: Performance on Rails

DB Cache (stupid) example

Page 25: Performance on Rails

Memcached

New in Rails 2.0

Simple to use

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

Page 26: Performance on Rails

MethodsRails.cache.read

Rails.cache.write

Rails.cache.fetch

Rails.cache.delete

Rails.cache.exist?

Rails.cache.increment

Rails.cache.decrement

Page 27: Performance on Rails

example

Page 28: Performance on Rails

example with dataModel

Controller

Page 29: Performance on Rails

View (first time)

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

example with data

Page 31: Performance on Rails

Tips for Optimization

Page 32: Performance on Rails

Write Eficient SQL Queries

Bring only what you need

Page 33: Performance on Rails

Eager Loading

Sometimes it’s best to bring all data at once

Prevents N+1 problem

Optimized Eager Loading in Rails 2.1

Page 34: Performance on Rails

Create indexes on DBIt really makes a difference

Don’t exagerate

Page 35: Performance on Rails

Use TransactionsGroup Transactions when possible

Minimizes errors

Less effort on the Database

Page 36: Performance on Rails

Reduce http requests

Too many javascript and css files makes your page slow

You can package all you files into only one

Page 37: Performance on Rails

SolutionsBuilt-in mechanism in Rails 2.0 but without js minification

Alternative plugin - Bundle-FU

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

Page 38: Performance on Rails

Bundling Example Before/After

Page 39: Performance on Rails

Too many images?Use CSS Sprites Technique

Less http requests

More hard to manage

Page 41: Performance on Rails

Tools

Page 42: Performance on Rails

Firebug & Yslow

Track your page performance

Easy to install and run

Download from

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

Page 43: Performance on Rails

Examples

Page 44: Performance on Rails

More examples

Page 46: Performance on Rails

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/

Page 47: Performance on Rails

That’s it! Have fun!

Don’t be afraid to experiment!

Tweak until your satisfied!

If all fails, invest in hardware. :)

Page 48: Performance on Rails

The End

Page 49: Performance on Rails

Thank you

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

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

Pedro Sousa