| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 18:25:08 +09:00
										 |  |  | require "cli/parser" | 
					
						
							| 
									
										
										
										
											2019-03-09 13:00:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-27 12:07:30 +00:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   extend T::Sig | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-27 12:07:30 +00:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   sig { returns(CLI::Parser) } | 
					
						
							| 
									
										
										
										
											2018-10-08 22:48:52 -04:00
										 |  |  |   def prof_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							| 
									
										
										
										
											2021-01-15 15:04:02 -05:00
										 |  |  |       description <<~EOS | 
					
						
							| 
									
										
										
										
											2021-01-24 01:59:02 -05:00
										 |  |  |         Run Homebrew with a Ruby profiler. For example, `brew prof readall`. | 
					
						
							| 
									
										
										
										
											2018-10-08 22:48:52 -04:00
										 |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2020-08-20 13:01:58 +01:00
										 |  |  |       switch "--stackprof", | 
					
						
							|  |  |  |              description: "Use `stackprof` instead of `ruby-prof` (the default)." | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-14 20:50:15 +11:00
										 |  |  |       named_args :command, min: 1
 | 
					
						
							| 
									
										
										
										
											2018-10-08 22:48:52 -04:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-27 12:07:30 +00:00
										 |  |  |   def prof | 
					
						
							| 
									
										
										
										
											2020-08-03 02:07:06 +01:00
										 |  |  |     args = prof_args.parse | 
					
						
							| 
									
										
										
										
											2019-03-27 11:49:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-27 12:07:30 +00:00
										 |  |  |     brew_rb = (HOMEBREW_LIBRARY_PATH/"brew.rb").resolved_path | 
					
						
							| 
									
										
										
										
											2020-08-20 13:01:58 +01:00
										 |  |  |     FileUtils.mkdir_p "prof" | 
					
						
							| 
									
										
										
										
											2021-01-11 21:01:45 +09:00
										 |  |  |     cmd = args.named.first | 
					
						
							| 
									
										
										
										
											2023-02-15 00:50:21 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     case Commands.path(cmd)&.extname | 
					
						
							|  |  |  |     when ".rb" | 
					
						
							|  |  |  |       # expected file extension so we do nothing | 
					
						
							|  |  |  |     when ".sh" | 
					
						
							|  |  |  |       raise UsageError, <<~EOS | 
					
						
							|  |  |  |         `#{cmd}` is a Bash command! | 
					
						
							|  |  |  |         Try `hyperfine` for benchmarking instead. | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       raise UsageError, "`#{cmd}` is an unknown command!" | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2020-08-20 13:01:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if args.stackprof? | 
					
						
							|  |  |  |       Homebrew.install_gem_setup_path! "stackprof" | 
					
						
							|  |  |  |       with_env HOMEBREW_STACKPROF: "1" do | 
					
						
							| 
									
										
										
										
											2022-11-05 03:06:10 +00:00
										 |  |  |         system(*HOMEBREW_RUBY_EXEC_ARGS, brew_rb, *args.named) | 
					
						
							| 
									
										
										
										
											2020-08-20 13:01:58 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |       output_filename = "prof/d3-flamegraph.html" | 
					
						
							|  |  |  |       safe_system "stackprof --d3-flamegraph prof/stackprof.dump > #{output_filename}" | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2022-12-29 12:48:13 -08:00
										 |  |  |       # NOTE: ruby-prof v1.4.3 is the last version that supports Ruby 2.6.x | 
					
						
							|  |  |  |       # TODO: Remove explicit version arg when we move to a newer version of Ruby | 
					
						
							|  |  |  |       Homebrew.install_gem_setup_path! "ruby-prof", version: "1.4.3" | 
					
						
							| 
									
										
										
										
											2020-08-20 13:01:58 +01:00
										 |  |  |       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 | 
					
						
							| 
									
										
										
										
											2023-02-15 00:50:21 -08:00
										 |  |  |   rescue OptionParser::InvalidOption => e | 
					
						
							|  |  |  |     ofail e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # The invalid option could have been meant for the subcommand. | 
					
						
							|  |  |  |     # Suggest `brew prof list -r` -> `brew prof -- list -r` | 
					
						
							|  |  |  |     args = ARGV - ["--"] | 
					
						
							|  |  |  |     puts "Try `brew prof -- #{args.join(" ")}` instead." | 
					
						
							| 
									
										
										
										
											2018-02-27 12:07:30 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | end |