Class: Gestalt

Inherits:
Object
  • Object
show all
Defined in:
lib/gestalt.rb,
lib/gestalt/call.rb

Defined Under Namespace

Classes: Call

Constant Summary collapse

VERSION =
'0.0.8'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Gestalt

Returns a new instance of Gestalt.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gestalt.rb', line 12

def initialize(options = {})
  options = {
    'call'      => true,
    'c-call'    => false,
    'formatador' => Formatador.new
  }.merge!(options)

  @traceable_calls = []
  @traceable_returns = []
  if options['call']
    @traceable_calls << 'call'
    @traceable_returns << 'return'
  end
  if options ['c-call']
    @traceable_calls << 'c-call'
    @traceable_returns << 'c-return'
  end

  @calls = []
  @formatador = options['formatador']
  @stack = []
  @totals = {}
end

Instance Attribute Details

#callsObject

Returns the value of attribute calls.



10
11
12
# File 'lib/gestalt.rb', line 10

def calls
  @calls
end

Class Method Details

.profile(options = {}, &block) ⇒ Object



113
114
115
116
117
# File 'lib/gestalt.rb', line 113

def self.profile(options = {}, &block)
  gestalt = new(options)
  gestalt.run(&block)
  gestalt.display_profile
end

.trace(options = {}, &block) ⇒ Object



119
120
121
122
123
# File 'lib/gestalt.rb', line 119

def self.trace(options = {}, &block)
  gestalt = new(options)
  gestalt.run(&block)
  gestalt.display_calls
end

Instance Method Details

#display_callsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gestalt.rb', line 36

def display_calls
  @formatador.display_line
  condensed = []
  total = 0.0
  for call in @calls
    if condensed.last && condensed.last == call
      condensed.last.durations.concat(call.durations)
    else
      condensed << call
    end
    total += call.duration
  end
  for call in condensed
    call.display(total, @formatador)
  end
  @formatador.display_line
end

#display_profileObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gestalt.rb', line 54

def display_profile
  for call in calls
    parse_call(call)
  end

  table = []
  for key, value in @totals
    table << {
      :action => "#{value[:occurances]}x #{key}",
      :duration => format("%.6f", value[:duration])
    }
  end
  table = table.sort {|x,y| y[:duration] <=> x[:duration]}

  @formatador.display_line
  @formatador.display_table(table)
  @formatador.display_line
end

#run(&block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gestalt.rb', line 73

def run(&block)
  Kernel.set_trace_func(
    lambda do |event, file, line, id, binding, classname|
      case event
      when *@traceable_calls
        call = Gestalt::Call.new(
          :action     => "#{classname}##{id}",
          :binding    => binding,
          :location   => "#{file}:#{line}"
        )
        unless @stack.empty?
          @stack.last.children.push(call)
        end
        @stack.push(call)
      when *@traceable_returns
        unless @stack.empty? # we get one of these when we set the trace_func
          call = @stack.pop
          call.finish
          if @stack.empty?
            @calls << call
          end
        end
      end
    end
  )
  yield
  rescue StandardError, Interrupt
  Kernel.set_trace_func(nil)
  @stack.pop # pop Kernel#set_trace_func(nil)
  unless @stack.empty?
    @stack.last.children.pop # pop Kernel#set_trace_func(nil)
  end
  while call = @stack.pop # leftovers, not sure why...
    call.finish
    if @stack.empty?
      @calls << call
    end
  end
end