Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Web Performance and Optimization in Rails

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Web Performance and Optimization in Rails

How do we measure our application performance?
How can we optimize our application?
This presentation covers *backend* and *frontend* with a focus on Ruby on Rails.

Avatar for Constantin Hofstetter

Constantin Hofstetter

May 20, 2015
Tweet

Other Decks in Technology

Transcript

  1. Backend::Measure Does the application have a memory leak?
 What causes

    the memory leak? How Ruby Uses Memory
 http://www.sitepoint.com/ruby-uses-memory/
  2. Backend::Optimize lol_dba gem install lol_dba Scan application for missing indices

    postgres query to find missing indices:
 http://is.gd/blue_elephant_pg
  3. Frontend::Optimize::StaticAssets Optimize Delivery • Leverage browser caching • Store on

    file storage web service (e.g. S3) • Use content delivery network (e.g. CloudFront) Create an S3 bucket and a CloudFront instance
 that points to the bucket. Use the asset_sync gem. Set config.action_controller.asset_host.
  4. Frontend::Optimize::StaticAssets # in your asset_sync.rb config.custom_headers = { # for

    fonts and svg, you have to set Access-Control-Allow-Origin '.*\.[svg|woff|ttf|eot]' => { 'Access-Control-Allow-Origin' => '*', cache_control: "public, max-age=#{ 1.month.to_i }", expires: nil }, # Everything else, set cache-control to max-age 1 month '.*' => { cache_control: "public, max-age=#{ 1.month.to_i }", expires: nil } } asset_sync • Set CacheControl (time in seconds the client is allowed to cache
 the resource). Don’t use Expires (future date) • Set Access-Control-Allow-Origin for fonts and SVG
  5. Frontend::Optimize::StaticAssets # in your asset_sync.rb config.gzip_compression = true Text compression

    • Enable gzip_compression • S3 will deliver compressed version if GET request
 includes `Accept-Encoding: gzip` Both, the original and gzipped
 version will be synced to S3.
  6. Frontend::Optimize::StaticAssets Image compression • Lossless and (optional) lossy compression •

    ImageOptim (drag and drop assets/images folder) • ImageOptim-CLI with git pre-commit hook # in your_project/.git/hooks/pre-commit images=$(git diff --exit-code --cached --name-only
 --diff-filter=ACM -- '*.png' ‘*.jpg' ‘*.jpeg' '*.gif')
 $(exit $?) || echo $images | imageoptim && git add $images Also converts PNG-24 to
 PNG-8+alpha images.
  7. Frontend::Optimize::FileUploads Optimize Upload • Have browser client upload directly to

    S3 • Add ActiveJob to postprocess files • Resize images on client (e.g. jQuery-File-Upload)
  8. Frontend::Optimize::ImageUploads Minimize Payload Optimize images when post-processing # in config/initializers/carrierwave.rb

    module CarrierWave::MiniMagick def optimize manipulate! do |img| img.strip img.interlace(“Plane”) # makes image progressive if JPG img.quality(90) img = yield(img) if block_given? img end end end # in ImageUploader version :preview do process :optimize end Thumbnails should be
 stored as baseline and
 not as progressive JPGs.
  9. Frontend::Optimize::ImageUploads Optimize Delivery • Store on file storage web service

    (e.g. S3) • Use content delivery network (e.g. CloudFront) • Leverage browser caching # in config/initializers/carrierwave.rb CarrierWave.configure do |config| config.asset_host = “http://d12345678.cloudfront.net” config.fog_attributes = { 'Cache-Control' => 1.month.to_i } … end
  10. Frontend::Optimize Turbolinks is great! It’s even better with Turbolinks.enableTransitionCache() Other

    Optimizations • Minimize external resources (e.g. Google Fonts) • Use Turbolinks
  11. HTTP/2 multiplexing serve multiple resources in one response binary based

    protocol – allows for smaller payload header compression – compression to the max server push – proactively send resources to client
  12. Backend • Profile application • Use tools for quick
 wins

    Frontend • Webpagetest etc. • Compress resources • Use CDN Things to come • Keep HTTP/2
 features in mind