Your SlideShare is downloading. ×
0
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
environm...
Setup up your
environment
   > Rails 2.1




   <= Rails 2.0
Types of
Cache Store
Setup up your
environment

  development.rb
3 Types of Caching

Page Caching

 will always use File Store

Action Caching & Fragment Caching

 Will use the cache stor...
Page Caching
Transforms your views into pure HTML

Best for pages with common content for all
your users or very static pa...
Example
Action Caching

Works the same as way as page caching ...

but goes through dispatcher to run filters and
callbacks

For ex...
Example
More options
Fragment Caching

More flexible

Great for dynamic content directed at specific
users or conditions (ex: your friends latest...
Examples of Caching
Example No caching
Fragment Caching
Oops, I forgot about
  the controller
the fragment
Cleaning up
individual 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... ...
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
Methods
Rails.cache.read

Rails.cache.write

Rails.cache.fetch

Rails.cache.delete

Rails.cache.exist?

Rails.cache.increm...
example
example with data
        Model




      Controller
example with data
            View (first time)




View (second time, it’s read from the cache)
More info on caching
 http://www.railsenvy.com/2007/2/28/rails-caching-
                      tutorial
       http://rails...
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 DB
It really makes a difference

Don’t exagerate
Use Transactions
Group 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
Solutions
Built-in mechanism in Rails 2.0 but without js
minification

Alternative plugin - Bundle-FU

  http://code.google...
Bundling Example
  Before/After
Too many images?
Use CSS Sprites Technique

Less http requests

More hard to manage
References about
           CSS Sprites

http://css-tricks.com/css-sprites-what-they-are-why-
         theyre-cool-and-how...
Tools
Firebug & Yslow

Track your page performance

Easy to install and run

Download from

  http://developer.yahoo.com/yslow/
Examples
More examples
TextMate Footnotes
Useful plugin for Rails

Download from

  http://github.com/drnic/rails-footnotes/
  tree/master
Other Small Tips
Use LibXML-Ruby for processing XML:

 http://libxml.rubyforge.org/

Log only what you need

Use a CDN (ex...
That’s it!
Have fun!
 Don’t be afraid to
   experiment!

 Tweak until your
    satisfied!

If all fails, invest in
    har...
The End
Thank you

              Pedro Sousa




     [blog] http://www.reinventar.com
     [work] http://www.thinkorange.pt
[link...
Upcoming SlideShare
Loading in...5
×

Performance on Rails

2,907

Published on

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

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,907
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
74
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Transcript of "Performance on Rails"

  1. 1. Ruby on Rails Performance Tips Barcamp Coimbra 2008 Pedro Sousa
  2. 2. Your application is slow
  3. 3. Caching in RoR
  4. 4. 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
  5. 5. Setup up your environment > Rails 2.1 <= Rails 2.0
  6. 6. Types of Cache Store
  7. 7. Setup up your environment development.rb
  8. 8. 3 Types of Caching Page Caching will always use File Store Action Caching & Fragment Caching Will use the cache store you defined
  9. 9. Page Caching Transforms your views into pure HTML Best for pages with common content for all your users or very static pages
  10. 10. Example
  11. 11. 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
  12. 12. Example
  13. 13. More options
  14. 14. 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
  15. 15. Examples of Caching
  16. 16. Example No caching
  17. 17. Fragment Caching
  18. 18. Oops, I forgot about the controller
  19. 19. the fragment
  20. 20. Cleaning up individual cache files
  21. 21. Cleaning up (brute force)
  22. 22. Cleaning up with memcached soon...
  23. 23. 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
  24. 24. DB Cache (stupid) example
  25. 25. Memcached New in Rails 2.0 Simple to use It’s like a Hash in memory, has a key/value way of working
  26. 26. Methods Rails.cache.read Rails.cache.write Rails.cache.fetch Rails.cache.delete Rails.cache.exist? Rails.cache.increment Rails.cache.decrement
  27. 27. example
  28. 28. example with data Model Controller
  29. 29. example with data View (first time) View (second time, it’s read from the cache)
  30. 30. More info on caching http://www.railsenvy.com/2007/2/28/rails-caching- tutorial http://railscasts.com/episodes/115 http://www.thewebfellas.com/blog/2008/6/9/rails-2-1- now-with-better-integrated-caching http://www.akitaonrails.com/2008/5/25/rolling-with- rails-2-1-the-first-full-tutorial-part-1
  31. 31. Tips for Optimization
  32. 32. Write Eficient SQL Queries Bring only what you need
  33. 33. Eager Loading Sometimes it’s best to bring all data at once Prevents N+1 problem Optimized Eager Loading in Rails 2.1
  34. 34. Create indexes on DB It really makes a difference Don’t exagerate
  35. 35. Use Transactions Group Transactions when possible Minimizes errors Less effort on the Database
  36. 36. Reduce http requests Too many javascript and css files makes your page slow You can package all you files into only one
  37. 37. Solutions Built-in mechanism in Rails 2.0 but without js minification Alternative plugin - Bundle-FU http://code.google.com/p/bundle-fu/
  38. 38. Bundling Example Before/After
  39. 39. Too many images? Use CSS Sprites Technique Less http requests More hard to manage
  40. 40. References about CSS Sprites http://css-tricks.com/css-sprites-what-they-are-why- theyre-cool-and-how-to-use-them/ http://www.alistapart.com/articles/sprites
  41. 41. Tools
  42. 42. Firebug & Yslow Track your page performance Easy to install and run Download from http://developer.yahoo.com/yslow/
  43. 43. Examples
  44. 44. More examples
  45. 45. TextMate Footnotes Useful plugin for Rails Download from http://github.com/drnic/rails-footnotes/ tree/master
  46. 46. 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/
  47. 47. That’s it! Have fun! Don’t be afraid to experiment! Tweak until your satisfied! If all fails, invest in hardware. :)
  48. 48. The End
  49. 49. Thank you Pedro Sousa [blog] http://www.reinventar.com [work] http://www.thinkorange.pt [linkedin] http://www.linkedin.com/psousa
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×