Class: Benelux::Timeline

Inherits:
Array
  • Object
show all
Defined in:
lib/benelux/timeline.rb

Overview

|——--------—-|

 |
0.02
Usage examples

Benelux.timeline.each do |mark|

p [mark.track, mark.name, mark.tags[:usecase], mark.tags[:call_id]]

end

Benelux.timeline.ranges(:do_request).each do |range|

puts "Client%s: %s: %s: %f" % [range.track, range.thread_id, range.name, range.duration]

end

regions = Benelux.timeline(track_id).regions(:execute)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Timeline

Returns a new instance of Timeline.



25
26
27
28
29
30
31
# File 'lib/benelux/timeline.rb', line 25

def initialize(*args)
  @caller = Kernel.caller
  @ranges, @default_tags = [], Benelux::Tags.new
  @stats = Benelux::Stats.new
  add_default_tag :thread_id => Thread.current.object_id.abs
  super
end

Instance Attribute Details

#callerObject (readonly)

Returns the value of attribute caller.



24
25
26
# File 'lib/benelux/timeline.rb', line 24

def caller
  @caller
end

#default_tagsObject

Returns the value of attribute default_tags.



23
24
25
# File 'lib/benelux/timeline.rb', line 23

def default_tags
  @default_tags
end

#ranges(name = nil, tags = Benelux::Tags.new) ⇒ Object

obj.ranges(:do_request) =>

[[:do_request_a, :do_request_z], [:do_request_a, ...]]


90
91
92
# File 'lib/benelux/timeline.rb', line 90

def ranges
  @ranges
end

#statsObject

Returns the value of attribute stats.



22
23
24
# File 'lib/benelux/timeline.rb', line 22

def stats
  @stats
end

Instance Method Details

#+(other) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/benelux/timeline.rb', line 162

def +(other)
  self << other
  self.ranges += other.ranges
  self.stats += other.stats
  self.flatten!
  self
end

#[](tags = {}) ⇒ Object



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

def [](tags={})
  tags = Benelux::TagHelpers.normalize tags
  marks = self.select do |mark|
    mark.tags >= tags
  end
  tl = Benelux::Timeline.new marks
  tl.ranges = @ranges.select do |region|
    region.tags >= tags
  end
  stats = Benelux::Stats.new
  @stats.each do |stat|
    next unless stat.tags >= tags
    stats += stat
  end
  tl.stats = stats
  tl
end

#add_default_tags(tags = Benelux::Tags.new) ⇒ Object Also known as: add_default_tag



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

def add_default_tags(tags=Benelux::Tags.new)
  @default_tags.merge! tags
end

#add_mark(name) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/benelux/timeline.rb', line 127

def add_mark(name)
  mark = Benelux::Mark.now(name)
  mark.add_tags Benelux.thread_timeline.default_tags
  mark.add_tags self.default_tags
  Benelux.thread_timeline << mark
  self << mark
  mark
end

#add_range(name, from, to) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/benelux/timeline.rb', line 136

def add_range(name, from, to)
  range = Benelux::Range.new(name, from, to)
  range.add_tags Benelux.thread_timeline.default_tags
  range.add_tags self.default_tags
  @stats.add_keeper(name)
  @stats.send(name).sample(range.duration, range.tags)
  @ranges << range
  Benelux.thread_timeline.ranges << range
  Benelux.thread_timeline.stats.add_keeper(name)
  Benelux.thread_timeline.stats.send(name).sample(range.duration, range.tags)
  range
end

#clearObject



122
123
124
125
# File 'lib/benelux/timeline.rb', line 122

def clear
  @ranges.clear
  super
end

#durationObject



44
45
46
# File 'lib/benelux/timeline.rb', line 44

def duration
  self.last - self.first
end

#each(*args, &blk) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/benelux/timeline.rb', line 48

def each(*args, &blk)
  if args.empty? 
    super(&blk) 
  else 
    self.marks(*args).each(&blk)
  end
end

#marks(*names) ⇒ Object

obj.marks(:execute_a, :execute_z, :do_request_a) =>

[:execute_a, :do_request_a, :do_request_a, :execute_z]


60
61
62
63
64
65
66
# File 'lib/benelux/timeline.rb', line 60

def marks(*names)
  return self if names.empty?
  names = names.flatten.collect { |n| n.to_s }
  self.select do |mark| 
    names.member? mark.name.to_s
  end
end

#regions(name = nil, tags = Benelux::Tags.new) ⇒ Object

obj.ranges(:do_request) =>

[[:do_request_a, :get_body, :do_request_z], [:do_request_a, ...]]


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/benelux/timeline.rb', line 103

def regions(name=nil, tags=Benelux::Tags.new)
  return self if name.nil?
  self.ranges(name, tags).collect do |base_range|
    marks = self.sort.select do |mark|
      mark >= base_range.from && 
      mark <= base_range.to &&
      mark.tags >= base_range.tags
    end
    ranges = self.ranges.select do |range|
      range.from >= base_range.from &&
      range.to <= base_range.to &&
      range.tags >= base_range.tags
    end
    tl = Benelux::Timeline.new(marks)
    tl.ranges = ranges.sort
    tl
  end
end

#remove_default_tags(*tags) ⇒ Object



36
37
38
# File 'lib/benelux/timeline.rb', line 36

def remove_default_tags(*tags)
  @default_tags.delete_if { |n,v| tags.member?(n) }
end

#to_lineObject



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

def to_line
  marks = self.sort
end

#to_line2Object



153
154
155
156
157
158
159
160
161
# File 'lib/benelux/timeline.rb', line 153

def to_line2
  marks = self.sort
  str, prev = [], marks.first
  marks.each do |mark|
    str << "%s(%s):%.4f" % [mark.name, mark.thread_id, mark.to_s(prev)]
    prev = mark
  end
  str.join('; ')
end

#trackObject



40
41
42
# File 'lib/benelux/timeline.rb', line 40

def track 
  @default_tags[:track]
end