Ruby ruby-prof: Difference between revisions

From wikinotes
No edit summary
 
 
(One intermediate revision by the same user not shown)
Line 9: Line 9:
|}
|}
</blockquote><!-- Documentation -->
</blockquote><!-- Documentation -->
= Example =
<blockquote>
<syntaxhighlight lang="yaml">
1: Add 'ruby-prof' to Gemfile
2: bundle install
</syntaxhighlight>
<syntaxhighlight lang="ruby">
# 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)
</syntaxhighlight>
<syntaxhighlight lang="yaml">
4: cat foo.rb | rails c
5: a colour-coded flamegraph!
</syntaxhighlight>
</blockquote><!-- Example -->

Latest revision as of 16:06, 19 July 2023

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!