50 lines
1.3 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
2019-04-17 18:25:08 +09:00
require "cli/parser"
module Homebrew
2020-10-20 12:03:48 +02:00
extend T::Sig
module_function
2020-10-20 12:03:48 +02:00
sig { returns(CLI::Parser) }
def prof_args
Homebrew::CLI::Parser.new do
usage_banner <<~EOS
2020-08-03 02:07:06 +01:00
`prof` [<command>]
Run Homebrew with a Ruby profiler, e.g. `brew prof readall`.
EOS
switch "--stackprof",
description: "Use `stackprof` instead of `ruby-prof` (the default)."
2021-01-10 14:26:40 -05:00
named_args :command
end
end
def prof
2020-08-03 02:07:06 +01:00
args = prof_args.parse
brew_rb = (HOMEBREW_LIBRARY_PATH/"brew.rb").resolved_path
FileUtils.mkdir_p "prof"
cmd = args.named.first
raise UsageError, "#{cmd} is a Bash command!" if Commands.path(cmd).extname == ".sh"
if args.stackprof?
Homebrew.install_gem_setup_path! "stackprof"
with_env HOMEBREW_STACKPROF: "1" do
safe_system ENV["HOMEBREW_RUBY_PATH"], brew_rb, *args.named
end
output_filename = "prof/d3-flamegraph.html"
safe_system "stackprof --d3-flamegraph prof/stackprof.dump > #{output_filename}"
else
Homebrew.install_gem_setup_path! "ruby-prof"
output_filename = "prof/call_stack.html"
safe_system "ruby-prof", "--printer=call_stack", "--file=#{output_filename}", brew_rb, "--", *args.named
end
exec_browser output_filename
end
end