| OLD | NEW |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 class TimelineEvent(object): | 5 class TimelineEvent(object): |
| 6 """Represents a timeline event.""" | 6 """Represents a timeline event.""" |
| 7 def __init__(self, name, start, duration, args=None, parent=None): | 7 def __init__(self, category, name, start, duration, args=None, parent=None): |
| 8 self.category = category |
| 8 self.name = name | 9 self.name = name |
| 9 self.start = start | 10 self.start = start |
| 10 self.duration = duration | 11 self.duration = duration |
| 11 self.children = [] | |
| 12 self.parent = parent | 12 self.parent = parent |
| 13 self.args = args | 13 self.args = args |
| 14 | 14 |
| 15 @property | 15 @property |
| 16 def end(self): | 16 def end(self): |
| 17 return self.start + self.duration | 17 return self.start + self.duration |
| 18 | 18 |
| 19 @property | |
| 20 def self_time(self): | |
| 21 """Time spent in this function less any time spent in child events.""" | |
| 22 child_total = sum( | |
| 23 [e.duration for e in self.children]) | |
| 24 return self.duration - child_total | |
| 25 | |
| 26 def __repr__(self): | 19 def __repr__(self): |
| 27 if self.args: | 20 if self.args: |
| 28 args_str = ', ' + repr(self.args) | 21 args_str = ', ' + repr(self.args) |
| 29 else: | 22 else: |
| 30 args_str = '' | 23 args_str = '' |
| 31 | 24 |
| 32 return "TimelineEvent(name='%s', start=%f, duration=%s%s)" % ( | 25 return "TimelineEvent(name='%s', start=%f, duration=%s%s)" % ( |
| 33 self.name, | 26 self.name, |
| 34 self.start, | 27 self.start, |
| 35 self.duration, | 28 self.duration, |
| 36 args_str) | 29 args_str) |
| 37 | |
| 38 @staticmethod | |
| 39 def _GetAllChildrenRecursive(events, item): | |
| 40 events.append(item) | |
| 41 for child in item.children: | |
| 42 TimelineEvent._GetAllChildrenRecursive(events, child) | |
| 43 | |
| 44 def GetAllChildrenRecursive(self, include_self=False): | |
| 45 events = [] | |
| 46 TimelineEvent._GetAllChildrenRecursive(events, self) | |
| 47 if not include_self: | |
| 48 del events[0] | |
| 49 return events | |
| 50 | |
| 51 def ShiftTimestampsForward(self, delta_time): | |
| 52 """ Shifts start time of event by delta_time and also | |
| 53 recursively shifts child events. | |
| 54 """ | |
| 55 for event in self.children: | |
| 56 event.ShiftTimestampsForward(delta_time) | |
| 57 self.start += delta_time | |
| 58 | |
| 59 def UpdateBounds(self): | |
| 60 """ Updates the start time to be the minimum start time of all | |
| 61 child events and the end time to be the maximum end time of all | |
| 62 child events. | |
| 63 """ | |
| 64 if not len(self.children): | |
| 65 return | |
| 66 | |
| 67 for event in self.children: | |
| 68 event.UpdateBounds() | |
| 69 | |
| 70 self.start = min(self.children, key=lambda e: e.start).start | |
| 71 end_timestamp = max(self.children, key=lambda e: e.end).end | |
| 72 self.duration = end_timestamp - self.start | |
| OLD | NEW |