Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Rails Performance Tricks and Treats

3,463 views

Published on

Published in: Technology
  • exellent!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • rails
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Rails Performance Tricks and Treats

  1. 1. Rails Performance Tricks and Treats Marshall Yount Yount Labs
  2. 2. Everything I really need to know about code optimization I learned by playing Quake
  3. 3. The Big Picture • What is Performance? • How do we know we’re performant? • What tools do we use? • How do we fix problems? • Demonstration
  4. 4. Different ways of looking at performance • Client perceived • Server
  5. 5. Client • slow loading web page (latency) • slow executing webpage (javascript/css) http://www.flickr.com/photos/7423510@N06/2984486385/
  6. 6. The Enemy Latency: It’s a bitch
  7. 7. The Enemy http://www.flickr.com/photos/peterhaza/3492771467/ Latency: It’s a bitch
  8. 8. Diagnostic Tools (Client) • Firebug • YSlow • PageSpeed • UserTime
  9. 9. Diagnostic Tools (Client) • Firebug • YSlow • PageSpeed • UserTime
  10. 10. Diagnostic Tools (Client) • Firebug • YSlow • PageSpeed • UserTime
  11. 11. Diagnostic Tools (Client) • Firebug • YSlow • PageSpeed • UserTime
  12. 12. Reducing Roundtrips • CSS Sprites • CSS Data URIs • Jammit • Rails *_link_tag http://www.flickr.com/photos/mezzoblue/3217540317/
  13. 13. Reducing Roundtrips • CSS Sprites ul.checklist li.complete { margin-left: 20px; background: • CSS Data URIs url('data:image/ png;base64,AAAAFGGFyjOXZtQAAAAAElFT...') • Jammit top left no-repeat; } • Rails *_link_tag
  14. 14. Reducing Roundtrips <%= stylesheet_link_tag "main", "nav", • CSS Sprites "blog", :cache => “pagename_all” %> • CSS Data URIs <link href="/stylesheets/ • Jammit pagename_all.css?1234567890" media="screen" rel="stylesheet" • Rails *_link_tag type="text/css" />
  15. 15. HTTP Hacking for Fun and Profit • Off with her <HEAD> • Expires: • Last-Modified: • ETag: • Content-Encoding:
  16. 16. Server • registers • memory • network • database • disk • APIs http://www.flickr.com/photos/buro9/298998011/
  17. 17. Infrastructure • squid 2.7 • varnish • memcached • redis / resque
  18. 18. Top Clones • top • htop • mytop • memcache-top
  19. 19. Top Clones • top • htop • mytop • memcache-top
  20. 20. Top Clones • top • htop • mytop • memcache-top
  21. 21. Top Clones • top • htop • mytop • memcache-top
  22. 22. Monitoring -- free • nagios • cacti • zabbix • zenoss (core)
  23. 23. Monitoring -- free • nagios • cacti • zabbix • zenoss (core)
  24. 24. Monitoring -- free • nagios • cacti • zabbix • zenoss (core)
  25. 25. Monitoring -- free • nagios • cacti • zabbix • zenoss (core)
  26. 26. Monitoring -- paid • Pingdom Text • NewRelic • Scout
  27. 27. Monitoring -- paid • Pingdom • NewRelic • Scout
  28. 28. Monitoring -- paid • Pingdom • NewRelic • Scout
  29. 29. Fixing Problems • Performance is a moving target! • Horizontal/ Vertical scaling http://www.flickr.com/photos/tpapi/2765541278/
  30. 30. NewRelic Workflow • Web Transactions • Most Time Consuming • Slowest Average Response Time • Database
  31. 31. And the Number One Ruby on Rails Performance Problem Is ...
  32. 32. And the Number One Ruby on Rails Performance Problem Is ... N+1 Select
  33. 33. N+1 Select photos.each do |photo| puts photo.rating.id puts photo.user.email puts photo.camera.name end How many queries does this execute?
  34. 34. N+1 Select photos = Photo.find(:all, :include => [:rating, :user, :email]) photos.each do |photo| puts photo.rating.id puts photo.user.email puts photo.camera.name end How many queries does this execute?
  35. 35. Plugins/Gems • bullet • slim_scrooge • query_reviewer • ambitious_query_indexer • ActiveRecord::Extensions
  36. 36. Still not fast enough for ya? • ruby-prof • memprof - where are objects being created • change interpreters • ree -- gc.enable stats • jruby • ruby 1.9
  37. 37. Readings • Mark Nottingham’s HTTP caching tutorial • http://www.mnot.net/cache_docs/ • YSlow Best Practices • http://developer.yahoo.com/performance/rules.html • http://delicious.com/marshall.yount/performance
  38. 38. Tools • http://documentcloud.github.com/jammit/ • http://jeremy.zawodny.com/mysql/mytop/ • http://htop.sourceforge.net/ • http://code.google.com/p/memcache-top/
  39. 39. Appendix • The following incomplete slides were suggested by audience members at the Dallas Ruby Brigade. I’ll be fleshing these out and adding them to a future version of the presentation.
  40. 40. Rails caching model
  41. 41. Template rendering pain
  42. 42. Database Model Fields • wasteful data types • varchars •
  43. 43. Evented API calls

×