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

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.



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

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.



5
6
7
# File 'lib/hotch.rb', line 5

def filter
  @filter
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/hotch.rb', line 5

def options
  @options
end

#viewerObject (readonly)

Returns the value of attribute viewer.



5
6
7
# File 'lib/hotch.rb', line 5

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

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

Yields:

  • (svg)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hotch.rb', line 38

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")

  yield svg
end

#report_at_exitObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hotch.rb', line 50

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



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

def results
  StackProf.results
end

#run(*args) ⇒ Object



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

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

#start(*args) ⇒ Object



16
17
18
# File 'lib/hotch.rb', line 16

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

#stopObject



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

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