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.7.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Hotch.



11
12
13
14
15
16
17
18
19
# 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

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



9
10
11
# File 'lib/hotch.rb', line 9

def filter
  @filter
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/hotch.rb', line 9

def options
  @options
end

#viewerObject (readonly)

Returns the value of attribute viewer.



9
10
11
# File 'lib/hotch.rb', line 9

def viewer
  @viewer
end

Class Method Details

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Method Details

#report {|report, svg| ... } ⇒ Object

Yields:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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_exitObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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

#resultsObject



42
43
44
# File 'lib/hotch.rb', line 42

def results
  StackProf.results
end

#runObject



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

def run(...)
  start(...)
  yield
ensure
  stop
end

#start(**options) ⇒ Object



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

def start(**options)
  return if StackProf.running?

  stackprof = { mode: @mode }.merge(options)
  StackProf.start(**stackprof)
end

#stopObject



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

def stop
  return unless StackProf.running?

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