1 # Copyright 2012 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 """The page cycler measurement.
7 This measurement registers a window load handler in which is forces a layout and
8 then records the value of performance.now(). This call to now() measures the
9 time from navigationStart (immediately after the previous page's beforeunload
10 event) until after the layout in the page's load event. In addition, two garbage
11 collections are performed in between the page loads (in the beforeunload event).
12 This extra garbage collection time is not included in the measurement times.
14 Finally, various memory and IO statistics are gathered at the very end of
21 from metrics
import cpu
22 from metrics
import memory
23 from metrics
import power
24 from metrics
import speedindex
25 from metrics
import v8_object_stats
26 from telemetry
.core
import util
27 from telemetry
.page
import page_test
28 from telemetry
.value
import scalar
31 class PageCycler(page_test
.PageTest
):
32 def __init__(self
, page_repeat
, pageset_repeat
, cold_load_percent
=50,
33 record_v8_object_stats
=False, report_speed_index
=False,
34 clear_cache_before_each_run
=False):
35 super(PageCycler
, self
).__init
__(
36 clear_cache_before_each_run
=clear_cache_before_each_run
)
38 with
open(os
.path
.join(os
.path
.dirname(__file__
),
39 'page_cycler.js'), 'r') as f
:
40 self
._page
_cycler
_js
= f
.read()
42 self
._record
_v
8_object
_stats
= record_v8_object_stats
43 self
._report
_speed
_index
= report_speed_index
44 self
._speedindex
_metric
= speedindex
.SpeedIndexMetric()
45 self
._memory
_metric
= None
46 self
._power
_metric
= None
47 self
._cpu
_metric
= None
48 self
._v
8_object
_stats
_metric
= None
49 self
._has
_loaded
_page
= collections
.defaultdict(int)
50 self
._initial
_renderer
_url
= None # to avoid cross-renderer navigation
52 cold_runs_percent_set
= (cold_load_percent
!= None)
53 # Handle requests for cold cache runs
54 if (cold_runs_percent_set
and
55 (cold_load_percent
< 0 or cold_load_percent
> 100)):
56 raise Exception('cold-load-percent must be in the range [0-100]')
58 # Make sure _cold_run_start_index is an integer multiple of page_repeat.
59 # Without this, --pageset_shuffle + --page_repeat could lead to
60 # assertion failures on _started_warm in WillNavigateToPage.
61 if cold_runs_percent_set
:
62 number_warm_pageset_runs
= int(
63 (int(pageset_repeat
) - 1) * (100 - cold_load_percent
) / 100)
64 number_warm_runs
= number_warm_pageset_runs
* page_repeat
65 self
._cold
_run
_start
_index
= number_warm_runs
+ page_repeat
66 self
._discard
_first
_result
= (not cold_load_percent
or
67 self
._discard
_first
_result
)
69 self
._cold
_run
_start
_index
= pageset_repeat
* page_repeat
71 def WillStartBrowser(self
, platform
):
72 """Initialize metrics once right before the browser has been launched."""
73 self
._power
_metric
= power
.PowerMetric(platform
)
75 def DidStartBrowser(self
, browser
):
76 """Initialize metrics once right after the browser has been launched."""
77 self
._memory
_metric
= memory
.MemoryMetric(browser
)
78 self
._cpu
_metric
= cpu
.CpuMetric(browser
)
79 if self
._record
_v
8_object
_stats
:
80 self
._v
8_object
_stats
_metric
= v8_object_stats
.V8ObjectStatsMetric()
82 def WillNavigateToPage(self
, page
, tab
):
84 # For legacy page cyclers which use the filesystem, do an initial
85 # navigate to avoid paying for a cross-renderer navigation.
86 initial_url
= tab
.browser
.http_server
.UrlOf('nonexistent.html')
87 if self
._initial
_renderer
_url
!= initial_url
:
88 self
._initial
_renderer
_url
= initial_url
89 tab
.Navigate(self
._initial
_renderer
_url
)
91 page
.script_to_evaluate_on_commit
= self
._page
_cycler
_js
92 if self
.ShouldRunCold(page
.url
):
93 tab
.ClearCache(force
=True)
94 if self
._report
_speed
_index
:
95 self
._speedindex
_metric
.Start(page
, tab
)
96 self
._cpu
_metric
.Start(page
, tab
)
97 self
._power
_metric
.Start(page
, tab
)
99 def DidNavigateToPage(self
, page
, tab
):
100 self
._memory
_metric
.Start(page
, tab
)
101 if self
._record
_v
8_object
_stats
:
102 self
._v
8_object
_stats
_metric
.Start(page
, tab
)
104 def CustomizeBrowserOptions(self
, options
):
105 memory
.MemoryMetric
.CustomizeBrowserOptions(options
)
106 power
.PowerMetric
.CustomizeBrowserOptions(options
)
107 options
.AppendExtraBrowserArgs('--js-flags=--expose_gc')
109 if self
._record
_v
8_object
_stats
:
110 v8_object_stats
.V8ObjectStatsMetric
.CustomizeBrowserOptions(options
)
111 if self
._report
_speed
_index
:
112 self
._speedindex
_metric
.CustomizeBrowserOptions(options
)
114 def ValidateAndMeasurePage(self
, page
, tab
, results
):
115 tab
.WaitForJavaScriptExpression('__pc_load_time', 60)
117 chart_name_prefix
= ('cold_' if self
.IsRunCold(page
.url
) else
120 results
.AddValue(scalar
.ScalarValue(
121 results
.current_page
, '%stimes.page_load_time' % chart_name_prefix
,
122 'ms', tab
.EvaluateJavaScript('__pc_load_time'),
123 description
='Average page load time. Measured from '
124 'performance.timing.navigationStart until the completion '
125 'time of a layout after the window.load event. Cold times '
126 'are the times when the page is loaded cold, i.e. without '
127 'loading it before, and warm times are times when the '
128 'page is loaded after being loaded previously.'))
130 self
._has
_loaded
_page
[page
.url
] += 1
132 self
._power
_metric
.Stop(page
, tab
)
133 self
._memory
_metric
.Stop(page
, tab
)
134 self
._memory
_metric
.AddResults(tab
, results
)
135 self
._power
_metric
.AddResults(tab
, results
)
137 self
._cpu
_metric
.Stop(page
, tab
)
138 self
._cpu
_metric
.AddResults(tab
, results
)
139 if self
._record
_v
8_object
_stats
:
140 self
._v
8_object
_stats
_metric
.Stop(page
, tab
)
141 self
._v
8_object
_stats
_metric
.AddResults(tab
, results
)
143 if self
._report
_speed
_index
:
144 def SpeedIndexIsFinished():
145 return self
._speedindex
_metric
.IsFinished(tab
)
146 util
.WaitFor(SpeedIndexIsFinished
, 60)
147 self
._speedindex
_metric
.Stop(page
, tab
)
148 self
._speedindex
_metric
.AddResults(
149 tab
, results
, chart_name
=chart_name_prefix
+'speed_index')
151 def IsRunCold(self
, url
):
152 return (self
.ShouldRunCold(url
) or
153 self
._has
_loaded
_page
[url
] == 0)
155 def ShouldRunCold(self
, url
):
156 # We do the warm runs first for two reasons. The first is so we can
157 # preserve any initial profile cache for as long as possible.
158 # The second is that, if we did cold runs first, we'd have a transition
159 # page set during which we wanted the run for each URL to both
160 # contribute to the cold data and warm the catch for the following
161 # warm run, and clearing the cache before the load of the following
162 # URL would eliminate the intended warmup for the previous URL.
163 return (self
._has
_loaded
_page
[url
] >= self
._cold
_run
_start
_index
)