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
.platform
import tracing_category_filter
8 from telemetry
.core
.platform
import tracing_options
9 from telemetry
.timeline
import trace_data
as trace_data_module
10 from telemetry
.timeline
.model
import TimelineModel
11 from telemetry
.page
import page_test
12 from telemetry
.page
.actions
import action_runner
13 from telemetry
.value
import list_of_scalar_values
14 from telemetry
.value
import scalar
15 from telemetry
.value
import trace
16 from telemetry
.web_perf
import timeline_interaction_record
as tir_module
17 from telemetry
.web_perf
.metrics
import smoothness
20 RUN_SMOOTH_ACTIONS
= 'RunSmoothAllActions'
23 class SmoothnessController(object):
25 self
._timeline
_model
= None
26 self
._trace
_data
= None
27 self
._interaction
= None
28 self
._surface
_flinger
_trace
_data
= None
30 def SetUp(self
, page
, tab
):
31 # FIXME: Remove webkit.console when blink.console lands in chromium and
32 # the ref builds are updated. crbug.com/386847
33 custom_categories
= ['webkit.console', 'blink.console', 'benchmark']
34 custom_categories
+= page
.GetSyntheticDelayCategories()
35 category_filter
= tracing_category_filter
.TracingCategoryFilter()
36 for c
in custom_categories
:
37 category_filter
.AddIncludedCategory(c
)
38 options
= tracing_options
.TracingOptions()
39 options
.enable_chrome_trace
= True
40 options
.enable_platform_display_trace
= True
41 tab
.browser
.platform
.tracing_controller
.Start(options
, category_filter
, 60)
44 # Start the smooth marker for all smooth actions.
45 runner
= action_runner
.ActionRunner(tab
)
46 self
._interaction
= runner
.BeginInteraction(
47 RUN_SMOOTH_ACTIONS
, is_smooth
=True)
50 # End the smooth marker for all smooth actions.
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(
63 run_smooth_actions_record
= None
65 for event
in renderer_thread
.async_slices
:
66 if not tir_module
.IsTimelineInteractionRecord(event
.name
):
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' %
73 run_smooth_actions_record
= r
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 '
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
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()