Class: TT::Profiler

Inherits:
Object
  • Object
show all
Extended by:
BooleanAttributes
Defined in:
TT_Lib2/profiler.rb

Overview

Examples:

Profile Definition


TT::Profiler.new( 'Editor Activation' ) {
  self.report_each_event = true
  self.report_event_start = true
  track( TT_Vertex, :edit_vertex )
  track( TT_Vertex::Editor, :activate )
  track( TT_Vertex::Editor, :build_ui )
  track( TT_Vertex::Selection )
  start()
}

Since:

  • 2.7.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BooleanAttributes

battr, battr_accessor, battr_reader

Constructor Details

#initialize(title = '', &block) ⇒ Profiler

Returns a new instance of Profiler

Since:

  • 2.7.0



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

def initialize( title = '', &block )
  @title = title
  
  @triggers = {}
  @tracker = {}
  @data = {}

  @active = false

  @report_each_event = true
  @report_event_start = true
  
  @debug = false
  
  instance_eval( &block ) if block_given?
end

Instance Attribute Details

#dataObject (readonly)

Since:

  • 2.7.0



31
32
33
# File 'TT_Lib2/profiler.rb', line 31

def data
  @data
end

#triggersObject (readonly)

Since:

  • 2.7.0



32
33
34
# File 'TT_Lib2/profiler.rb', line 32

def triggers
  @triggers
end

Instance Method Details

#all_methods(klass) ⇒ Array

Parameters:

  • klass (Class, Module)

Returns:

  • (Array)

Since:

  • 2.7.0



84
85
86
87
88
89
90
91
92
93
94
# File 'TT_Lib2/profiler.rb', line 84

def all_methods( klass )
  stack = []
  stack.concat( klass.methods(false) )
  stack.concat( klass.instance_methods(false) )
  stack.concat( klass.protected_methods(false) )
  stack.concat( klass.protected_instance_methods(false) )
  stack.concat( klass.private_methods(false) )
  stack.concat( klass.private_instance_methods(false) )
  stack.map! { |method_name| method_name.to_sym }
  stack
end

#attach_trigger(klass, method_id, &block) ⇒ Proc

Returns:

  • (Proc)

Since:

  • 2.7.0



198
199
200
201
202
203
# File 'TT_Lib2/profiler.rb', line 198

def attach_trigger( klass, method_id, &block )
  @triggers[ klass ] ||= {}
  @triggers[ klass ][ method_id ] ||= []
  @triggers[ klass ][ method_id ] << block
  block
end

#detach_trigger(klass, method_id, proc) ⇒ Mixed

Returns:

  • (Mixed)

Since:

  • 2.7.0



207
208
209
210
211
# File 'TT_Lib2/profiler.rb', line 207

def detach_trigger( klass, method_id, proc )
  return false unless @triggers[ klass ]
  return false unless @triggers[ klass ][ method_id ]
  @triggers[ klass ][ method_id ].delete( proc )
end

#inspectString

Returns:

  • (String)

Since:

  • 2.7.0



56
57
58
59
60
# File 'TT_Lib2/profiler.rb', line 56

def inspect
  hex_id = TT.object_id_hex( self )
  post_fix = ( @title.empty? ) ? '' : %{ - "#{@title}"}
  return "#<#{self.class.name}:#{hex_id}#{post_fix}>"
end

Returns:

  • (Nil)

Since:

  • 2.7.0



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'TT_Lib2/profiler.rb', line 215

def print_report( output = $stdout )
  timestamp = Time.now.strftime('%d %B %Y - %H:%M')
  report = ''
  report << "\n\n"
  report << "==============================================================\n"
  report << " Profile: #{@title} (#{timestamp})\n"
  report << "--------+----------+------------+----------+------------------\n"
  report << "   %%   |          | cumulative | average  | Method\n"
  report << "  time  |  calls   |  seconds   | seconds  | Name\n"
  report << "--------+----------+------------+----------+------------------\n"
  # Sum up grand totals.
  total_real_time = 0.0
  total_calls = 0
  for klass, methods in @data
    for method_id, log_data in methods
      total_calls += log_data[ :calls ]
      total_real_time += log_data[ :real_time ]
    end
  end
  total_average = ( total_calls == 0 ) ? 0.0 : total_real_time / total_calls 
  # Process each call.
  for klass, methods in @data
    for method_id, log_data in methods
      method_name = "#{klass.name}.#{method_id}"
      calls = log_data[ :calls ]
      total = log_data[ :real_time ]
      percent = ( total / total_real_time ) * 100
      average = ( calls == 0 ) ? 0.0 : total / calls
      report << sprintf(
                  " %6.2f | %8d |  %8.4f  | %8.4f | %s\n",
                  percent, calls, total, average, method_name
                )
    end
  end
  report << "--------+----------+------------+----------+------------------\n"
  report << sprintf(
              " %6.2f | %8d |  %8.4f  | %8.4f | %s\n",
              100.0, total_calls, total_real_time, total_average, 'Total'
            )
  report << "========+==========+============+==========+==================\n"
  report << "\n\n"
  output.write( report )
  nil
end

#resetNil

Returns:

  • (Nil)

Since:

  • 2.7.0



184
185
186
187
188
189
190
191
192
193
194
# File 'TT_Lib2/profiler.rb', line 184

def reset
  for klass, methods in @data
    for method_id, log_data in methods
      @data[ klass ][ method_id ] = {
        :calls => 0,
        :real_time => 0.0
      }
    end
  end
  nil
end

#startNil

Returns:

  • (Nil)

Since:

  • 2.7.0



160
161
162
163
164
165
166
167
168
# File 'TT_Lib2/profiler.rb', line 160

def start
  @active = true
  for klass, methods in @tracker
    for method_id in methods
      profile_method( klass, method_id )
    end
  end
  nil
end

#stopNil

Returns:

  • (Nil)

Since:

  • 2.7.0



172
173
174
175
176
177
178
179
180
# File 'TT_Lib2/profiler.rb', line 172

def stop
  for klass, methods in @tracker
    for method_id in methods
      untrack_method( klass, method_id )
    end
  end
  @active = false
  nil
end

#track(klass, method_id = nil) ⇒ Nil

Parameters:

  • klass (Class, Module)
  • method_id (Symbol) (defaults to: nil)

Returns:

  • (Nil)

Since:

  • 2.7.0



69
70
71
72
73
74
75
76
77
78
# File 'TT_Lib2/profiler.rb', line 69

def track( klass, method_id = nil )
  if method_id
    track_method( klass, method_id )
  else
    for method_id in all_methods( klass )
      track_method( klass, method_id )
    end
  end
  nil
end

#track_method(klass, method_id) ⇒ Nil

Parameters:

  • klass (Class, Module)
  • method_id (Symbol)

Returns:

  • (Nil)

Since:

  • 2.7.0



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'TT_Lib2/profiler.rb', line 101

def track_method( klass, method_id )
  @triggers[ klass ] ||= {}
  @triggers[ klass ][ method_id ] = []
  @tracker[ klass ] ||= []
  @tracker[ klass ] << method_id
  @data[ klass ] ||= {}
  @data[ klass ][ method_id ] = {
    :calls => 0,
    :real_time => 0.0
  }
  if active?
    profile_method( klass, method_id )
  end
  nil
end

#untrack(klass, method_id = nil) ⇒ Nil

Parameters:

  • klass (Class, Module)
  • method_id (Symbol) (defaults to: nil)

Returns:

  • (Nil)

Since:

  • 2.7.0



122
123
124
125
126
127
128
129
130
131
# File 'TT_Lib2/profiler.rb', line 122

def untrack( klass, method_id = nil )
  if method_id
    self.untrack_method( klass, method_id )
  else
    for method_id in all_methods( klass )
      self.untrack_method( klass, method_id )
    end
  end
  nil
end

#untrack_method(klass, method_id) ⇒ Nil

Parameters:

  • klass (Class, Module)
  • method_id (Symbol)

Returns:

  • (Nil)

Since:

  • 2.7.0



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'TT_Lib2/profiler.rb', line 138

def untrack_method( klass, method_id )
  original = get_profile_method_id( method_id )
  remove_wrapper = proc {
    if method_defined?( method_id ) && method_defined?( original )
      remove_method( method_id )
      alias_method( method_id, original )
      remove_method( original )
    end
  }
  # Instance Methods
  klass.class_eval( &remove_wrapper )
  # Class Methods
  metaklass = ( class << klass; self; end )
  metaklass.class_eval( &remove_wrapper )
  # Clean up references
  @tracker[ klass ].delete( method_id )
  @tracker.delete( klass ) if @tracker[ klass ].empty?
  nil
end