| Index: tools/telemetry/telemetry/inspector_timeline.py
|
| diff --git a/tools/telemetry/telemetry/inspector_timeline.py b/tools/telemetry/telemetry/inspector_timeline.py
|
| index e0810c6e5959bebbd02b18a533384a72ed38ac64..8faf38ddb6512382d4d44842e2519d4c5b9508fa 100644
|
| --- a/tools/telemetry/telemetry/inspector_timeline.py
|
| +++ b/tools/telemetry/telemetry/inspector_timeline.py
|
| @@ -1,51 +1,12 @@
|
| # Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| # Use of this source code is governed by a BSD-style license that can be
|
| # found in the LICENSE file.
|
| -
|
| +from telemetry.timeline_event import TimelineEvent
|
| +from telemetry.timeline_model import TimelineModel
|
|
|
| class InspectorBackendException(Exception):
|
| pass
|
|
|
| -
|
| -class TimelineEvent(object):
|
| - """Represents a timeline event."""
|
| - def __init__(self, d):
|
| - self.__dict__.update(d)
|
| -
|
| - @property
|
| - def type(self):
|
| - return self.__dict__.get('type')
|
| -
|
| - @property
|
| - def start_time(self):
|
| - return self.__dict__.get('startTime', 0)
|
| -
|
| - @property
|
| - def end_time(self):
|
| - return self.__dict__.get('endTime', 0)
|
| -
|
| - @property
|
| - def elapsed_time(self):
|
| - return self.end_time - self.start_time
|
| -
|
| -
|
| -class TimelineEvents(object):
|
| - def __init__(self):
|
| - self._events = []
|
| -
|
| - def AppendRawEvents(self, raw_inspector_stream):
|
| - if raw_inspector_stream.get('params', {}).get('record'):
|
| - self._FlattenEvents(raw_inspector_stream['params']['record'])
|
| -
|
| - def _FlattenEvents(self, raw_inspector_events):
|
| - self._events.append(TimelineEvent(raw_inspector_events))
|
| - for child in raw_inspector_events.get('children', []):
|
| - self._FlattenEvents(child)
|
| -
|
| - def GetAllOfType(self, type_name):
|
| - return [e for e in self._events if e.type == type_name]
|
| -
|
| -
|
| class InspectorTimeline(object):
|
| """Implementation of dev tools timeline."""
|
| class Recorder(object):
|
| @@ -63,17 +24,17 @@ class InspectorTimeline(object):
|
| self._inspector_backend = inspector_backend
|
| self._tab = tab
|
| self._is_recording = False
|
| - self._timeline_events = None
|
| + self._timeline_model = None
|
|
|
| @property
|
| - def timeline_events(self):
|
| - return self._timeline_events
|
| + def timeline_model(self):
|
| + return self._timeline_model
|
|
|
| def Start(self):
|
| if self._is_recording:
|
| return
|
| - self._timeline_events = TimelineEvents()
|
| self._is_recording = True
|
| + self._timeline_model = TimelineModel()
|
| self._inspector_backend.RegisterDomain('Timeline',
|
| self._OnNotification, self._OnClose)
|
| req = {'method': 'Timeline.start'}
|
| @@ -83,6 +44,7 @@ class InspectorTimeline(object):
|
| if not self._is_recording:
|
| raise InspectorBackendException('Stop() called but not started')
|
| self._is_recording = False
|
| + self._timeline_model.DidFinishRecording()
|
| req = {'method': 'Timeline.stop'}
|
| self._SendSyncRequest(req)
|
| self._inspector_backend.UnregisterDomain('Timeline')
|
| @@ -97,7 +59,63 @@ class InspectorTimeline(object):
|
| if not self._is_recording:
|
| return
|
| if 'method' in msg and msg['method'] == 'Timeline.eventRecorded':
|
| - self._timeline_events.AppendRawEvents(msg)
|
| + self._OnEventRecorded(msg)
|
| +
|
| + def _OnEventRecorded(self, msg):
|
| + record = msg.get('params', {}).get('record')
|
| + if record:
|
| + newly_created_event = InspectorTimeline.RawEventToTimelineEvent(record)
|
| + self._timeline_model.AddEvent(newly_created_event)
|
| +
|
| + @staticmethod
|
| + def RawEventToTimelineEvent(raw_inspector_event):
|
| + """Converts raw_inspector_event to TimelineEvent."""
|
| + return InspectorTimeline._RawEventToTimelineEventRecursive(
|
| + None, raw_inspector_event)
|
| +
|
| + @staticmethod
|
| + def _RawEventToTimelineEventRecursive(
|
| + parent_for_created_events, raw_inspector_event):
|
| + """
|
| + Creates a new TimelineEvent for the raw_inspector_event, if possible, adding
|
| + it to the provided parent_for_created_events.
|
| +
|
| + It then recurses on any child events found inside, building a tree of
|
| + TimelineEvents.
|
| +
|
| + Returns the root of the created tree, or None.
|
| + """
|
| + # Create a TimelineEvent for this raw_inspector_event if possible. Only
|
| + # events with start-time and end-time get imported.
|
| + if ('startTime' in raw_inspector_event and
|
| + 'endTime' in raw_inspector_event):
|
| + args = {}
|
| + for x in raw_inspector_event:
|
| + if x in ('startTime', 'endTime', 'children'):
|
| + continue
|
| + args[x] = raw_inspector_event[x]
|
| + if len(args) == 0:
|
| + args = None
|
| + newly_created_event = TimelineEvent(
|
| + name=raw_inspector_event['type'],
|
| + start_time_ms=raw_inspector_event['startTime'],
|
| + duration_ms=(raw_inspector_event['endTime'] -
|
| + raw_inspector_event['startTime']),
|
| + args=args)
|
| + if parent_for_created_events:
|
| + parent_for_created_events.children.append(newly_created_event)
|
| + else:
|
| + newly_created_event = None
|
| +
|
| + # Process any children events, creating TimelineEvents for them as well.
|
| + if newly_created_event:
|
| + parent_for_children = newly_created_event
|
| + else:
|
| + parent_for_children = parent_for_created_events
|
| + for child in raw_inspector_event.get('children', []):
|
| + InspectorTimeline._RawEventToTimelineEventRecursive(
|
| + parent_for_children, child)
|
| + return newly_created_event
|
|
|
| def _OnClose(self):
|
| if self._is_recording:
|
|
|