Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / perf / measurements / timeline_controller.py
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
5
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
10
11
12 RUN_SMOOTH_ACTIONS = 'RunSmoothAllActions'
13
14
15 class TimelineController(object):
16   def __init__(self):
17     super(TimelineController, self).__init__()
18     self.trace_categories = None
19     self._model = None
20     self._renderer_process = None
21     self._smooth_records = []
22     self._interaction = None
23
24   def SetUp(self, page, tab):
25     """Starts gathering timeline data.
26
27     """
28     # Resets these member variables incase this object is reused.
29     self._model = None
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)
38
39   def Start(self, tab):
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)
44
45   def Stop(self, tab):
46     # End the smooth marker for all actions.
47     self._interaction.End()
48     # Stop tracing.
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)
53
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):
58         continue
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' %
63           RUN_SMOOTH_ACTIONS)
64         run_smooth_actions_record = r
65       elif r.is_smooth:
66         self._smooth_records.append(
67           smooth_gesture_util.GetAdjustedInteractionIfContainGesture(
68             self.model, r))
69
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]
76
77
78   def CleanUp(self, tab):
79     if tab.browser.is_tracing_running:
80       tab.browser.StopTracing()
81
82   @property
83   def model(self):
84     return self._model
85
86   @property
87   def renderer_process(self):
88     return self._renderer_process
89
90   @property
91   def smooth_records(self):
92     return self._smooth_records