Ruby ruby-prof

From wikinotes

A cross platform profiler for ruby.
dump to a file, then obtain reports like flamegraphs, call-trees, etc.

Documentation

official docs https://ruby-prof.github.io/

Example

1: Add 'ruby-prof' to Gemfile
2: bundle install
# 3: write file: foo.rb

def flat_printer(result)
  printer = RubyProf::FlatPrinter.new(result)
  File.open("out.prof", "w") do |fd|
    printer.print(fd, {})
  end
  puts(File.read("out.prof"))
  puts("---------------------------")
  puts("RESULTS WRITTEN TO out.prof")
  puts("---------------------------")
end

def callstack_printer(result)
  printer = RubyProf::CallStackPrinter.new(result)
  Tempfile.create(["foo", ".html"]) do |fo|
    printer.print(fo)
    %x{open -a "Google Chrome.app" -W "#{fo.path}"}
  end
end


Rails.application.eager_load!
result = RubyProf.profile do
  # ... your code to profile ...
end

flat_printer(result)
callstack_printer(result)
4: cat foo.rb | rails c
5: a colour-coded flamegraph!