Port Android relocation packer to chromium build
[chromium-blink-merge.git] / tools / perf / measurements / timeline_controller.py
blob6392859e535e5fef9ea897af556648e7935269e1
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.
5 from telemetry.core.platform import tracing_category_filter
6 from telemetry.core.platform import tracing_options
7 from telemetry.page.actions import action_runner
8 from telemetry.timeline.model import TimelineModel
9 from telemetry.value import trace
10 from telemetry.web_perf import timeline_interaction_record as tir_module
12 from measurements import smooth_gesture_util
15 RUN_SMOOTH_ACTIONS = 'RunSmoothAllActions'
18 class TimelineController(object):
19 def __init__(self):
20 super(TimelineController, self).__init__()
21 self.trace_categories = None
22 self._model = None
23 self._renderer_process = None
24 self._smooth_records = []
25 self._interaction = None
27 def SetUp(self, page, tab):
28 """Starts gathering timeline data.
30 """
31 # Resets these member variables incase this object is reused.
32 self._model = None
33 self._renderer_process = None
34 if not tab.browser.platform.tracing_controller.IsChromeTracingSupported():
35 raise Exception('Not supported')
36 category_filter = tracing_category_filter.TracingCategoryFilter(
37 filter_string=self.trace_categories)
38 for delay in page.GetSyntheticDelayCategories():
39 category_filter.AddSyntheticDelay(delay)
40 options = tracing_options.TracingOptions()
41 options.enable_chrome_trace = True
42 tab.browser.platform.tracing_controller.Start(options, category_filter)
44 def Start(self, tab):
45 # Start the smooth marker for all actions.
46 runner = action_runner.ActionRunner(tab)
47 self._interaction = runner.BeginInteraction(
48 RUN_SMOOTH_ACTIONS)
50 def Stop(self, tab, results):
51 # End the smooth marker for all actions.
52 self._interaction.End()
53 # Stop tracing.
54 timeline_data = tab.browser.platform.tracing_controller.Stop()
55 results.AddValue(trace.TraceValue(
56 results.current_page, timeline_data))
57 self._model = TimelineModel(timeline_data)
58 self._renderer_process = self._model.GetRendererProcessFromTabId(tab.id)
59 renderer_thread = self.model.GetRendererThreadFromTabId(tab.id)
61 run_smooth_actions_record = None
62 self._smooth_records = []
63 for event in renderer_thread.async_slices:
64 if not tir_module.IsTimelineInteractionRecord(event.name):
65 continue
66 r = tir_module.TimelineInteractionRecord.FromAsyncEvent(event)
67 if r.label == RUN_SMOOTH_ACTIONS:
68 assert run_smooth_actions_record is None, (
69 'TimelineController cannot issue more than 1 %s record' %
70 RUN_SMOOTH_ACTIONS)
71 run_smooth_actions_record = r
72 else:
73 self._smooth_records.append(
74 smooth_gesture_util.GetAdjustedInteractionIfContainGesture(
75 self.model, r))
77 # If there is no other smooth records, we make measurements on time range
78 # marked by timeline_controller itself.
79 # TODO(nednguyen): when crbug.com/239179 is marked fixed, makes sure that
80 # page sets are responsible for issueing the markers themselves.
81 if len(self._smooth_records) == 0 and run_smooth_actions_record:
82 self._smooth_records = [run_smooth_actions_record]
85 def CleanUp(self, tab):
86 if tab.browser.platform.tracing_controller.is_tracing_running:
87 tab.browser.platform.tracing_controller.Stop()
89 @property
90 def model(self):
91 return self._model
93 @property
94 def renderer_process(self):
95 return self._renderer_process
97 @property
98 def smooth_records(self):
99 return self._smooth_records