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 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
.page
import page_test
10 from telemetry
.timeline
.model
import TimelineModel
11 from telemetry
.timeline
import trace_data
as trace_data_module
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
.metrics
import smoothness
16 from telemetry
.web_perf
import timeline_interaction_record
as tir_module
18 from measurements
import smooth_gesture_util
21 RUN_SMOOTH_ACTIONS
= 'RunSmoothAllActions'
24 class SmoothnessController(object):
26 self
._timeline
_model
= None
27 self
._trace
_data
= None
28 self
._interaction
= None
29 self
._surface
_flinger
_trace
_data
= None
31 def SetUp(self
, page
, tab
):
32 # FIXME: Remove webkit.console when blink.console lands in chromium and
33 # the ref builds are updated. crbug.com/386847
34 custom_categories
= ['webkit.console', 'blink.console', 'benchmark']
35 custom_categories
+= page
.GetSyntheticDelayCategories()
36 category_filter
= tracing_category_filter
.TracingCategoryFilter()
37 for c
in custom_categories
:
38 category_filter
.AddIncludedCategory(c
)
39 options
= tracing_options
.TracingOptions()
40 options
.enable_chrome_trace
= True
41 options
.enable_platform_display_trace
= True
42 tab
.browser
.platform
.tracing_controller
.Start(options
, category_filter
, 60)
45 # Start the smooth marker for all smooth actions.
46 runner
= action_runner
.ActionRunner(tab
)
47 self
._interaction
= runner
.BeginInteraction(
51 # End the smooth marker for all smooth actions.
52 self
._interaction
.End()
53 self
._trace
_data
= tab
.browser
.platform
.tracing_controller
.Stop()
54 self
._timeline
_model
= TimelineModel(self
._trace
_data
)
56 def AddResults(self
, tab
, results
):
57 # Add results of smoothness metric. This computes the smoothness metric for
58 # the time ranges of gestures, if there is at least one, else the the time
59 # ranges from the first action to the last action.
60 results
.AddValue(trace
.TraceValue(
61 results
.current_page
, self
._trace
_data
))
62 renderer_thread
= self
._timeline
_model
.GetRendererThreadFromTabId(
64 run_smooth_actions_record
= None
66 for event
in renderer_thread
.async_slices
:
67 if not tir_module
.IsTimelineInteractionRecord(event
.name
):
69 r
= tir_module
.TimelineInteractionRecord
.FromAsyncEvent(event
)
70 if r
.label
== RUN_SMOOTH_ACTIONS
:
71 assert run_smooth_actions_record
is None, (
72 'SmoothnessController cannot issue more than 1 %s record' %
74 run_smooth_actions_record
= r
76 smooth_records
.append(
77 smooth_gesture_util
.GetAdjustedInteractionIfContainGesture(
78 self
._timeline
_model
, r
))
80 # If there is no other smooth records, we make measurements on time range
81 # marked smoothness_controller itself.
82 # TODO(nednguyen): when crbug.com/239179 is marked fixed, makes sure that
83 # page sets are responsible for issueing the markers themselves.
84 if len(smooth_records
) == 0:
85 if run_smooth_actions_record
is None:
86 sys
.stderr
.write('Raw tracing data:\n')
87 self
._trace
_data
.Serialize(sys
.stderr
)
88 sys
.stderr
.write('\n')
89 raise Exception('SmoothnessController failed to issue markers for the '
92 smooth_records
= [run_smooth_actions_record
]
94 # Create an interaction_record for this legacy measurement. Since we don't
95 # wrap the results that are sent to smoothness metric, the label will
97 smoothness_metric
= smoothness
.SmoothnessMetric()
98 smoothness_metric
.AddResults(
99 self
._timeline
_model
, renderer_thread
, smooth_records
, results
)
101 def CleanUp(self
, tab
):
102 if tab
.browser
.platform
.tracing_controller
.is_tracing_running
:
103 tab
.browser
.platform
.tracing_controller
.Stop()