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?
• Demonstration
Different ways of
looking at performance

• Client perceived
• Server
Client

                                                      • slow loading
                                                        web page
                                                        (latency)
                                                      • slow executing
                                                        webpage
                                                        (javascript/css)


http://www.flickr.com/photos/7423510@N06/2984486385/
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.com/photos/mezzoblue/3217540317/
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
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" />
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/298998011/
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.flickr.com/photos/tpapi/2765541278/
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
   end


How many queries does this execute?
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?
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
 • ree -- gc.enable stats
 • jruby
 • ruby 1.9
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
Tools

• http://documentcloud.github.com/jammit/
• http://jeremy.zawodny.com/mysql/mytop/
• http://htop.sourceforge.net/
• http://code.google.com/p/memcache-top/
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.
Rails caching model
Template rendering
       pain
Database Model Fields

• wasteful data types
 • varchars
 •
Evented API calls

Rails Performance Tricks and Treats

Editor's Notes

  • #3 Tell a nice little story about how I got into optimizations
  • #4 ASK: What is performance: take a survey group items on board between client and server metrics
  • #8 ASK: What are your favorite client tools
  • #17 ASK: What are your favorite server tools?
  • #18 ASK: What are some other infrastructure tools more tools here than you can shake a stick at.
  • #19 http://jeremy.zawodny.com/mysql/mytop/ http://htop.sourceforge.net/ http://code.google.com/p/memcache-top/
  • #20 http://jeremy.zawodny.com/mysql/mytop/ http://htop.sourceforge.net/ http://code.google.com/p/memcache-top/
  • #21 http://jeremy.zawodny.com/mysql/mytop/ http://htop.sourceforge.net/ http://code.google.com/p/memcache-top/
  • #29 Rack-a-mole
  • #37 check 37signals twitter and others for publicly posted gc settings