1 # Copyright 2013 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 metrics
import Metric
7 from telemetry
.page
import page_measurement
9 TRACING_MODE
= 'tracing-mode'
10 TIMELINE_MODE
= 'timeline-mode'
12 class TimelineMetric(Metric
):
13 def __init__(self
, mode
):
14 assert mode
in (TRACING_MODE
, TIMELINE_MODE
)
15 super(TimelineMetric
, self
).__init
__()
18 self
._thread
_for
_tab
= None
20 def Start(self
, page
, tab
):
22 self
._thread
_for
_tab
= None
24 if self
._mode
== TRACING_MODE
:
25 if not tab
.browser
.supports_tracing
:
26 raise Exception('Not supported')
27 tab
.browser
.StartTracing()
29 assert self
._mode
== TIMELINE_MODE
30 tab
.StartTimelineRecording()
32 def Stop(self
, page
, tab
):
33 if self
._mode
== TRACING_MODE
:
34 # This creates an async trace event in the render process for tab that
35 # will allow us to find that tab during the AddTracingResultsForTab
37 success
= tab
.EvaluateJavaScript("""
38 console.time("__loading_measurement_was_here__");
39 console.timeEnd("__loading_measurement_was_here__");
40 console.time.toString().indexOf('[native code]') != -1;
42 trace_result
= tab
.browser
.StopTracing()
44 raise page_measurement
.MeasurementFailure(
45 'Page stomped on console.time')
46 self
._model
= trace_result
.AsTimelineModel()
48 s
in self
._model
.GetAllEventsOfName(
49 '__loading_measurement_was_here__')
50 if s
.parent_slice
== None]
51 assert len(events
) == 1, 'Expected one marker, got %d' % len(events
)
52 # TODO(tonyg): This should be threads_for_tab and report events for both
53 # the starting thread and ending thread.
54 self
._thread
_for
_tab
= events
[0].start_thread
56 tab
.StopTimelineRecording()
57 self
._model
= tab
.timeline_model
58 self
._thread
_for
_tab
= self
._model
.GetAllThreads()[0]
60 def AddResults(self
, tab
, results
):
63 events
= self
._thread
_for
_tab
.all_slices
65 events_by_name
= collections
.defaultdict(list)
67 events_by_name
[e
.name
].append(e
)
69 for event_name
, event_group
in events_by_name
.iteritems():
70 times
= [event
.self_time
for event
in event_group
]
72 biggest_jank
= max(times
)
73 results
.Add(event_name
, 'ms', total
)
74 results
.Add(event_name
+ '_max', 'ms', biggest_jank
)
75 results
.Add(event_name
+ '_avg', 'ms', total
/ len(times
))
77 counters_by_name
= self
._thread
_for
_tab
.parent
.counters
78 for counter_name
, counter
in counters_by_name
.iteritems():
79 total
= sum(counter
.totals
)
80 results
.Add(counter_name
, 'count', total
)
81 results
.Add(counter_name
+ '_avg', 'count', total
/ len(counter
.totals
))