Class: EventTree

Inherits:
Object
  • Object
show all
Defined in:
lib/agent/event_tree.rb

Overview

Constructs a frame tree from a series of related ActiveSupport::events

Defined Under Namespace

Classes: Event

Constant Summary collapse

@@DEFAULT_EVENT =
"DEFAULT"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEventTree

Returns a new instance of EventTree.



38
39
40
41
# File 'lib/agent/event_tree.rb', line 38

def initialize
  @events = []
  @eventsEnd = []
end

Instance Attribute Details

#DEFAULT_EVENTObject

Returns the value of attribute DEFAULT_EVENT.



43
44
45
# File 'lib/agent/event_tree.rb', line 43

def DEFAULT_EVENT
  @DEFAULT_EVENT
end

Instance Method Details

#addEvent(name, label, startTime, endTime, payload = {}) ⇒ Object

Add an event to this tree



54
55
56
57
58
59
60
# File 'lib/agent/event_tree.rb', line 54

def addEvent(name, label, startTime, endTime, payload = {})
  e = Event.new(name, label, startTime, endTime, payload)
  @events << e
  @events = @events.sort_by { |a| [ a.startTime, a.invertEndTime ] }
  @eventsEnd << e
  @eventsEnd = @eventsEnd.sort_by { |a| [ a.endTime, a.duration ]}
end

#chooseBestDefaultLabel(eventA, eventB) ⇒ Object

Prefer event with the longest duration



46
47
48
49
50
51
# File 'lib/agent/event_tree.rb', line 46

def chooseBestDefaultLabel(eventA, eventB)
  if eventA.duration > eventB.duration
    return eventA.label
  end
  return eventB.label
end

#dumpFramesObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
# File 'lib/agent/event_tree.rb', line 62

def dumpFrames
  FrameBuilder.instance.reset
  if @events == nil
    raise "Trying to dump frame stack before any events collected"
  end

  # if needed, create a wrapper event if the events do not have
  # a spanning parent
  firstEvent = @events.first
  lastEvent = @eventsEnd.last
  if firstEvent != lastEvent
    firstStartTime = @events.first.startTime
    lastEndTime = @eventsEnd.last.endTime
    puts " NEW DEFAULT " + firstStartTime.to_f.to_s + "->" + lastEndTime.to_f.to_s
    label = chooseBestDefaultLabel(firstEvent, lastEvent)
    @events << Event.new(@@DEFAULT_EVENT, label, firstStartTime, lastEndTime, {})
    @events = @events.sort_by { |a| [ a.startTime, a.invertEndTime ] }
  end

  # run through the event list
  stack = []
  @events.each {|e|

    # Create the root
    if stack.size == 0
      op = Operation.new(e.payload, e.label, e.name)
      FrameBuilder.instance.enter(op, e.startTime)
      stack << e
      next
    end

    # If the last event on the stack cannot contain this one, crawl
    # up the stack until we find one that does
    while stack.size > 1 && !stack.last.contains(e)
      FrameBuilder.instance.exit(stack.last.endTime, false)
      stack.pop
    end

    # Add the event, enter the frame
    op = Operation.new(e.payload, e.label, e.name)
    FrameBuilder.instance.enter(op, e.startTime)
    stack << e
  }

  # crawl back up the stack
  while stack.size > 1
    FrameBuilder.instance.exit(stack.last.endTime, false)
    stack.pop
  end

  # final exit
  trace = FrameBuilder.instance.exit(stack.last.endTime, false)
  if trace != nil
    return trace
  end
  raise "Unable to create complete frame stack from events. Your algorithm is broken."
end