Performance on Rails
-
Upload
pedro-sousa -
Category
Technology
-
view
2.891 -
download
0
description
Transcript of Performance on Rails
![Page 1: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/1.jpg)
Ruby on Rails Performance
Tips
Barcamp Coimbra 2008 Pedro Sousa
![Page 2: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/2.jpg)
Your application is slow
![Page 3: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/3.jpg)
Caching in RoR
![Page 4: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/5.jpg)
Setup up your environment
<= Rails 2.0
> Rails 2.1
![Page 6: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/6.jpg)
Types of Cache Store
![Page 7: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/7.jpg)
development.rb
Setup up your environment
![Page 8: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/10.jpg)
Example
![Page 11: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/12.jpg)
Example
![Page 13: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/13.jpg)
More options
![Page 14: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/15.jpg)
Examples of Caching
![Page 16: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/16.jpg)
Example No caching
![Page 17: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/17.jpg)
Fragment Caching
![Page 18: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/18.jpg)
Oops, I forgot about the controller
![Page 19: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/19.jpg)
the fragment
![Page 20: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/20.jpg)
Cleaning upindividual cache files
![Page 21: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/21.jpg)
Cleaning up (brute force)
![Page 22: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/22.jpg)
Cleaning up with memcached
soon...
![Page 23: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/24.jpg)
DB Cache (stupid) example
![Page 25: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/25.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/27.jpg)
example
![Page 28: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/28.jpg)
example with dataModel
Controller
![Page 29: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/29.jpg)
View (first time)
View (second time, it’s read from the cache)
example with data
![Page 30: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/30.jpg)
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
![Page 31: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/31.jpg)
Tips for Optimization
![Page 32: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/32.jpg)
Write Eficient SQL Queries
Bring only what you need
![Page 33: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/33.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/34.jpg)
Create indexes on DBIt really makes a difference
Don’t exagerate
![Page 35: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/35.jpg)
Use TransactionsGroup Transactions when possible
Minimizes errors
Less effort on the Database
![Page 36: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/36.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/37.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/38.jpg)
Bundling Example Before/After
![Page 39: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/39.jpg)
Too many images?Use CSS Sprites Technique
Less http requests
More hard to manage
![Page 40: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/40.jpg)
References aboutCSS Sprites
http://css-tricks.com/css-sprites-what-they-are-why-theyre-cool-and-how-to-use-them/
http://www.alistapart.com/articles/sprites
![Page 41: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/41.jpg)
Tools
![Page 42: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/42.jpg)
Firebug & Yslow
Track your page performance
Easy to install and run
Download from
http://developer.yahoo.com/yslow/
![Page 43: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/43.jpg)
Examples
![Page 44: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/44.jpg)
More examples
![Page 45: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/45.jpg)
TextMate FootnotesUseful plugin for Rails
Download from
http://github.com/drnic/rails-footnotes/tree/master
![Page 46: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/46.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/47.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/48.jpg)
The End
![Page 49: Performance on Rails](https://reader033.fdocuments.us/reader033/viewer/2022052522/554a39a4b4c905863d8b483d/html5/thumbnails/49.jpg)
Thank you
[blog] http://www.reinventar.com[work] http://www.thinkorange.pt
[linkedin] http://www.linkedin.com/psousa
Pedro Sousa