$30 off During Our Annual Pro Sale. View details »

Debugging and Profiling Rails App

Debugging and Profiling Rails App

Overview of Debugging and Profiling tools for Rails

David Paluy

January 28, 2013
Tweet

More Decks by David Paluy

Other Decks in How-to & DIY

Transcript

  1. Debugging and Profiling Rails App David Paluy January 2013

  2. None
  3. Ruby is eating RAM

  4. Agenda • “Winter is coming!” • Garbage Collector • Debug

    Tools • Profiling Tools
  5. The Task: Send ~30,000 e-mails

  6. Result Before

  7. How Ruby Works? Physical RAM Process Heap Ruby Heap Ruby

    Heap Ruby Object Ruby Object Ruby Object Ruby Object Ruby Object Ruby Object
  8. New Object allocation Free List A L L O C

    A T E D F R E E
  9. New Object allocation Free List A L L O C

    A T E D
  10. New Object allocation Free List is empty A L L

    O C A T E D
  11. New Object allocation Free List is empty – Call GC

    A L L O C A T E D
  12. GC Process • GC finds non-reachable objects and adds them

    to Free List • If Free List is still empty, another Heap allocated
  13. MRI GC • “Conservative”: any bit pattern could be a

    pointer (may produce false positive) • “Stop the world”: no other Ruby code can execute during GC • “Mark & Sweep”: mark all objects in use, than sweep away unmarked objects
  14. More Objects => Longer GC => Slow

  15. In our case – Out of Memory!

  16. How to Debug? • gem "pry-debugger" https://github.com/nixme/pry-debugger • gem "debugger-pry"

    https://github.com/pry/debugger-pry
  17. Tools • ObjectSpace.count_objects • GC debug - Enable heap dump

    support • gdb.rb (only Linux) Note: memprof works only with Ruby 1.8
  18. ObjectSpace.count_objects

  19. Enable heap dump support to Ruby Install custom patched version

    of ruby Usage:
  20. https://github.com/tmm1/gdb.rb Attached to existing process and examine the HEAP

  21. Result After

  22. Profiling Tools • Ruby Benchmark • ruby-prof • perftools.rb (Google

    perftools for Ruby)
  23. Benchmark • gem 'benchmark_suite' https://github.com/evanphx/benchmark_suite

  24. ruby-prof gem 'ruby-prof' https://github.com/rdp/ruby-prof

  25. ruby-prof Measurements • process time (RubyProf::PROCESS_TIME) • wall time (RubyProf::WALL_TIME)

    • cpu time (RubyProf::CPU_TIME) • object allocations (RubyProf::ALLOCATIONS) • memory usage (RubyProf::MEMORY) • garbage collections runs (RubyProf::GC_RUNS) • garbage collection time (RubyProf::GC_TIME)
  26. perftools.rb https://github.com/tmm1/perftools.rb gem 'rack-perftools_profiler', :require => 'rack/perftools_profiler'

  27. rack-perftools_profiler usage

  28. KCacheGrind

  29. Summary • More Objects => Longer GC => Slow •

    Examine your HEAP • Use Tools
  30. Q&A http://dpaluy.github.com @dpaluy dpaluy@gmail.com http://www.linkedin.com/in/davidpaluy