Rails Performance
Tricks and Treats
     Marshall Yount
      Yount Labs
Everything I
really need to
 know about
     code
optimization I
  learned by
playing Quake
The Big Picture

• What is Performance?
• How do we know we’re performant?
• What tools do we use?
• How do we fix problems...
Different ways of
looking at performance

• Client perceived
• Server
Client

                                                      • slow loading
                                             ...
The Enemy




 Latency: It’s a bitch
The Enemy




http://www.flickr.com/photos/peterhaza/3492771467/



           Latency: It’s a bitch
Diagnostic Tools
         (Client)
• Firebug
• YSlow
• PageSpeed
• UserTime
Diagnostic Tools
         (Client)
• Firebug
• YSlow
• PageSpeed
• UserTime
Diagnostic Tools
        (Client)
• Firebug
• YSlow
• PageSpeed
• UserTime
Diagnostic Tools
        (Client)
• Firebug
• YSlow
• PageSpeed
• UserTime
Reducing Roundtrips

• CSS Sprites
• CSS Data URIs
  • Jammit
• Rails *_link_tag

                     http://www.flickr.co...
Reducing Roundtrips

• CSS Sprites        ul.checklist li.complete { margin-left: 20px; background:


• CSS Data URIs     ...
Reducing Roundtrips

                     <%= stylesheet_link_tag "main", "nav",
•   CSS Sprites
                     "blo...
HTTP Hacking for Fun
     and Profit
• Off with her <HEAD>
• Expires:
• Last-Modified:
• ETag:
• Content-Encoding:
Server
• registers
• memory
• network
• database
• disk
• APIs
                 http://www.flickr.com/photos/buro9/29899801...
Infrastructure

• squid 2.7
• varnish
• memcached
• redis / resque
Top Clones

• top
• htop
• mytop
• memcache-top
Top Clones

• top
• htop
• mytop
• memcache-top
Top Clones

• top
• htop
• mytop
• memcache-top
Top Clones

• top
• htop
• mytop
• memcache-top
Monitoring -- free

• nagios
• cacti
• zabbix
• zenoss (core)
Monitoring -- free

• nagios
• cacti
• zabbix
• zenoss (core)
Monitoring -- free

• nagios
• cacti
• zabbix
• zenoss (core)
Monitoring -- free

• nagios
• cacti
• zabbix
• zenoss (core)
Monitoring -- paid

• Pingdom    Text
• NewRelic
• Scout
Monitoring -- paid

• Pingdom
• NewRelic
• Scout
Monitoring -- paid

• Pingdom
• NewRelic
• Scout
Fixing Problems

• Performance is a
  moving target!
• Horizontal/
  Vertical scaling


                     http://www.fli...
NewRelic Workflow
• Web Transactions
 • Most Time
    Consuming
 • Slowest
    Average
    Response Time
• Database
And the Number One
    Ruby on Rails
Performance Problem
        Is ...
And the Number One
    Ruby on Rails
Performance Problem
        Is ...
    N+1 Select
N+1 Select
   photos.each do |photo|
      puts photo.rating.id
      puts photo.user.email
      puts photo.camera.name
 ...
N+1 Select
photos = Photo.find(:all, :include => [:rating, :user, :email])
              photos.each do |photo|
           ...
Plugins/Gems

• bullet
• slim_scrooge
• query_reviewer
• ambitious_query_indexer
• ActiveRecord::Extensions
Still not fast enough for
            ya?
• ruby-prof
• memprof - where are objects being created
• change interpreters
 •...
Readings

•   Mark Nottingham’s HTTP caching tutorial

•              http://www.mnot.net/cache_docs/

•                  ...
Tools

• http://documentcloud.github.com/jammit/
• http://jeremy.zawodny.com/mysql/mytop/
• http://htop.sourceforge.net/
•...
Appendix

• The following incomplete slides were
  suggested by audience members at the
  Dallas Ruby Brigade. I’ll be fles...
Rails caching model
Template rendering
       pain
Database Model Fields

• wasteful data types
 • varchars
 •
Evented API calls
Upcoming SlideShare
Loading in...5
×

Rails Performance Tricks and Treats

2,947

Published on

Published in: Technology
2 Comments
4 Likes
Statistics
Notes
  • 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
No Downloads
Views
Total Views
2,947
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
36
Comments
2
Likes
4
Embeds 0
No embeds

No notes for slide

  • Tell a nice little story about how I got into optimizations
  • ASK: What is performance: take a survey
    group items on board between client and server metrics



  • ASK: What are your favorite client tools








  • ASK: What are your favorite server tools?
  • ASK: What are some other infrastructure tools
    more tools here than you can shake a stick at.

  • http://jeremy.zawodny.com/mysql/mytop/
    http://htop.sourceforge.net/
    http://code.google.com/p/memcache-top/

  • http://jeremy.zawodny.com/mysql/mytop/
    http://htop.sourceforge.net/
    http://code.google.com/p/memcache-top/

  • http://jeremy.zawodny.com/mysql/mytop/
    http://htop.sourceforge.net/
    http://code.google.com/p/memcache-top/








  • Rack-a-mole







  • check 37signals twitter and others for publicly posted gc settings








  • 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
    1. A particular slide catching your eye?

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

    ×