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.
5 from telemetry
.page
import action_runner
6 from telemetry
.page
import page_test
7 from telemetry
.timeline
.model
import TimelineModel
8 from telemetry
.timeline
import tracing_category_filter
9 from telemetry
.timeline
import tracing_options
10 from telemetry
.value
import trace
11 from telemetry
.web_perf
import smooth_gesture_util
12 from telemetry
.web_perf
import timeline_interaction_record
as tir_module
15 RUN_SMOOTH_ACTIONS
= 'RunSmoothAllActions'
18 class TimelineController(object):
19 def __init__(self
, enable_auto_issuing_record
=True):
20 super(TimelineController
, self
).__init
__()
21 self
.trace_categories
= None
23 self
._renderer
_process
= None
24 self
._smooth
_records
= []
25 self
._interaction
= None
26 self
._enable
_auto
_issuing
_record
= enable_auto_issuing_record
28 def SetUp(self
, page
, tab
):
29 """Starts gathering timeline data.
32 # Resets these member variables incase this object is reused.
34 self
._renderer
_process
= None
35 if not tab
.browser
.platform
.tracing_controller
.IsChromeTracingSupported():
36 raise Exception('Not supported')
37 category_filter
= tracing_category_filter
.TracingCategoryFilter(
38 filter_string
=self
.trace_categories
)
39 for delay
in page
.GetSyntheticDelayCategories():
40 category_filter
.AddSyntheticDelay(delay
)
41 options
= tracing_options
.TracingOptions()
42 options
.enable_chrome_trace
= True
43 tab
.browser
.platform
.tracing_controller
.Start(options
, category_filter
)
46 # Start the smooth marker for all actions.
47 runner
= action_runner
.ActionRunner(tab
)
48 if self
._enable
_auto
_issuing
_record
:
49 self
._interaction
= runner
.CreateInteraction(
51 self
._interaction
.Begin()
53 def Stop(self
, tab
, results
):
54 # End the smooth marker for all actions.
55 if self
._enable
_auto
_issuing
_record
:
56 self
._interaction
.End()
58 timeline_data
= tab
.browser
.platform
.tracing_controller
.Stop()
59 results
.AddValue(trace
.TraceValue(
60 results
.current_page
, timeline_data
))
61 self
._model
= TimelineModel(timeline_data
)
62 self
._renderer
_process
= self
._model
.GetRendererProcessFromTabId(tab
.id)
63 renderer_thread
= self
.model
.GetRendererThreadFromTabId(tab
.id)
65 run_smooth_actions_record
= None
66 self
._smooth
_records
= []
67 for event
in renderer_thread
.async_slices
:
68 if not tir_module
.IsTimelineInteractionRecord(event
.name
):
70 r
= tir_module
.TimelineInteractionRecord
.FromAsyncEvent(event
)
71 if r
.label
== RUN_SMOOTH_ACTIONS
:
72 assert run_smooth_actions_record
is None, (
73 'TimelineController cannot issue more than 1 %s record' %
75 run_smooth_actions_record
= r
77 self
._smooth
_records
.append(
78 smooth_gesture_util
.GetAdjustedInteractionIfContainGesture(
81 # If there is no other smooth records, we make measurements on time range
82 # marked by timeline_controller itself.
83 # TODO(nednguyen): when crbug.com/239179 is marked fixed, makes sure that
84 # page sets are responsible for issueing the markers themselves.
85 if len(self
._smooth
_records
) == 0 and run_smooth_actions_record
:
86 self
._smooth
_records
= [run_smooth_actions_record
]
88 if len(self
._smooth
_records
) == 0:
89 raise page_test
.Failure('No interaction record was created.')
92 def CleanUp(self
, platform
):
93 if platform
.tracing_controller
.is_tracing_running
:
94 platform
.tracing_controller
.Stop()
101 def renderer_process(self
):
102 return self
._renderer
_process
105 def smooth_records(self
):
106 return self
._smooth
_records