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 from telemetry
.core
.platform
import tracing_category_filter
5 from telemetry
.core
.platform
import tracing_options
6 from telemetry
.timeline
.model
import TimelineModel
7 from telemetry
.value
import trace
8 from telemetry
.web_perf
.metrics
import smoothness
9 from telemetry
.web_perf
import smooth_gesture_util
10 from telemetry
.web_perf
import timeline_interaction_record
as tir_module
12 from telemetry
.page
import page_test
15 class SmoothnessController(object):
17 self
._timeline
_model
= None
18 self
._trace
_data
= None
19 self
._interaction
= None
20 self
._surface
_flinger
_trace
_data
= None
22 def Start(self
, page
, tab
):
23 # FIXME: Remove webkit.console when blink.console lands in chromium and
24 # the ref builds are updated. crbug.com/386847
25 custom_categories
= ['webkit.console', 'blink.console', 'benchmark']
26 custom_categories
+= page
.GetSyntheticDelayCategories()
27 category_filter
= tracing_category_filter
.TracingCategoryFilter()
28 for c
in custom_categories
:
29 category_filter
.AddIncludedCategory(c
)
30 options
= tracing_options
.TracingOptions()
31 options
.enable_chrome_trace
= True
32 options
.enable_platform_display_trace
= True
33 tab
.browser
.platform
.tracing_controller
.Start(options
, category_filter
, 60)
36 self
._trace
_data
= tab
.browser
.platform
.tracing_controller
.Stop()
37 self
._timeline
_model
= TimelineModel(self
._trace
_data
)
39 def AddResults(self
, tab
, results
):
40 # Add results of smoothness metric. This computes the smoothness metric for
41 # the time ranges of gestures, if there is at least one, else the the time
42 # ranges from the first action to the last action.
43 results
.AddValue(trace
.TraceValue(
44 results
.current_page
, self
._trace
_data
))
45 renderer_thread
= self
._timeline
_model
.GetRendererThreadFromTabId(
48 for event
in renderer_thread
.async_slices
:
49 if not tir_module
.IsTimelineInteractionRecord(event
.name
):
51 r
= tir_module
.TimelineInteractionRecord
.FromAsyncEvent(event
)
52 smooth_records
.append(
53 smooth_gesture_util
.GetAdjustedInteractionIfContainGesture(
54 self
._timeline
_model
, r
))
56 # If there is no other smooth records, we make measurements on time range
57 # marked smoothness_controller itself.
58 # TODO(nednguyen): when crbug.com/239179 is marked fixed, makes sure that
59 # page sets are responsible for issueing the markers themselves.
60 if len(smooth_records
) == 0:
61 raise page_test
.Failure('Page failed to issue any markers.')
63 # Check to make sure all smooth records have same label and repeatable if
64 # there are more than one.
65 need_repeatable_flag
= len(smooth_records
) > 1
66 record_label
= smooth_records
[0].label
67 for r
in smooth_records
:
68 if r
.label
!= record_label
:
69 raise page_test
.Failure(
70 'SmoothController does not support multiple interactions with '
71 'different label. Interactions: %s' % repr(smooth_records
))
72 if need_repeatable_flag
and not r
.repeatable
:
73 raise page_test
.Failure(
74 'If there are more than one interaction record, each interaction '
75 'must has repeatable flag. Interactions: %s' % repr(smooth_records
))
77 # Create an interaction_record for this legacy measurement. Since we don't
78 # wrap the results that are sent to smoothness metric, the label will
80 smoothness_metric
= smoothness
.SmoothnessMetric()
81 smoothness_metric
.AddResults(
82 self
._timeline
_model
, renderer_thread
, smooth_records
, results
)
84 def CleanUp(self
, tab
):
85 if tab
.browser
.platform
.tracing_controller
.is_tracing_running
:
86 tab
.browser
.platform
.tracing_controller
.Stop()