Class: Hotch::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/hotch/memory.rb,
lib/hotch/memory/minitest.rb

Defined Under Namespace

Modules: Minitest Classes: Report

Instance Method Summary collapse

Constructor Details

#initialize(name, ignore_paths: [], disable_gc: true) ⇒ Memory

Returns a new instance of Memory.



23
24
25
26
27
28
29
# File 'lib/hotch/memory.rb', line 23

def initialize(name, ignore_paths: [], disable_gc: true)
  @name = name
  @ignore_paths = Array(ignore_paths || [])
  @reports = []
  @started = nil
  @disable_gc = disable_gc
end

Instance Method Details

#reportObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hotch/memory.rb', line 54

def report
  # TODO make it persistent (as CSV)
  report = @reports.inject(:+)
  @reports.clear

  if block_given?
    yield report
  else
    report
  end
end

#report_at_exitObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hotch/memory.rb', line 66

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

#runObject



47
48
49
50
51
52
# File 'lib/hotch/memory.rb', line 47

def run
  start
  yield
ensure
  stop
end

#startObject



31
32
33
34
35
36
37
# File 'lib/hotch/memory.rb', line 31

def start
  return if @started
  GC.disable if @disable_gc
  ObjectSpace::AllocationTracer.setup [:path, :line, :type]
  ObjectSpace::AllocationTracer.start
  @started = true
end

#stopObject



39
40
41
42
43
44
45
# File 'lib/hotch/memory.rb', line 39

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