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 keychain_metric
23 from metrics
import memory
24 from metrics
import power
25 from metrics
import speedindex
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 report_speed_index
=False, clear_cache_before_each_run
=False):
34 super(PageCycler
, self
).__init
__(
35 action_name_to_run
='RunPageInteractions',
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
._report
_speed
_index
= report_speed_index
43 self
._speedindex
_metric
= speedindex
.SpeedIndexMetric()
44 self
._memory
_metric
= None
45 self
._power
_metric
= None
46 self
._cpu
_metric
= None
47 self
._has
_loaded
_page
= collections
.defaultdict(int)
48 self
._initial
_renderer
_url
= None # to avoid cross-renderer navigation
50 cold_runs_percent_set
= (cold_load_percent
!= None)
51 # Handle requests for cold cache runs
52 if (cold_runs_percent_set
and
53 (cold_load_percent
< 0 or cold_load_percent
> 100)):
54 raise Exception('cold-load-percent must be in the range [0-100]')
56 # Make sure _cold_run_start_index is an integer multiple of page_repeat.
57 # Without this, --pageset_shuffle + --page_repeat could lead to
58 # assertion failures on _started_warm in WillNavigateToPage.
59 if cold_runs_percent_set
:
60 number_warm_pageset_runs
= int(
61 (int(pageset_repeat
) - 1) * (100 - cold_load_percent
) / 100)
62 number_warm_runs
= number_warm_pageset_runs
* page_repeat
63 self
._cold
_run
_start
_index
= number_warm_runs
+ page_repeat
64 self
._discard
_first
_result
= (not cold_load_percent
or
65 self
._discard
_first
_result
)
67 self
._cold
_run
_start
_index
= pageset_repeat
* page_repeat
69 def WillStartBrowser(self
, platform
):
70 """Initialize metrics once right before the browser has been launched."""
71 self
._power
_metric
= power
.PowerMetric(platform
)
73 def DidStartBrowser(self
, browser
):
74 """Initialize metrics once right after the browser has been launched."""
75 self
._memory
_metric
= memory
.MemoryMetric(browser
)
76 self
._cpu
_metric
= cpu
.CpuMetric(browser
)
78 def WillNavigateToPage(self
, page
, tab
):
80 # For legacy page cyclers which use the filesystem, do an initial
81 # navigate to avoid paying for a cross-renderer navigation.
82 initial_url
= tab
.browser
.http_server
.UrlOf('nonexistent.html')
83 if self
._initial
_renderer
_url
!= initial_url
:
84 self
._initial
_renderer
_url
= initial_url
85 tab
.Navigate(self
._initial
_renderer
_url
)
87 page
.script_to_evaluate_on_commit
= self
._page
_cycler
_js
88 if self
.ShouldRunCold(page
.url
):
89 tab
.ClearCache(force
=True)
90 if self
._report
_speed
_index
:
91 self
._speedindex
_metric
.Start(page
, tab
)
92 self
._cpu
_metric
.Start(page
, tab
)
93 self
._power
_metric
.Start(page
, tab
)
95 def DidNavigateToPage(self
, page
, tab
):
96 self
._memory
_metric
.Start(page
, tab
)
98 def CustomizeBrowserOptions(self
, options
):
99 memory
.MemoryMetric
.CustomizeBrowserOptions(options
)
100 power
.PowerMetric
.CustomizeBrowserOptions(options
)
101 options
.AppendExtraBrowserArgs('--js-flags=--expose_gc')
103 if self
._report
_speed
_index
:
104 self
._speedindex
_metric
.CustomizeBrowserOptions(options
)
106 keychain_metric
.KeychainMetric
.CustomizeBrowserOptions(options
)
108 def ValidateAndMeasurePage(self
, page
, tab
, results
):
109 tab
.WaitForJavaScriptExpression('__pc_load_time', 60)
111 chart_name_prefix
= ('cold_' if self
.IsRunCold(page
.url
) else
114 results
.AddValue(scalar
.ScalarValue(
115 results
.current_page
, '%stimes.page_load_time' % chart_name_prefix
,
116 'ms', tab
.EvaluateJavaScript('__pc_load_time'),
117 description
='Average page load time. Measured from '
118 'performance.timing.navigationStart until the completion '
119 'time of a layout after the window.load event. Cold times '
120 'are the times when the page is loaded cold, i.e. without '
121 'loading it before, and warm times are times when the '
122 'page is loaded after being loaded previously.'))
124 self
._has
_loaded
_page
[page
.url
] += 1
126 self
._power
_metric
.Stop(page
, tab
)
127 self
._memory
_metric
.Stop(page
, tab
)
128 self
._memory
_metric
.AddResults(tab
, results
)
129 self
._power
_metric
.AddResults(tab
, results
)
131 self
._cpu
_metric
.Stop(page
, tab
)
132 self
._cpu
_metric
.AddResults(tab
, results
)
134 if self
._report
_speed
_index
:
135 def SpeedIndexIsFinished():
136 return self
._speedindex
_metric
.IsFinished(tab
)
137 util
.WaitFor(SpeedIndexIsFinished
, 60)
138 self
._speedindex
_metric
.Stop(page
, tab
)
139 self
._speedindex
_metric
.AddResults(
140 tab
, results
, chart_name
=chart_name_prefix
+'speed_index')
141 keychain_metric
.KeychainMetric().AddResults(tab
, results
)
143 def IsRunCold(self
, url
):
144 return (self
.ShouldRunCold(url
) or
145 self
._has
_loaded
_page
[url
] == 0)
147 def ShouldRunCold(self
, url
):
148 # We do the warm runs first for two reasons. The first is so we can
149 # preserve any initial profile cache for as long as possible.
150 # The second is that, if we did cold runs first, we'd have a transition
151 # page set during which we wanted the run for each URL to both
152 # contribute to the cold data and warm the catch for the following
153 # warm run, and clearing the cache before the load of the following
154 # URL would eliminate the intended warmup for the previous URL.
155 return (self
._has
_loaded
_page
[url
] >= self
._cold
_run
_start
_index
)