class Hotch::Memory

Public Class Methods

new(name, ignore_paths: [], disable_gc: true) click to toggle source
# File lib/hotch/memory.rb, line 26
def initialize(name, ignore_paths: [], disable_gc: true)
  @name = name
  @ignore_paths = Array(ignore_paths || [])
  @reports = []
  @started = nil
  @disable_gc = disable_gc
end
report(name, **args, &block) click to toggle source
# File lib/hotch/memory.rb, line 34
def self.report(name, **args, &block)
  hotch = new(name, **args)
  hotch.run(&block)
  hotch.report
end

Public Instance Methods

report() { |report| ... } click to toggle source
# File lib/hotch/memory.rb, line 65
def report
  # TODO: make it persistent (as CSV)
  report = @reports.inject(:+)
  @reports.clear

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

  at_exit do
    stop

    report do |report|
      report.puts($stdout)
    end
  end

  @at_exit_installed = true
end
run() { || ... } click to toggle source
# File lib/hotch/memory.rb, line 58
def run
  start
  yield
ensure
  stop
end
start() click to toggle source
# File lib/hotch/memory.rb, line 40
def start
  return if @started

  GC.disable if @disable_gc
  ObjectSpace::AllocationTracer.setup %i[path line type]
  ObjectSpace::AllocationTracer.start
  @started = true
end
stop() click to toggle source
# File lib/hotch/memory.rb, line 49
def stop
  return unless @started

  results = ObjectSpace::AllocationTracer.stop
  @started = nil
  GC.enable if @disable_gc
  @reports << Report.new(results, @ignore_paths)
end

Private Instance Methods

name() click to toggle source
# File lib/hotch/memory.rb, line 93
def name
  @name.gsub(/\W+/, "_")
end