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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Memory.



26
27
28
29
30
31
32
# 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

Class Method Details

.report(name, **args, &block) ⇒ Object



34
35
36
37
38
# File 'lib/hotch/memory.rb', line 34

def self.report(name, **args, &block)
  hotch = new(name, **args)
  hotch.run(&block)
  hotch.report
end

Instance Method Details

#reportObject



65
66
67
68
69
70
71
72
73
74
75
# 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_exitObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# 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

#runObject



58
59
60
61
62
63
# File 'lib/hotch/memory.rb', line 58

def run
  start
  yield
ensure
  stop
end

#startObject



40
41
42
43
44
45
46
47
# 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

#stopObject



49
50
51
52
53
54
55
56
# 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