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 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.
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 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”
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”
A simple example...
RubyProf Reports: Flat Report
Shows the total amount of time spent in each method
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.
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.
RubyProf Reports: Call Stack Report
Creates 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 GC
2.Always profile in production
mode
3.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 #includes
o Let the database do the work
● ActionView
o 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

More Related Content

ODP
Performance Optimization of Rails Applications
PDF
Rails Application Optimization Techniques & Tools
PDF
Rails Performance
PDF
[231] the simplicity of cluster apps with circuit
PDF
mruby で mackerel のプラグインを作るはなし
PDF
20141210 rakuten techtalk
PDF
How to Begin Developing Ruby Core
PDF
Practical ngx_mruby
Performance Optimization of Rails Applications
Rails Application Optimization Techniques & Tools
Rails Performance
[231] the simplicity of cluster apps with circuit
mruby で mackerel のプラグインを作るはなし
20141210 rakuten techtalk
How to Begin Developing Ruby Core
Practical ngx_mruby

What's hot (20)

PDF
How to test code with mruby
PDF
The details of CI/CD environment for Ruby
KEY
Leave end-to-end testing to Capybara
PDF
Large-scaled Deploy Over 100 Servers in 3 Minutes
PDF
Middleware as Code with mruby
PPTX
Run Node Run
PDF
The OMR GC talk - Ruby Kaigi 2015
PPT
Find bottleneck and tuning in Java Application
PDF
Tuning Solr for Logs
PDF
Gate of Agile Web Development
PDF
Python Load Testing - Pygotham 2012
PPTX
Tale of Kafka Consumer for Spark Streaming
PDF
How to Begin to Develop Ruby Core
PDF
Mасштабирование микросервисов на Go, Matt Heath (Hailo)
PPTX
Usenix lisa 2011
PDF
Javascript TDD with Jasmine, Karma, and Gulp
ODP
30 Minutes To CPAN
PDF
DSLing your System For Scalability Testing Using Gatling - Dublin Scala User ...
PPTX
Toolbox of a Ruby Team
PDF
Middleware as Code with mruby
How to test code with mruby
The details of CI/CD environment for Ruby
Leave end-to-end testing to Capybara
Large-scaled Deploy Over 100 Servers in 3 Minutes
Middleware as Code with mruby
Run Node Run
The OMR GC talk - Ruby Kaigi 2015
Find bottleneck and tuning in Java Application
Tuning Solr for Logs
Gate of Agile Web Development
Python Load Testing - Pygotham 2012
Tale of Kafka Consumer for Spark Streaming
How to Begin to Develop Ruby Core
Mасштабирование микросервисов на Go, Matt Heath (Hailo)
Usenix lisa 2011
Javascript TDD with Jasmine, Karma, and Gulp
30 Minutes To CPAN
DSLing your System For Scalability Testing Using Gatling - Dublin Scala User ...
Toolbox of a Ruby Team
Middleware as Code with mruby
Ad

Similar to Ruby/rails performance and profiling (20)

PDF
Ruby performance - The low hanging fruit
PDF
Profiling Ruby
PDF
Профилирование и оптимизация производительности Ruby-кода
PDF
Profiling and monitoring ruby & rails applications
PDF
Profiling ruby
PDF
Практический опыт профайлинга и оптимизации производительности Ruby-приложений
PDF
Ruby memory tips and tricks
ODP
Debugging and Profiling Rails Application
PDF
Rubinius - What Have You Done For Me Lately?
PDF
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes
PPT
Rails performance: Ruby GC tweaking
PPTX
Ruby3x3: How are we going to measure 3x
PPTX
Ruby/Rails Performance Tips
DOCX
Rails Concept
PPT
WorkinOnTheRailsRoad
PPT
Workin ontherailsroad
KEY
Nyt Prof 200910
KEY
Rails performance at Justin.tv - Guillaume Luccisano
PDF
Rubyconf presentation
ZIP
Rubinius 1.0 and more!
Ruby performance - The low hanging fruit
Profiling Ruby
Профилирование и оптимизация производительности Ruby-кода
Profiling and monitoring ruby & rails applications
Profiling ruby
Практический опыт профайлинга и оптимизации производительности Ruby-приложений
Ruby memory tips and tricks
Debugging and Profiling Rails Application
Rubinius - What Have You Done For Me Lately?
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes
Rails performance: Ruby GC tweaking
Ruby3x3: How are we going to measure 3x
Ruby/Rails Performance Tips
Rails Concept
WorkinOnTheRailsRoad
Workin ontherailsroad
Nyt Prof 200910
Rails performance at Justin.tv - Guillaume Luccisano
Rubyconf presentation
Rubinius 1.0 and more!
Ad

Ruby/rails performance and profiling

  • 2. And the results are in... ● Benchmark Basics ● Memory/GC Profiling ● CPU Profiling ● Web App Performance ● Ruby Performance Patterns ● Rails Performance Patterns
  • 3. Basics of Benchmarking ● Benchmark module ● benchmark-ips gem ● Beware GC!
  • 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”
  • 10. Memory and GC Profiling ● GC module ● memory_profiler gem
  • 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.
  • 14. memory_profiler gem ● Memory profiling routines for Ruby Head gem “memory_profiler” require “memory_profiler”
  • 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”
  • 19. RubyProf Reports: Flat Report Shows the total amount of time spent in each method
  • 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
  • 23. RubyProf Reports: Visualizing with KCachegrind
  • 24. RubyProf Reports: Visualizing with KCachegrind
  • 25. 3 Rules of Rails Profiling 1.Disable GC 2.Always profile in production mode 3.Profile at least twice, ignore first result
  • 26. Web App Performance ● New Relic ● rack-mini-profiler gem
  • 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.
  • 30. Ruby Performance Patterns ● Modify objects in place ● Avoid iterators that allocate additional objects ● Beware String allocations ● Write less Ruby
  • 32. Beware of iterators that make extra allocations
  • 33. Beware of iterators that make extra allocations
  • 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