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.
9 from measurements
import timeline_controller
10 from metrics
import speedindex
11 from telemetry
import benchmark
12 from telemetry
.core
import util
13 from telemetry
.page
import page_test
14 from telemetry
.timeline
import async_slice
as async_slice_module
15 from telemetry
.timeline
import slice as slice_module
16 from telemetry
.value
import scalar
19 class _ServiceWorkerTimelineMetric(object):
20 def AddResultsOfCounters(self
, process
, counter_regex_string
, results
):
21 counter_filter
= re
.compile(counter_regex_string
)
22 for counter_name
, counter
in process
.counters
.iteritems():
23 if not counter_filter
.search(counter_name
):
26 total
= sum(counter
.totals
)
28 # Results objects cannot contain the '.' character, so remove that here.
29 sanitized_counter_name
= counter_name
.replace('.', '_')
31 results
.AddValue(scalar
.ScalarValue(
32 results
.current_page
, sanitized_counter_name
, 'count', total
))
33 results
.AddValue(scalar
.ScalarValue(
34 results
.current_page
, sanitized_counter_name
+ '_avg', 'count',
35 total
/ float(len(counter
.totals
))))
37 def AddResultsOfEvents(
38 self
, process
, thread_regex_string
, event_regex_string
, results
):
39 thread_filter
= re
.compile(thread_regex_string
)
40 event_filter
= re
.compile(event_regex_string
)
42 for thread
in process
.threads
.itervalues():
43 thread_name
= thread
.name
.replace('/', '_')
44 if not thread_filter
.search(thread_name
):
48 for event
in thread
.IterAllEvents():
49 event_name
= event
.name
.replace('.', '_')
50 if event_filter
.search(event_name
):
51 filtered_events
.append(event
)
53 async_events_by_name
= collections
.defaultdict(list)
54 sync_events_by_name
= collections
.defaultdict(list)
55 for event
in filtered_events
:
56 if isinstance(event
, async_slice_module
.AsyncSlice
):
57 async_events_by_name
[event
.name
].append(event
)
58 elif isinstance(event
, slice_module
.Slice
):
59 sync_events_by_name
[event
.name
].append(event
)
61 for event_name
, event_group
in async_events_by_name
.iteritems():
62 times
= [e
.duration
for e
in event_group
]
63 self
._AddResultOfEvent
(thread_name
, event_name
, times
, results
)
65 for event_name
, event_group
in sync_events_by_name
.iteritems():
66 times
= [e
.self_time
for e
in event_group
]
67 self
._AddResultOfEvent
(thread_name
, event_name
, times
, results
)
69 def _AddResultOfEvent(self
, thread_name
, event_name
, times
, results
):
71 biggest_jank
= max(times
)
73 # Results objects cannot contain the '.' character, so remove that here.
74 sanitized_event_name
= event_name
.replace('.', '_')
76 full_name
= thread_name
+ '|' + sanitized_event_name
77 results
.AddValue(scalar
.ScalarValue(
78 results
.current_page
, full_name
, 'ms', total
))
79 results
.AddValue(scalar
.ScalarValue(
80 results
.current_page
, full_name
+ '_max', 'ms', biggest_jank
))
81 results
.AddValue(scalar
.ScalarValue(
82 results
.current_page
, full_name
+ '_avg', 'ms', total
/ len(times
)))
85 class _ServiceWorkerMeasurement(page_test
.PageTest
):
86 """Measure Speed Index and TRACE_EVENTs"""
89 super(_ServiceWorkerMeasurement
, self
).__init
__(
90 action_name_to_run
='RunPageInteractions')
91 self
._timeline
_controller
= timeline_controller
.TimelineController()
92 self
._speed
_index
= speedindex
.SpeedIndexMetric()
93 self
._page
_open
_times
= collections
.defaultdict(int)
95 def CustomizeBrowserOptions(self
, options
):
96 options
.AppendExtraBrowserArgs([
97 '--enable-experimental-web-platform-features'
100 def WillNavigateToPage(self
, page
, tab
):
101 self
._timeline
_controller
.SetUp(page
, tab
)
102 self
._timeline
_controller
.Start(tab
)
103 self
._speed
_index
.Start(page
, tab
)
105 def ValidateAndMeasurePage(self
, page
, tab
, results
):
106 tab
.WaitForDocumentReadyStateToBeComplete(40)
107 self
._timeline
_controller
.Stop(tab
, results
)
109 # Retrieve TRACE_EVENTs
110 timeline_metric
= _ServiceWorkerTimelineMetric()
111 browser_process
= self
._timeline
_controller
.model
.browser_process
112 filter_text
= '(RegisterServiceWorker|'\
113 'UnregisterServiceWorker|'\
115 'FindRegistrationForDocument|'\
116 'DispatchFetchEvent)'
117 timeline_metric
.AddResultsOfEvents(
118 browser_process
, 'IOThread', filter_text
, results
)
121 def SpeedIndexIsFinished():
122 return self
._speed
_index
.IsFinished(tab
)
123 util
.WaitFor(SpeedIndexIsFinished
, 60)
124 self
._speed
_index
.Stop(page
, tab
)
125 # Distinguish the first and second load from the subsequent loads
127 chart_prefix
= 'page_load'
128 self
._page
_open
_times
[url
] += 1
129 if self
._page
_open
_times
[url
] == 1:
130 chart_prefix
+= '_1st'
131 elif self
._page
_open
_times
[url
] == 2:
132 chart_prefix
+= '_2nd'
134 chart_prefix
+= '_later'
135 self
._speed
_index
.AddResults(tab
, results
, chart_prefix
)
138 class _ServiceWorkerMicroBenchmarkMeasurement(page_test
.PageTest
):
139 """Measure JS land values and TRACE_EVENTs"""
142 super(_ServiceWorkerMicroBenchmarkMeasurement
, self
).__init
__(
143 action_name_to_run
='RunPageInteractions')
144 self
._timeline
_controller
= timeline_controller
.TimelineController()
146 def CustomizeBrowserOptions(self
, options
):
147 options
.AppendExtraBrowserArgs([
148 '--enable-experimental-web-platform-features'
151 def WillNavigateToPage(self
, page
, tab
):
152 self
._timeline
_controller
.SetUp(page
, tab
)
153 self
._timeline
_controller
.Start(tab
)
155 def ValidateAndMeasurePage(self
, page
, tab
, results
):
156 tab
.WaitForJavaScriptExpression('window.done', 40)
157 self
._timeline
_controller
.Stop(tab
, results
)
159 # Measure JavaScript-land
160 json
= tab
.EvaluateJavaScript('window.results || {}')
161 for key
, value
in json
.iteritems():
162 results
.AddValue(scalar
.ScalarValue(
163 results
.current_page
, key
, value
['units'], value
['value']))
165 # Retrieve TRACE_EVENTs
166 timeline_metric
= _ServiceWorkerTimelineMetric()
167 browser_process
= self
._timeline
_controller
.model
.browser_process
168 filter_text
= '(RegisterServiceWorker|'\
169 'UnregisterServiceWorker|'\
171 'FindRegistrationForDocument|'\
172 'DispatchFetchEvent)'
173 timeline_metric
.AddResultsOfEvents(
174 browser_process
, 'IOThread', filter_text
, results
)
177 # TODO(simonhatch): Temporarily disabling (http://crbug.com/433943)
179 class ServiceWorkerPerfTest(benchmark
.Benchmark
):
180 """Performance test on public applications using ServiceWorker"""
181 test
= _ServiceWorkerMeasurement
182 page_set
= page_sets
.ServiceWorkerPageSet
186 return 'service_worker.service_worker'
189 # Disabled due to redness on the tree. crbug.com/442752
190 # TODO(horo): Enable after the reference build newer than M39 will be rolled.
191 @benchmark.Disabled('reference')
192 class ServiceWorkerMicroBenchmarkPerfTest(benchmark
.Benchmark
):
193 """This test measures the performance of pages using ServiceWorker.
195 As a page set, two benchamrk pages (many registration, many concurrent
196 fetching) and one application (Trained-to-thrill:
197 https://jakearchibald.github.io/trained-to-thrill/) are included. Execution
198 time of these pages will be shown as Speed Index, and TRACE_EVENTs are
199 subsidiary information to know more detail performance regression.
201 test
= _ServiceWorkerMicroBenchmarkMeasurement
202 page_set
= page_sets
.ServiceWorkerMicroBenchmarkPageSet
205 return 'service_worker.service_worker_micro_benchmark'