Ruby/rails performance and profiling

36
Ruby/Rails Performance and Profiling

Transcript of Ruby/rails performance and profiling

Page 1: Ruby/rails performance and profiling

Ruby/Rails Performance and Profiling

Page 2: Ruby/rails performance and profiling

And the results are in...

● Benchmark Basics● Memory/GC Profiling● CPU Profiling● Web App Performance● Ruby Performance

Patterns● Rails Performance

Patterns

Page 3: Ruby/rails performance and profiling

Basics of Benchmarking● Benchmark module● benchmark-ips gem● Beware GC!

Page 4: Ruby/rails performance and profiling

Benchmark module

The Benchmark module provides methods to measure and report the

time used to execute Ruby code.

Benchmark.realtime - Returns the elapsed real time used to execute the given block.Benchmark.bmbm - Attempts to reduce noise in benchmarks by running benchmarks twice: once to warm up the runtime environment and once for real.

require “benchmark”

Page 5: Ruby/rails performance and profiling

Benchmark.realtime && Benchmark.bmbm

Page 6: Ruby/rails performance and profiling

benchmark-ips gem● IPS == Iterations Per Second● Similar to Benchmark.bmbm, but

executes the given block as many times as possible per second

● Also includes warm-up period● More configurable● Includes margin of errorgem “benchmark-ips”

require “benchmark/ips”

Page 7: Ruby/rails performance and profiling

Benchmark.ips

Page 8: Ruby/rails performance and profiling

Beware GC!

Page 9: Ruby/rails performance and profiling

A Thorough Benchmark...

Page 10: Ruby/rails performance and profiling

Memory and GC Profiling● GC module● memory_profiler gem

Page 11: Ruby/rails performance and profiling

GC ModuleGC#stat - Returns a Hash of information about the GC.

GC::Profiler - Provides access to information on GC runs including time, length and object space size.

Page 12: Ruby/rails performance and profiling

GC.stat

Page 13: Ruby/rails performance and profiling

GC::Profiler

Page 14: Ruby/rails performance and profiling

memory_profiler gem● Memory profiling routines for Ruby

Head

gem “memory_profiler”require “memory_profiler”

Page 15: Ruby/rails performance and profiling

CPU Profiling● ruby-prof gem

Page 16: Ruby/rails performance and profiling

ruby-prof gemA fast code profiler for Ruby● Fast - C extension so, many times faster than the

standard Ruby profiler.● Modes - Can measure a number of different

parameters, including call times, memory usage and object allocations.

● Reports - can generate many types of reports.

gem “ruby-prof”require “ruby-prof”

Page 17: Ruby/rails performance and profiling

ruby-prof gemModes:● wall time (RubyProf::WALL_TIME)● process time (RubyProf::PROCESS_TIME)● cpu time (RubyProf::CPU_TIME)*● object allocations (RubyProf::ALLOCATIONS) ● memory usage (RubyProf::MEMORY) **● garbage collection time (RubyProf::GC_TIME) **● garbage collections runs (RubyProf::GC_RUNS) **

* Linux only** Requires patched Ruby

gem “ruby-prof”require “ruby-prof”

Page 18: Ruby/rails performance and profiling

A simple example...

Page 19: Ruby/rails performance and profiling

RubyProf Reports: Flat ReportShows the total amount of time spent in each

method

Page 20: Ruby/rails performance and profiling

RubyProf Reports: Flat ReportShows the total amount of time spent in each

method

%self - The percentage of time spent in this method, derived from self_time/total_time

total - The time spent in this method and its children.self - The time spent in this method.wait - The time the thread spent waiting on other threads.children - The time spent in this method's children.calls - The number of times this method was called.self/call - The average time spent per call in this method.total/call - The average time spent per call in this method and its children.name - The name of the method.

Page 21: Ruby/rails performance and profiling

RubyProf Reports: Graph ReportShows the total amount of time spent in each

method

%total - The percentage of time spent in this method and its children%self - The percentage of time spent in this methodtotal - The time spent in this method and its children.self - The time spent in this method.wait - The time the thread spent waiting on other threads.child - The time spent in this method's children.calls - The number of times this method was called.name - The name of the method.

Page 22: Ruby/rails performance and profiling

RubyProf Reports: Call Stack ReportCreates an HTML representation of the Ruby call stack

Page 23: Ruby/rails performance and profiling

RubyProf Reports: Visualizing with KCachegrind

Page 24: Ruby/rails performance and profiling

RubyProf Reports: Visualizing with KCachegrind

Page 25: Ruby/rails performance and profiling

3 Rules of Rails Profiling

1.Disable GC2.Always profile in

production mode3.Profile at least twice,

ignore first result

Page 26: Ruby/rails performance and profiling

Web App Performance● New Relic● rack-mini-profiler gem

Page 27: Ruby/rails performance and profiling

New Relic

Page 28: Ruby/rails performance and profiling

rack-mini-profiler gem

gem “rack-mini-profiler”require “rack-mini-profiler”

Middleware that displays speed badge and flame-graph for every HTML page.● Can be useful in development● Useful for examining a particular page● Tends to be less useful in environments that use

New Relic.

Page 29: Ruby/rails performance and profiling

rack-mini-profiler gem

Page 30: Ruby/rails performance and profiling

Ruby Performance Patterns● Modify objects in place● Avoid iterators that allocate additional

objects● Beware String allocations● Write less Ruby

Page 31: Ruby/rails performance and profiling

Modify objects in place

Page 32: Ruby/rails performance and profiling

Beware of iterators that make extra allocations

Page 33: Ruby/rails performance and profiling

Beware of iterators that make extra allocations

Page 34: Ruby/rails performance and profiling

Rails Performance Patterns● ActiveRecord

o Take only what you need using #select and #pluck

o Eager loading using #includeso Let the database do the work

● ActionViewo Avoid rendering partials in a loop

● Use caching where it makes sense

Page 35: Ruby/rails performance and profiling

Other tools

stackprof - A fast sampling profiler for ruby code, with cpu, wallclock and object allocation samplers.

allocation_tracer - Similar to memory_profiler but focused on "age" of objects. Written by Koichi Sasada.

Page 36: Ruby/rails performance and profiling

Additional Resources● Module: Benchmark - RubyDoc.org:

http://ruby-doc.org/stdlib-2.0.0/libdoc/benchmark/rdoc/Benchmark.html

● Module: GC - RubyDoc.org: http://ruby-doc.org/core-2.0.0/GC.html● Performance Testing Rails Applications - Rails Guide:

http://guides.rubyonrails.org/v3.2.13/performance_testing.html● Rails Performance - What You Need To Know:

https://www.airpair.com/ruby-on-rails/performance● Ruby Performance Optimization: Why Ruby Is Slow, and How to Fix

It by Alexander Dymo: https://pragprog.com/book/adrpo/ruby-performance-optimization

● GORUCO 2015 - How to Performance by Eileen Uchitelle: https://www.youtube.com/watch?v=oT74HLvDo_A