Class: TT::Profiler
- Inherits:
-
Object
- Object
- TT::Profiler
- Extended by:
- BooleanAttributes
- Defined in:
- TT_Lib2/profiler.rb
Overview
Instance Attribute Summary collapse
- #data ⇒ Object readonly
- #triggers ⇒ Object readonly
Instance Method Summary collapse
- #all_methods(klass) ⇒ Array
- #attach_trigger(klass, method_id, &block) ⇒ Proc
- #detach_trigger(klass, method_id, proc) ⇒ Mixed
-
#initialize(title = '', &block) ⇒ Profiler
constructor
A new instance of Profiler.
- #inspect ⇒ String
- #print_report(output = $stdout) ⇒ Nil
- #reset ⇒ Nil
- #start ⇒ Nil
- #stop ⇒ Nil
- #track(klass, method_id = nil) ⇒ Nil
- #track_method(klass, method_id) ⇒ Nil
- #untrack(klass, method_id = nil) ⇒ Nil
- #untrack_method(klass, method_id) ⇒ Nil
Methods included from BooleanAttributes
battr, battr_accessor, battr_reader
Constructor Details
#initialize(title = '', &block) ⇒ Profiler
Returns a new instance of Profiler
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
#data ⇒ Object (readonly)
31 32 33 |
# File 'TT_Lib2/profiler.rb', line 31 def data @data end |
#triggers ⇒ Object (readonly)
32 33 34 |
# File 'TT_Lib2/profiler.rb', line 32 def triggers @triggers end |
Instance Method Details
#all_methods(klass) ⇒ Array
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
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
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 |
#inspect ⇒ String
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 |
#print_report(output = $stdout) ⇒ Nil
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 ) = Time.now.strftime('%d %B %Y - %H:%M') report = '' report << "\n\n" report << "==============================================================\n" report << " Profile: #{@title} (#{})\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 |
#reset ⇒ Nil
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 |
#start ⇒ Nil
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 |
#stop ⇒ Nil
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
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
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
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
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 = ( class << klass; self; end ) .class_eval( &remove_wrapper ) # Clean up references @tracker[ klass ].delete( method_id ) @tracker.delete( klass ) if @tracker[ klass ].empty? nil end |