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.11'

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
35
# 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

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

Instance Attribute Details

#bindingsObject

Returns the value of attribute bindings.



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

def bindings
  @bindings
end

#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



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

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

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



126
127
128
129
130
# File 'lib/gestalt.rb', line 126

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

Instance Method Details

#display_callsObject



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

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



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

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



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
112
113
114
115
116
117
118
# File 'lib/gestalt.rb', line 74

def run(&block)
  Kernel.set_trace_func(
    lambda do |event, file, line, id, binding, classname|
      @bindings << binding
      case event
      when *@traceable_calls
        call = Gestalt::Call.new(
          :action     => "#{classname}##{id}",
          :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
  )
  begin
    value = yield
  rescue StandardError, Interrupt => value
    # noop
  end
  Kernel.set_trace_func(nil)
  @bindings.pop # pop Kernel#set_trace_func(nil)
  @bindings.pop # pop Gestalt.run
  @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
  value
end