Module: Benelux
- Defined in:
- lib/benelux.rb,
lib/benelux/mark.rb,
lib/benelux/tags.rb,
lib/benelux/range.rb,
lib/benelux/stats.rb,
lib/benelux/timeline.rb
Defined Under Namespace
Modules: TagHelpers
Classes: BeneluxError, Mark, NoTrack, NotSupported, Range, Stats, Tags, Timeline
Constant Summary
collapse
- VERSION =
"0.3.0"
- NOTSUPPORTED =
[Class, Object, Kernel]
- @@timed_methods =
{}
- @@known_threads =
[]
- @@timelines =
{}
- @@mutex =
Mutex.new
- @@debug =
true
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.add_tally(obj, meth) ⇒ Object
125
126
|
# File 'lib/benelux.rb', line 125
def Benelux.add_tally obj, meth
end
|
.add_thread_tag(*args) ⇒ Object
157
|
# File 'lib/benelux.rb', line 157
def Benelux.add_thread_tag(*args) add_thread_tags *args end
|
Thread tags become the default for any new Mark or Range.
154
155
156
|
# File 'lib/benelux.rb', line 154
def Benelux.add_thread_tags(args=Benelux::Tags.new)
Benelux.thread_timeline.add_default_tags args
end
|
.add_timer(klass, meth) ⇒ Object
116
117
118
119
120
121
122
123
|
# File 'lib/benelux.rb', line 116
def Benelux.add_timer klass, meth
raise NotSupported, klass unless Benelux.supported? klass
raise AlreadyTimed, klass if Benelux.timed_method? klass, meth
prepare_object klass
meth_alias = rename_method klass, meth
timed_methods[klass] << meth
klass.module_eval generate_timer_str(meth_alias, meth), __FILE__, 215
end
|
.current_track(track = nil) ⇒ Object
If track
is specified, this will associate the current thread with that track
.
If track
is nil, it returns the timeline for the track associated to the current thread.
.debug? ⇒ Boolean
28
|
# File 'lib/benelux.rb', line 28
def Benelux.debug?; @@debug; end
|
.disable_debug ⇒ Object
27
|
# File 'lib/benelux.rb', line 27
def Benelux.disable_debug; @@debug = false; end
|
.enable_debug ⇒ Object
26
|
# File 'lib/benelux.rb', line 26
def Benelux.enable_debug; @@debug = true; end
|
.generate_timer_str(meth_alias, meth) ⇒ Object
Creates a method definition (for an ‘eval` that) for a method named meth
which times a call to meth_alias
.
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
# File 'lib/benelux.rb', line 217
def Benelux.generate_timer_str(meth_alias, meth)
%Q{
def #{meth}(*args, &block)
call_id = "" << self.object_id.abs.to_s << args.object_id.abs.to_s
# We only need to do these things once.
if self.timeline.nil?
self.timeline = Benelux::Timeline.new
Benelux.store_thread_reference
end
mark_a = self.timeline.add_mark :'#{meth}_a'
mark_a.add_tag :call_id => call_id
tags = mark_a.tags
ret = #{meth_alias}(*args, &block)
rescue => ex
raise ex
ensure
mark_z = self.timeline.add_mark :'#{meth}_z'
mark_z.tags = tags # In case tags were added between these marks
range = self.timeline.add_range :'#{meth}', mark_a, mark_z
range.exception = ex if defined?(ex) && !ex.nil?
end
}
end
|
.included(klass) ⇒ Object
Make note of the class which included Benelux.
108
109
110
|
# File 'lib/benelux.rb', line 108
def Benelux.included(klass)
timed_methods[klass] = [] unless timed_methods.has_key? klass
end
|
.known_threads ⇒ Object
185
186
187
|
# File 'lib/benelux.rb', line 185
def Benelux.known_threads
@@known_threads
end
|
.merge_timelines(*timelines) ⇒ Object
Combine two or more timelines into a new, single Benelux::Timeline.
94
95
96
97
98
99
100
|
# File 'lib/benelux.rb', line 94
def Benelux.merge_timelines(*timelines)
tl, stats, ranges = Benelux::Timeline.new, Benelux::Stats.new, []
timelines.each do |t|
tl += t
end
tl
end
|
.name(*names) ⇒ Object
128
129
130
|
# File 'lib/benelux.rb', line 128
def Benelux.name(*names)
names.flatten.collect { |n| n.to_s }.join('_')
end
|
.prepare_object(obj) ⇒ Object
133
134
135
136
137
138
139
|
# File 'lib/benelux.rb', line 133
def Benelux.prepare_object obj
obj.extend Attic unless obj.kind_of?(Attic)
unless obj.kind_of?(Benelux)
obj.attic :timeline
obj.send :include, Benelux
end
end
|
.remove_thread_tag(*args) ⇒ Object
162
|
# File 'lib/benelux.rb', line 162
def Benelux.remove_thread_tag(*args) remove_thread_tags *args end
|
.rename_method(obj, meth) ⇒ Object
Rename the method meth
in the object obj
and return the new alias.
e.g.
Benelux.renamed(SomeClass, :execute)
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/benelux.rb', line 201
def Benelux.rename_method(obj, meth)
thread_id, call_id = Thread.current.object_id.abs, obj.object_id.abs
meth_alias = "__benelux_#{meth}_#{thread_id}_#{call_id}"
obj.module_eval do
alias_method meth_alias, meth
end
meth_alias
end
|
.store_thread_reference ⇒ Object
Benelux keeps track of the threads which have timed objects so it can process the timelines after all is said and done.
.supported?(klass) ⇒ Boolean
177
178
179
|
# File 'lib/benelux.rb', line 177
def Benelux.supported?(klass)
!NOTSUPPORTED.member?(klass)
end
|
.thread_timeline ⇒ Object
.timed_method?(klass, meth) ⇒ Boolean
112
113
114
|
# File 'lib/benelux.rb', line 112
def Benelux.timed_method? klass, meth
!timed_methods[klass].nil? && timed_methods[klass].member?(meth)
end
|
.timed_methods ⇒ Object
181
182
183
|
# File 'lib/benelux.rb', line 181
def Benelux.timed_methods
@@timed_methods
end
|
.timeline(track = nil) ⇒ Object
.timelines ⇒ Object
189
190
191
|
# File 'lib/benelux.rb', line 189
def Benelux.timelines
@@timelines
end
|
.update_all_track_timelines ⇒ Object
Must be run in single-threaded mode (after all track threads have finished).
55
56
57
|
# File 'lib/benelux.rb', line 55
def Benelux.update_all_track_timelines
Benelux.timelines.keys.each { |track| Benelux.update_track(track) }
end
|
.update_track_timeline(track = nil) ⇒ Object
Must be run from the master thread in the current track. The master thread is either the first thread in a track or the one which creates additional threads for the track.
Instance Method Details
#benelux_timers ⇒ Object
Returns an Array of method names for the current class that are timed by Benelux.
This is an instance method for objects which have Benelux modified methods.