Enable 'unused-import' and 'import-error' lint check for perf/
[chromium-blink-merge.git] / tools / perf / measurements / smoothness_controller.py
blob1aa1fd7c5de5470dd439dee903a3c65ab7709840
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 import sys
6 from telemetry.core.platform import tracing_category_filter
7 from telemetry.core.platform import tracing_options
8 from telemetry.page import action_runner
9 from telemetry.timeline.model import TimelineModel
10 from telemetry.value import trace
11 from telemetry.web_perf.metrics import smoothness
12 from telemetry.web_perf import smooth_gesture_util
13 from telemetry.web_perf import timeline_interaction_record as tir_module
16 RUN_SMOOTH_ACTIONS = 'RunSmoothAllActions'
19 class SmoothnessController(object):
20 def __init__(self, auto_issuing_marker=True):
21 self._timeline_model = None
22 self._trace_data = None
23 self._interaction = None
24 self._surface_flinger_trace_data = None
25 self._auto_issuing_marker = auto_issuing_marker
27 def SetUp(self, page, tab):
28 # FIXME: Remove webkit.console when blink.console lands in chromium and
29 # the ref builds are updated. crbug.com/386847
30 custom_categories = ['webkit.console', 'blink.console', 'benchmark']
31 custom_categories += page.GetSyntheticDelayCategories()
32 category_filter = tracing_category_filter.TracingCategoryFilter()
33 for c in custom_categories:
34 category_filter.AddIncludedCategory(c)
35 options = tracing_options.TracingOptions()
36 options.enable_chrome_trace = True
37 options.enable_platform_display_trace = True
38 tab.browser.platform.tracing_controller.Start(options, category_filter, 60)
40 def Start(self, tab):
41 # Start the smooth marker for all smooth actions.
42 runner = action_runner.ActionRunner(tab)
43 if self._auto_issuing_marker:
44 self._interaction = runner.CreateInteraction(
45 RUN_SMOOTH_ACTIONS)
46 self._interaction.Begin()
48 def Stop(self, tab):
49 # End the smooth marker for all smooth actions.
50 if self._auto_issuing_marker:
51 self._interaction.End()
52 self._trace_data = tab.browser.platform.tracing_controller.Stop()
53 self._timeline_model = TimelineModel(self._trace_data)
55 def AddResults(self, tab, results):
56 # Add results of smoothness metric. This computes the smoothness metric for
57 # the time ranges of gestures, if there is at least one, else the the time
58 # ranges from the first action to the last action.
59 results.AddValue(trace.TraceValue(
60 results.current_page, self._trace_data))
61 renderer_thread = self._timeline_model.GetRendererThreadFromTabId(
62 tab.id)
63 run_smooth_actions_record = None
64 smooth_records = []
65 for event in renderer_thread.async_slices:
66 if not tir_module.IsTimelineInteractionRecord(event.name):
67 continue
68 r = tir_module.TimelineInteractionRecord.FromAsyncEvent(event)
69 if r.label == RUN_SMOOTH_ACTIONS:
70 assert run_smooth_actions_record is None, (
71 'SmoothnessController cannot issue more than 1 %s record' %
72 RUN_SMOOTH_ACTIONS)
73 run_smooth_actions_record = r
74 else:
75 smooth_records.append(
76 smooth_gesture_util.GetAdjustedInteractionIfContainGesture(
77 self._timeline_model, r))
79 # If there is no other smooth records, we make measurements on time range
80 # marked smoothness_controller itself.
81 # TODO(nednguyen): when crbug.com/239179 is marked fixed, makes sure that
82 # page sets are responsible for issueing the markers themselves.
83 if len(smooth_records) == 0:
84 if run_smooth_actions_record is None:
85 sys.stderr.write('Raw tracing data:\n')
86 self._trace_data.Serialize(sys.stderr)
87 sys.stderr.write('\n')
88 raise Exception('SmoothnessController failed to issue markers for the '
89 'whole interaction.')
90 else:
91 smooth_records = [run_smooth_actions_record]
93 # Create an interaction_record for this legacy measurement. Since we don't
94 # wrap the results that are sent to smoothness metric, the label will
95 # not be used.
96 smoothness_metric = smoothness.SmoothnessMetric()
97 smoothness_metric.AddResults(
98 self._timeline_model, renderer_thread, smooth_records, results)
100 def CleanUp(self, tab):
101 if tab.browser.platform.tracing_controller.is_tracing_running:
102 tab.browser.platform.tracing_controller.Stop()