Class: Hotch

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

Defined Under Namespace

Modules: Minitest Classes: Memory

Constant Summary collapse

VERSION =
"0.5.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, viewer: nil, filter: nil, options: {}) ⇒ Hotch

Returns a new instance of Hotch.



9
10
11
12
13
14
15
16
# File 'lib/hotch.rb', line 9

def initialize(name, viewer: nil, filter: nil, options: {})
  @name = name
  @viewer = viewer
  @options = options
  @reports = []

  @options[:filter] = Regexp.new(filter) if filter
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



7
8
9
# File 'lib/hotch.rb', line 7

def filter
  @filter
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/hotch.rb', line 7

def options
  @options
end

#viewerObject (readonly)

Returns the value of attribute viewer.



7
8
9
# File 'lib/hotch.rb', line 7

def viewer
  @viewer
end

Class Method Details

.memory(name: $0, aggregate: true, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/hotch/memory.rb', line 4

def self.memory(name: $0, 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

Instance Method Details

#reportObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hotch.rb', line 40

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

  if block_given?
    yield report, svg
  else
    return report, svg
  end
end

#report_at_exitObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hotch.rb', line 58

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

#resultsObject



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

def results
  StackProf.results
end

#run(*args) ⇒ Object



29
30
31
32
33
34
# File 'lib/hotch.rb', line 29

def run(*args)
  start(*args)
  yield
ensure
  stop
end

#start(*args) ⇒ Object



18
19
20
# File 'lib/hotch.rb', line 18

def start(*args)
  StackProf.start(*args) unless StackProf.running?
end

#stopObject



22
23
24
25
26
27
# File 'lib/hotch.rb', line 22

def stop
  if StackProf.running?
    StackProf.stop
    @reports << StackProf::Report.new(results)
  end
end