Ruby/rails performance and profiling
-
Upload
danny-guinther -
Category
Documents
-
view
360 -
download
4
Transcript of Ruby/rails performance and profiling
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
Basics of Benchmarking● Benchmark module● benchmark-ips gem● Beware GC!
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”
Benchmark.realtime && Benchmark.bmbm
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”
Benchmark.ips
Beware GC!
A Thorough Benchmark...
Memory and GC Profiling● GC module● memory_profiler gem
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.
GC.stat
GC::Profiler
memory_profiler gem● Memory profiling routines for Ruby
Head
gem “memory_profiler”require “memory_profiler”
CPU Profiling● ruby-prof gem
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”
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”
A simple example...
RubyProf Reports: Flat ReportShows the total amount of time spent in each
method
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.
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.
RubyProf Reports: Call Stack ReportCreates an HTML representation of the Ruby call stack
RubyProf Reports: Visualizing with KCachegrind
RubyProf Reports: Visualizing with KCachegrind
3 Rules of Rails Profiling
1.Disable GC2.Always profile in
production mode3.Profile at least twice,
ignore first result
Web App Performance● New Relic● rack-mini-profiler gem
New Relic
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.
rack-mini-profiler gem
Ruby Performance Patterns● Modify objects in place● Avoid iterators that allocate additional
objects● Beware String allocations● Write less Ruby
Modify objects in place
Beware of iterators that make extra allocations
Beware of iterators that make extra allocations
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
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.
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