4. 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”
6. 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”
11. GC Module
GC#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.
16. ruby-prof gem
A 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”
17. ruby-prof gem
Modes:
● 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”
20. RubyProf Reports: Flat Report
Shows 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.
21. RubyProf Reports: Graph Report
Shows 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 method
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.
child - The time spent in this method's children.
calls - The number of times this method was called.
name - The name of the method.
22. RubyProf Reports: Call Stack Report
Creates an HTML representation of the Ruby call stack
28. 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.
34. Rails Performance Patterns
● ActiveRecord
o Take only what you need using #select
and #pluck
o Eager loading using #includes
o Let the database do the work
● ActionView
o Avoid rendering partials in a loop
● Use caching where it makes sense
35. 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.
36. 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