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.
6 from measurements import smooth_gesture_util
7 from telemetry.core.timeline.model import TimelineModel
8 from telemetry.page import page_measurement
9 from telemetry.page.actions import action_runner
10 from telemetry.web_perf import timeline_interaction_record as tir_module
11 from telemetry.web_perf.metrics import smoothness
14 RUN_SMOOTH_ACTIONS = 'RunSmoothAllActions'
17 class MissingDisplayFrameRateError(page_measurement.MeasurementFailure):
18 def __init__(self, name):
19 super(MissingDisplayFrameRateError, self).__init__(
20 'Missing display frame rate metrics: ' + name)
22 class SmoothnessController(object):
24 self._timeline_model = None
25 self._tracing_timeline_data = None
27 def Start(self, page, tab):
28 custom_categories = ['webkit.console', 'benchmark']
29 custom_categories += page.GetSyntheticDelayCategories()
30 tab.browser.StartTracing(','.join(custom_categories), 60)
31 if tab.browser.platform.IsRawDisplayFrameRateSupported():
32 tab.browser.platform.StartRawDisplayFrameRateMeasurement()
33 # Start the smooth marker for all smooth actions.
34 runner = action_runner.ActionRunner(None, tab)
35 runner.BeginInteraction(RUN_SMOOTH_ACTIONS, [tir_module.IS_SMOOTH])
38 # End the smooth marker for all smooth actions.
39 runner = action_runner.ActionRunner(None, tab)
40 runner.EndInteraction(RUN_SMOOTH_ACTIONS, [tir_module.IS_SMOOTH])
41 # Stop tracing for smoothness metric.
42 if tab.browser.platform.IsRawDisplayFrameRateSupported():
43 tab.browser.platform.StopRawDisplayFrameRateMeasurement()
44 self._tracing_timeline_data = tab.browser.StopTracing()
45 self._timeline_model = TimelineModel(
46 timeline_data=self._tracing_timeline_data)
48 def AddResults(self, tab, results):
49 # Add results of smoothness metric. This computes the smoothness metric for
50 # the time ranges of gestures, if there is at least one, else the the time
51 # ranges from the first action to the last action.
53 renderer_thread = self._timeline_model.GetRendererThreadFromTab(tab)
54 run_smooth_actions_record = None
56 for event in renderer_thread.async_slices:
57 if not tir_module.IsTimelineInteractionRecord(event.name):
59 r = tir_module.TimelineInteractionRecord.FromEvent(event)
60 if r.logical_name == RUN_SMOOTH_ACTIONS:
61 assert run_smooth_actions_record is None, (
62 'SmoothnessController cannot issue more than 1 %s record' %
64 run_smooth_actions_record = r
66 smooth_records.append(
67 smooth_gesture_util.GetAdjustedInteractionIfContainGesture(
68 self._timeline_model, r))
70 # If there is no other smooth records, we make measurements on time range
71 # marked smoothness_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(smooth_records) == 0:
75 if run_smooth_actions_record is None:
76 sys.stderr.write('Raw tracing data:\n')
77 sys.stderr.write(repr(self._tracing_timeline_data.EventData()))
78 sys.stderr.write('\n')
79 raise Exception('SmoothnessController failed to issue markers for the '
82 smooth_records = [run_smooth_actions_record]
84 # Create an interaction_record for this legacy measurement. Since we don't
85 # wrap the results that is sent to smoothnes metric, the logical_name will
87 smoothness_metric = smoothness.SmoothnessMetric()
88 smoothness_metric.AddResults(
89 self._timeline_model, renderer_thread, smooth_records, results)
90 if tab.browser.platform.IsRawDisplayFrameRateSupported():
91 for r in tab.browser.platform.GetRawDisplayFrameRateMeasurements():
93 raise MissingDisplayFrameRateError(r.name)
94 results.Add(r.name, r.unit, r.value)
96 def CleanUp(self, tab):
97 if tab.browser.platform.IsRawDisplayFrameRateSupported():
98 tab.browser.platform.StopRawDisplayFrameRateMeasurement()
99 if tab.browser.is_tracing_running:
100 tab.browser.StopTracing()