Ruby ruby-prof: Difference between revisions
From wikinotes
No edit summary |
|||
Line 15: | Line 15: | ||
1: Add 'ruby-prof' to Gemfile | 1: Add 'ruby-prof' to Gemfile | ||
2: bundle install | 2: bundle install | ||
3: write file: | </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 | end | ||
puts(File.read("out.prof")) | |||
puts("---------------------------") | |||
puts("RESULTS WRITTEN TO out.prof") | |||
puts("---------------------------") | |||
end | |||
def callstack_printer(result) | |||
printer = RubyProf::CallStackPrinter.new(result) | printer = RubyProf::CallStackPrinter.new(result) | ||
Tempfile.create(["foo", ".html"]) do |fo| | Tempfile.create(["foo", ".html"]) do |fo| | ||
Line 26: | Line 37: | ||
%x{open -a "Google Chrome.app" -W "#{fo.path}"} | %x{open -a "Google Chrome.app" -W "#{fo.path}"} | ||
end | 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 | 4: cat foo.rb | rails c | ||
5: a colour-coded flamegraph! | 5: a colour-coded flamegraph! | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Example --> | </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!