1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4 from measurements import smooth_gesture_util
6 from telemetry.core.platform import tracing_category_filter
7 from telemetry.timeline.model import TimelineModel
8 from telemetry.page.actions import action_runner
9 from telemetry.web_perf import timeline_interaction_record as tir_module
12 RUN_SMOOTH_ACTIONS = 'RunSmoothAllActions'
15 class TimelineController(object):
17 super(TimelineController, self).__init__()
18 self.trace_categories = None
20 self._renderer_process = None
21 self._smooth_records = []
22 self._interaction = None
24 def SetUp(self, page, tab):
25 """Starts gathering timeline data.
28 # Resets these member variables incase this object is reused.
30 self._renderer_process = None
31 if not tab.browser.supports_tracing:
32 raise Exception('Not supported')
33 category_filter = tracing_category_filter.TracingCategoryFilter(
34 filter_string=self.trace_categories)
35 for delay in page.GetSyntheticDelayCategories():
36 category_filter.AddSyntheticDelay(delay)
37 tab.browser.StartTracing(category_filter)
40 # Start the smooth marker for all actions.
41 runner = action_runner.ActionRunner(tab)
42 self._interaction = runner.BeginInteraction(
43 RUN_SMOOTH_ACTIONS, is_smooth=True)
46 # End the smooth marker for all actions.
47 self._interaction.End()
49 timeline_data = tab.browser.StopTracing()
50 self._model = TimelineModel(timeline_data)
51 self._renderer_process = self._model.GetRendererProcessFromTabId(tab.id)
52 renderer_thread = self.model.GetRendererThreadFromTabId(tab.id)
54 run_smooth_actions_record = None
55 self._smooth_records = []
56 for event in renderer_thread.async_slices:
57 if not tir_module.IsTimelineInteractionRecord(event.name):
59 r = tir_module.TimelineInteractionRecord.FromAsyncEvent(event)
60 if r.label == RUN_SMOOTH_ACTIONS:
61 assert run_smooth_actions_record is None, (
62 'TimelineController cannot issue more than 1 %s record' %
64 run_smooth_actions_record = r
66 self._smooth_records.append(
67 smooth_gesture_util.GetAdjustedInteractionIfContainGesture(
70 # If there is no other smooth records, we make measurements on time range
71 # marked by timeline_controller itself.
72 # TODO(nednguyen): when crbug.com/239179 is marked fixed, makes sure that
73 # page sets are responsible for issueing the markers themselves.
74 if len(self._smooth_records) == 0 and run_smooth_actions_record:
75 self._smooth_records = [run_smooth_actions_record]
78 def CleanUp(self, tab):
79 if tab.browser.is_tracing_running:
80 tab.browser.StopTracing()
87 def renderer_process(self):
88 return self._renderer_process
91 def smooth_records(self):
92 return self._smooth_records