Skip to content

Commit

Permalink
Add profiling hooks
Browse files Browse the repository at this point in the history
Summary: This adds the `--profiler` option for running ruby-prof.

Differential Revision: D56162222

fbshipit-source-id: ec7ccdc1641abfef63a3ff980ca6700bf2467ada
  • Loading branch information
dafyddcrosby authored and facebook-github-bot committed Apr 16, 2024
1 parent 81c1737 commit 3dfdb9f
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions cookbooks/fb_bookworm/files/default/bookworm/bookworm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ def initialize
# 'Enable verbose mode',
# )

# TODO(dcrosby) get ruby-prof working
# parser.on(
# '--profiler',
# '(WIP) Enable profiler for performance debugging',
# )
parser.on(
'--profiler',
'Enable profiler for performance debugging (requires ruby-prof)',
)

parser.on(
'--irb-config-step',
Expand Down Expand Up @@ -94,11 +93,12 @@ def parse
end
parser = Bookworm::CLIParser.new
options = parser.parse
# TODO(dcrosby) get ruby-prof working
# if options[:profiler]
# require 'ruby-prof'
# RubyProf.start
# end

if options[:profiler]
require 'ruby-prof'
Bookworm::Profile = RubyProf::Profile.new
Bookworm::Profile.start
end

# We require the libraries *after* the profiler has a chance to start,
# also means faster `bookworm -h` response
Expand Down Expand Up @@ -299,9 +299,13 @@ def build_report
run.do_action
end

# TODO(dcrosby) get ruby-prof working
# if options[:profiler]
# result = RubyProf.stop
# printer = RubyProf::FlatPrinter.new(result)
# printer.print($stdout)
# end
if options[:profiler]
result = Bookworm::Profile.stop
printer = RubyProf::GraphPrinter.new(result)
path = "#{Dir.tmpdir}/bookworm_profile-#{DateTime.now.iso8601(4)}.out"
printer = ::RubyProf::GraphPrinter.new(result)
File.open(path, 'w+') do |file|
printer.print(file)
end
puts "Wrote profiler output to #{path}"
end

0 comments on commit 3dfdb9f

Please sign in to comment.