lib/hotch.rb in hotch-0.6.0 vs lib/hotch.rb in hotch-0.7.0
- old
+ new
@@ -1,35 +1,41 @@
-require 'stackprof'
-require 'tmpdir'
+# frozen_string_literal: true
-require 'hotch/version'
+require "stackprof"
+require "tmpdir"
+require "hotch/version"
+
class Hotch
- attr_reader :name, :viewer, :filter, :options
+ attr_reader :viewer, :filter, :options
- def initialize(name, viewer: nil, filter: nil, options: {})
+ def initialize(name, viewer: nil, mode: :wall, filter: nil, options: {})
@name = name
@viewer = viewer
@options = options
@reports = []
+ @mode = mode
@options[:filter] = Regexp.new(filter) if filter
end
- def start(*args)
- StackProf.start(*args) unless StackProf.running?
+ def start(**options)
+ return if StackProf.running?
+
+ stackprof = { mode: @mode }.merge(options)
+ StackProf.start(**stackprof)
end
def stop
- if StackProf.running?
- StackProf.stop
- @reports << StackProf::Report.new(results)
- end
+ return unless StackProf.running?
+
+ StackProf.stop
+ @reports << StackProf::Report.new(results)
end
- def run(*args)
- start(*args)
+ def run(...)
+ start(...)
yield
ensure
stop
end
@@ -46,15 +52,13 @@
dot = report_dot(report, dir, "profile.dot")
svg = convert_svg(dir, dot, "profile.svg")
@reports.clear
- if block_given?
- yield report, svg
- else
- return report, svg
- end
+ return report, svg unless block_given?
+
+ yield report, svg
end
def report_at_exit
return if defined? @at_exit_installed
@@ -75,11 +79,11 @@
end
private
def name
- @name.gsub(/\W+/, '_')
+ @name.gsub(/\W+/, "_")
end
def report_dump(report, dir, file)
write_file(dir, file) do |fh|
report.print_dump(fh)
@@ -96,28 +100,27 @@
svg = File.join(dir, file)
system("dot", "-Tsvg", "-o", svg, dot) or raise "dot: command not found. Please install graphviz"
svg
end
- def write_file(dir, file)
+ def write_file(dir, file, &block)
path = File.join(dir, file)
- File.open(path, 'wb') do |fh|
- yield fh
- end
+ File.open(path, "wb", &block)
path
end
end
-def Hotch(name: $0, aggregate: true, viewer: ENV['HOTCH_VIEWER'], filter: ENV['HOTCH_FILTER'], options: {})
+# rubocop:disable Naming/MethodName
+def Hotch(name: $PROGRAM_NAME, aggregate: true, viewer: ENV.fetch("HOTCH_VIEWER", nil), mode: :wall,
+ filter: ENV.fetch("HOTCH_FILTER", nil), options: {}, &block)
hotch = if aggregate
- $hotch ||= Hotch.new(name, viewer: viewer, filter: filter, options: options)
- else
- caller = Kernel.caller_locations(1).first
- name = "#{name}:#{caller.path}:#{caller.lineno}"
- Hotch.new(name, viewer: viewer, filter: filter, options: options)
- end
+ $hotch ||= Hotch.new(name, viewer: viewer, mode: mode, filter: filter, options: options)
+ else
+ caller = Kernel.caller_locations(1).first
+ name = "#{name}:#{caller.path}:#{caller.lineno}"
+ Hotch.new(name, viewer: viewer, filter: filter, options: options)
+ end
hotch.report_at_exit
- hotch.run do
- yield
- end
+ hotch.run(&block)
end
+# rubocop:enable Naming/MethodName