class Hotch

Constants

VERSION

Attributes

filter[R]
options[R]
viewer[R]

Public Class Methods

memory(name: $PROGRAM_NAME, aggregate: true, &block) click to toggle source
# File lib/hotch/memory.rb, line 7
def self.memory(name: $PROGRAM_NAME, aggregate: true, &block)
  memory = if aggregate
             $hotch_memory ||= Memory.new(name)
           else
             caller = Kernel.caller_locations(1).first
             name = "#{name}:#{caller.path}:#{caller.lineno}"
             Memory.new(name)
           end

  memory.report_at_exit

  if block
    memory.run(&block)
  else
    memory.start
  end
end
new(name, viewer: nil, mode: :wall, filter: nil, options: {}) click to toggle source
# File lib/hotch.rb, line 11
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

Public Instance Methods

report() { |report, svg| ... } click to toggle source
# File lib/hotch.rb, line 46
def report
  report = @reports.inject(:+) or return

  dir = Dir.mktmpdir("hotch.#{name}.")

  report_dump(report, dir, "profile.dump")
  dot = report_dot(report, dir, "profile.dot")
  svg = convert_svg(dir, dot, "profile.svg")

  @reports.clear

  return report, svg unless block_given?

  yield report, svg
end
report_at_exit() click to toggle source
# File lib/hotch.rb, line 62
def report_at_exit
  return if defined? @at_exit_installed

  at_exit do
    stop

    report do |_, svg|
      if viewer
        puts "Profile SVG: #{svg}"
        Kernel.system viewer, svg
      else
        puts "Profile SVG: view #{svg} # no HOTCH_VIEWER set"
      end
    end
  end

  @at_exit_installed = true
end
results() click to toggle source
# File lib/hotch.rb, line 42
def results
  StackProf.results
end
run(...) { || ... } click to toggle source
# File lib/hotch.rb, line 35
def run(...)
  start(...)
  yield
ensure
  stop
end
start(**options) click to toggle source
# File lib/hotch.rb, line 21
def start(**options)
  return if StackProf.running?

  stackprof = { mode: @mode }.merge(options)
  StackProf.start(**stackprof)
end
stop() click to toggle source
# File lib/hotch.rb, line 28
def stop
  return unless StackProf.running?

  StackProf.stop
  @reports << StackProf::Report.new(results)
end

Private Instance Methods

convert_svg(dir, dot, file) click to toggle source
# File lib/hotch.rb, line 99
def convert_svg(dir, dot, file)
  svg = File.join(dir, file)
  system("dot", "-Tsvg", "-o", svg, dot) or raise "dot: command not found. Please install graphviz"
  svg
end
name() click to toggle source
# File lib/hotch.rb, line 83
def name
  @name.gsub(/\W+/, "_")
end
report_dot(report, dir, file) click to toggle source
# File lib/hotch.rb, line 93
def report_dot(report, dir, file)
  write_file(dir, file) do |fh|
    report.print_graphviz(options, fh)
  end
end
report_dump(report, dir, file) click to toggle source
# File lib/hotch.rb, line 87
def report_dump(report, dir, file)
  write_file(dir, file) do |fh|
    report.print_dump(fh)
  end
end
write_file(dir, file, &block) click to toggle source
# File lib/hotch.rb, line 105
def write_file(dir, file, &block)
  path = File.join(dir, file)
  File.open(path, "wb", &block)
  path
end