Module: Benelux

Defined in:
lib/benelux.rb,
lib/benelux/mark.rb,
lib/benelux/range.rb,
lib/benelux/stats.rb,
lib/benelux/track.rb,
lib/benelux/packer.rb,
lib/benelux/timeline.rb

Defined Under Namespace

Classes: AlreadyTimed, BadRecursion, BeneluxError, Mark, MethodCounter, MethodPacker, MethodTimer, NotSupported, Range, Stats, Timeline, Tms, Track, UnknownTrack

Constant Summary collapse

VERSION =
"0.6.1"
NOTSUPPORTED =
[Class, Object, Kernel]
@@mutex =
Mutex.new
@@debug =
false
@@logger =
STDERR

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.known_threadsObject (readonly)

Returns the value of attribute known_threads.



32
33
34
# File 'lib/benelux.rb', line 32

def known_threads
  @known_threads
end

.packed_methodsObject (readonly)

Returns the value of attribute packed_methods.



27
28
29
# File 'lib/benelux.rb', line 27

def packed_methods
  @packed_methods
end

.timelineObject (readonly)

Returns the value of attribute timeline.



29
30
31
# File 'lib/benelux.rb', line 29

def timeline
  @timeline
end

.timeline_chunkObject (readonly)

Returns the value of attribute timeline_chunk.



30
31
32
# File 'lib/benelux.rb', line 30

def timeline_chunk
  @timeline_chunk
end

.timeline_updatesObject (readonly)

Returns the value of attribute timeline_updates.



31
32
33
# File 'lib/benelux.rb', line 31

def timeline_updates
  @timeline_updates
end

.tracksObject (readonly)

Returns the value of attribute tracks.



28
29
30
# File 'lib/benelux.rb', line 28

def tracks
  @tracks
end

Class Method Details

.add_counter(klass, meth, aliaz = nil, &blk) ⇒ Object

Raises:



154
155
156
157
# File 'lib/benelux.rb', line 154

def Benelux.add_counter klass, meth, aliaz=nil, &blk
  raise NotSupported, klass unless Benelux.supported? klass
  Benelux::MethodCounter.new klass, meth, aliaz, &blk
end

.add_timer(klass, meth, aliaz = nil, &blk) ⇒ Object

Raises:



148
149
150
151
152
# File 'lib/benelux.rb', line 148

def Benelux.add_timer klass, meth, aliaz=nil, &blk
  raise NotSupported, klass unless Benelux.supported? klass
  raise AlreadyTimed, klass if Benelux.packed_method? klass, meth
  Benelux::MethodTimer.new klass, meth, aliaz, &blk
end

.bm(n = 1, reps = 5, &blk) ⇒ Object

Run a benchmark the healthy way: with a warmup run, with multiple repetitions, and standard deviation.

  • n Number of times to execute blk (one data sample)

  • reps Number of data samples to collect

  • blk a Ruby block to benchmark

Returns a Benelux::Tms object



226
227
228
229
230
231
232
233
234
# File 'lib/benelux.rb', line 226

def Benelux.bm(n=1, reps=5, &blk) 
  require 'benchmark'
  n.times &blk
  tms = Benelux::Tms.new
  reps.times do |rep|
    tms.sample Benchmark.measure() {n.times &blk}
  end
  tms
end

.current_track(name = nil, timeline = nil) ⇒ Object

If name is specified, this will associate the current thread with that Track name (the Track will be created if necessary).

If track is nil, it returns the Track object for the Track associated to the current thread.



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/benelux.rb', line 69

def Benelux.current_track(name=nil,timeline=nil)
  if name.nil?
    name = Thread.current.track_name
  else
    Thread.current.track_name = name
    @@mutex.synchronize do
      @tracks[name] ||= Track.new(name, timeline || Thread.current.timeline || Benelux::Timeline.new)
      @tracks[name].add_thread Thread.current
      @known_threads << Thread.current
    end
  end
  Benelux.track(name)
end

.debug?Boolean

Returns:

  • (Boolean)


166
# File 'lib/benelux.rb', line 166

def Benelux.debug?; @@debug; end

.disable_debugObject



165
# File 'lib/benelux.rb', line 165

def Benelux.disable_debug; @@debug = false; end

.enable_debugObject



164
# File 'lib/benelux.rb', line 164

def Benelux.enable_debug; @@debug = true; end

.inspectObject



124
125
126
127
128
# File 'lib/benelux.rb', line 124

def Benelux.inspect
  str = ["Benelux"]
  str << "tracks:" << Benelux.tracks.inspect
  str.join $/
end

.known_thread?(t = Thread.current) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/benelux.rb', line 135

def Benelux.known_thread?(t=Thread.current)
  Benelux.known_threads.member? t
end

.ld(*msg) ⇒ Object



159
160
161
# File 'lib/benelux.rb', line 159

def Benelux.ld(*msg)
  @@logger.puts "D:  " << msg.join("#{$/}D:  ") if debug?
end

.merge_tracksObject



84
85
86
87
88
89
90
# File 'lib/benelux.rb', line 84

def Benelux.merge_tracks
  tl = Benelux::Timeline.new
  tracks.each_pair do |trackid,track|
    tl.merge! track.timeline
  end
  tl
end

.packed_method(klass, meth) ⇒ Object



139
140
141
142
# File 'lib/benelux.rb', line 139

def Benelux.packed_method(klass, meth)
  return nil unless defined?(Benelux.packed_methods[klass][meth])
  Benelux.packed_methods[klass][meth]
end

.packed_method?(klass, meth) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/benelux.rb', line 144

def Benelux.packed_method? klass, meth
  !Benelux.packed_method(klass, meth).nil?
end

.resetObject



35
36
37
38
39
40
41
42
# File 'lib/benelux.rb', line 35

def Benelux.reset
  @tracks = SelectableHash.new
  @timeline = Timeline.new
  @timeline_chunk = Timeline.new  # See: update_global_timeline
  @timeline_updates = 0
  @known_threads = []
  @processed_dead_threads = []
end

.supported?(klass) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/benelux.rb', line 130

def Benelux.supported?(klass)
  !NOTSUPPORTED.member?(klass)
end

.thread_timelineObject



49
50
51
# File 'lib/benelux.rb', line 49

def Benelux.thread_timeline
  Thread.current.timeline
end

.track(name) ⇒ Object

Raises:



53
54
55
56
# File 'lib/benelux.rb', line 53

def Benelux.track(name)
  raise UnknownTrack unless track? name
  @tracks[name]
end

.track?(name) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/benelux.rb', line 58

def Benelux.track?(name)
  @tracks.has_key? name
end

.update_global_timelineObject

Only updates data from threads that are dead and rotated timelines.



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
119
120
121
122
# File 'lib/benelux.rb', line 94

def Benelux.update_global_timeline
  @@mutex.synchronize do
    dthreads = Benelux.known_threads.select { |t| 
      !t.timeline.nil? && (t.nil? || !t.status) &&
      !@processed_dead_threads.member?(t)
    }
    # Threads that have rotated timelines
    rthreads = Benelux.known_threads.select { |t|
      !t.rotated_timelines.empty?
    }
    dtimelines = dthreads.collect { |t| t.timeline }
    # Also get all rotated timelines. 
    rthreads.each { |t| 
      # We loop carefully through the rotated timelines
      # incase something was added while we're working. 
      while !t.rotated_timelines.empty?
        dtimelines.push t.rotated_timelines.shift 
      end
    }
    Benelux.ld [:update_global_timeline, dthreads.size, rthreads.size, dtimelines.size].inspect
    # Keep track of this update separately
    @timeline_chunk = Benelux::Timeline.new
    @timeline_chunk.merge! *dtimelines
    @processed_dead_threads.push *dthreads
    tl = Benelux.timeline.merge! Benelux.timeline_chunk
    @timeline_updates += 1
    tl
  end
end