Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / tools / perf / measurements / smoothness_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 import sys
5
6 from measurements import smooth_gesture_util
7 from telemetry.core.platform import tracing_category_filter
8 from telemetry.core.platform import tracing_options
9 from telemetry.timeline.model import TimelineModel
10 from telemetry.page import page_test
11 from telemetry.page.actions import action_runner
12 from telemetry.value import list_of_scalar_values
13 from telemetry.value import scalar
14 from telemetry.value import trace
15 from telemetry.web_perf import timeline_interaction_record as tir_module
16 from telemetry.web_perf.metrics import smoothness
17
18
19 RUN_SMOOTH_ACTIONS = 'RunSmoothAllActions'
20
21 # Descriptions for results from platform.GetRawDisplayFrameRateMeasurements().
22 DESCRIPTIONS = {
23     'avg_surface_fps': 'Average frames per second as measured by the '
24                        'platform\'s SurfaceFlinger.'
25 }
26
27
28 class MissingDisplayFrameRateError(page_test.MeasurementFailure):
29   def __init__(self, name):
30     super(MissingDisplayFrameRateError, self).__init__(
31       'Missing display frame rate metrics: ' + name)
32
33 class SmoothnessController(object):
34   def __init__(self):
35     self._timeline_model = None
36     self._tracing_timeline_data = None
37     self._interaction = None
38
39   def SetUp(self, page, tab):
40     # FIXME: Remove webkit.console when blink.console lands in chromium and
41     # the ref builds are updated. crbug.com/386847
42     custom_categories = ['webkit.console', 'blink.console', 'benchmark']
43     custom_categories += page.GetSyntheticDelayCategories()
44     category_filter = tracing_category_filter.TracingCategoryFilter()
45     for c in custom_categories:
46       category_filter.AddIncludedCategory(c)
47     options = tracing_options.TracingOptions()
48     options.enable_chrome_trace = True
49     tab.browser.platform.tracing_controller.Start(options, category_filter, 60)
50     if tab.browser.platform.IsRawDisplayFrameRateSupported():
51       tab.browser.platform.StartRawDisplayFrameRateMeasurement()
52
53   def Start(self, tab):
54     # Start the smooth marker for all smooth actions.
55     runner = action_runner.ActionRunner(tab)
56     self._interaction = runner.BeginInteraction(
57         RUN_SMOOTH_ACTIONS, is_smooth=True)
58
59   def Stop(self, tab):
60     # End the smooth marker for  all smooth actions.
61     self._interaction.End()
62     # Stop tracing for smoothness metric.
63     if tab.browser.platform.IsRawDisplayFrameRateSupported():
64       tab.browser.platform.StopRawDisplayFrameRateMeasurement()
65     self._tracing_timeline_data = tab.browser.platform.tracing_controller.Stop()
66     self._timeline_model = TimelineModel(
67       timeline_data=self._tracing_timeline_data)
68
69   def AddResults(self, tab, results):
70     # Add results of smoothness metric. This computes the smoothness metric for
71     # the time ranges of gestures, if there is at least one, else the the time
72     # ranges from the first action to the last action.
73     results.AddValue(trace.TraceValue(
74         results.current_page, self._tracing_timeline_data))
75     renderer_thread = self._timeline_model.GetRendererThreadFromTabId(
76         tab.id)
77     run_smooth_actions_record = None
78     smooth_records = []
79     for event in renderer_thread.async_slices:
80       if not tir_module.IsTimelineInteractionRecord(event.name):
81         continue
82       r = tir_module.TimelineInteractionRecord.FromAsyncEvent(event)
83       if r.label == RUN_SMOOTH_ACTIONS:
84         assert run_smooth_actions_record is None, (
85           'SmoothnessController cannot issue more than 1 %s record' %
86           RUN_SMOOTH_ACTIONS)
87         run_smooth_actions_record = r
88       elif r.is_smooth:
89         smooth_records.append(
90           smooth_gesture_util.GetAdjustedInteractionIfContainGesture(
91             self._timeline_model, r))
92
93     # If there is no other smooth records, we make measurements on time range
94     # marked smoothness_controller itself.
95     # TODO(nednguyen): when crbug.com/239179 is marked fixed, makes sure that
96     # page sets are responsible for issueing the markers themselves.
97     if len(smooth_records) == 0:
98       if run_smooth_actions_record is None:
99         sys.stderr.write('Raw tracing data:\n')
100         sys.stderr.write(repr(self._tracing_timeline_data.EventData()))
101         sys.stderr.write('\n')
102         raise Exception('SmoothnessController failed to issue markers for the '
103                         'whole interaction.')
104       else:
105         smooth_records = [run_smooth_actions_record]
106
107     # Create an interaction_record for this legacy measurement. Since we don't
108     # wrap the results that are sent to smoothness metric, the label will
109     # not be used.
110     smoothness_metric = smoothness.SmoothnessMetric()
111     smoothness_metric.AddResults(
112       self._timeline_model, renderer_thread, smooth_records, results)
113     if tab.browser.platform.IsRawDisplayFrameRateSupported():
114       for r in tab.browser.platform.GetRawDisplayFrameRateMeasurements():
115         if r.value is None:
116           raise MissingDisplayFrameRateError(r.name)
117         if isinstance(r.value, list):
118           results.AddValue(list_of_scalar_values.ListOfScalarValues(
119               results.current_page, r.name, r.unit, r.value,
120               description=DESCRIPTIONS.get(r.name)))
121         else:
122           results.AddValue(scalar.ScalarValue(
123               results.current_page, r.name, r.unit, r.value,
124               description=DESCRIPTIONS.get(r.name)))
125
126   def CleanUp(self, tab):
127     if tab.browser.platform.IsRawDisplayFrameRateSupported():
128       tab.browser.platform.StopRawDisplayFrameRateMeasurement()
129     if tab.browser.platform.tracing_controller.is_tracing_running:
130       tab.browser.platform.tracing_controller.Stop()