Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / tools / perf / measurements / timeline_controller.py
blob893f74226b14f9845c31a0b06353413c99450cc6
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.page import action_runner
6 from telemetry.page import page_test
7 from telemetry.timeline.model import TimelineModel
8 from telemetry.timeline import tracing_category_filter
9 from telemetry.timeline import tracing_options
10 from telemetry.value import trace
11 from telemetry.web_perf import smooth_gesture_util
12 from telemetry.web_perf import timeline_interaction_record as tir_module
15 RUN_SMOOTH_ACTIONS = 'RunSmoothAllActions'
18 class TimelineController(object):
19 def __init__(self, enable_auto_issuing_record=True):
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
26 self._enable_auto_issuing_record = enable_auto_issuing_record
28 def SetUp(self, page, tab):
29 """Starts gathering timeline data.
31 """
32 # Resets these member variables incase this object is reused.
33 self._model = None
34 self._renderer_process = None
35 if not tab.browser.platform.tracing_controller.IsChromeTracingSupported():
36 raise Exception('Not supported')
37 category_filter = tracing_category_filter.TracingCategoryFilter(
38 filter_string=self.trace_categories)
39 for delay in page.GetSyntheticDelayCategories():
40 category_filter.AddSyntheticDelay(delay)
41 options = tracing_options.TracingOptions()
42 options.enable_chrome_trace = True
43 tab.browser.platform.tracing_controller.Start(options, category_filter)
45 def Start(self, tab):
46 # Start the smooth marker for all actions.
47 runner = action_runner.ActionRunner(tab)
48 if self._enable_auto_issuing_record:
49 self._interaction = runner.CreateInteraction(
50 RUN_SMOOTH_ACTIONS)
51 self._interaction.Begin()
53 def Stop(self, tab, results):
54 # End the smooth marker for all actions.
55 if self._enable_auto_issuing_record:
56 self._interaction.End()
57 # Stop tracing.
58 timeline_data = tab.browser.platform.tracing_controller.Stop()
59 results.AddValue(trace.TraceValue(
60 results.current_page, timeline_data))
61 self._model = TimelineModel(timeline_data)
62 self._renderer_process = self._model.GetRendererProcessFromTabId(tab.id)
63 renderer_thread = self.model.GetRendererThreadFromTabId(tab.id)
65 run_smooth_actions_record = None
66 self._smooth_records = []
67 for event in renderer_thread.async_slices:
68 if not tir_module.IsTimelineInteractionRecord(event.name):
69 continue
70 r = tir_module.TimelineInteractionRecord.FromAsyncEvent(event)
71 if r.label == RUN_SMOOTH_ACTIONS:
72 assert run_smooth_actions_record is None, (
73 'TimelineController cannot issue more than 1 %s record' %
74 RUN_SMOOTH_ACTIONS)
75 run_smooth_actions_record = r
76 else:
77 self._smooth_records.append(
78 smooth_gesture_util.GetAdjustedInteractionIfContainGesture(
79 self.model, r))
81 # If there is no other smooth records, we make measurements on time range
82 # marked by timeline_controller itself.
83 # TODO(nednguyen): when crbug.com/239179 is marked fixed, makes sure that
84 # page sets are responsible for issueing the markers themselves.
85 if len(self._smooth_records) == 0 and run_smooth_actions_record:
86 self._smooth_records = [run_smooth_actions_record]
88 if len(self._smooth_records) == 0:
89 raise page_test.Failure('No interaction record was created.')
92 def CleanUp(self, platform):
93 if platform.tracing_controller.is_tracing_running:
94 platform.tracing_controller.Stop()
96 @property
97 def model(self):
98 return self._model
100 @property
101 def renderer_process(self):
102 return self._renderer_process
104 @property
105 def smooth_records(self):
106 return self._smooth_records