Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / tools / perf / measurements / page_cycler.py
blobe50d5c47f6705ea28eeec0dc09ec9d7b89cbca85
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
15 cycling all pages.
16 """
18 import collections
19 import os
21 from telemetry.core import util
22 from telemetry.page import page_test
23 from telemetry.value import scalar
25 from metrics import cpu
26 from metrics import keychain_metric
27 from metrics import memory
28 from metrics import power
29 from metrics import speedindex
32 class PageCycler(page_test.PageTest):
33 def __init__(self, page_repeat, pageset_repeat, cold_load_percent=50,
34 report_speed_index=False, 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._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 else:
65 self._cold_run_start_index = pageset_repeat * page_repeat
67 def WillStartBrowser(self, platform):
68 """Initialize metrics once right before the browser has been launched."""
69 self._power_metric = power.PowerMetric(platform)
71 def DidStartBrowser(self, browser):
72 """Initialize metrics once right after the browser has been launched."""
73 self._memory_metric = memory.MemoryMetric(browser)
74 self._cpu_metric = cpu.CpuMetric(browser)
76 def WillNavigateToPage(self, page, tab):
77 if page.is_file:
78 # For legacy page cyclers which use the filesystem, do an initial
79 # navigate to avoid paying for a cross-renderer navigation.
80 initial_url = tab.browser.http_server.UrlOf('nonexistent.html')
81 if self._initial_renderer_url != initial_url:
82 self._initial_renderer_url = initial_url
83 tab.Navigate(self._initial_renderer_url)
85 page.script_to_evaluate_on_commit = self._page_cycler_js
86 if self.ShouldRunCold(page.url):
87 tab.ClearCache(force=True)
88 if self._report_speed_index:
89 self._speedindex_metric.Start(page, tab)
90 self._cpu_metric.Start(page, tab)
91 self._power_metric.Start(page, tab)
93 def DidNavigateToPage(self, page, tab):
94 self._memory_metric.Start(page, tab)
96 def CustomizeBrowserOptions(self, options):
97 memory.MemoryMetric.CustomizeBrowserOptions(options)
98 power.PowerMetric.CustomizeBrowserOptions(options)
99 options.AppendExtraBrowserArgs('--js-flags=--expose_gc')
101 if self._report_speed_index:
102 self._speedindex_metric.CustomizeBrowserOptions(options)
104 keychain_metric.KeychainMetric.CustomizeBrowserOptions(options)
106 def ValidateAndMeasurePage(self, page, tab, results):
107 tab.WaitForJavaScriptExpression('__pc_load_time', 60)
109 chart_name_prefix = ('cold_' if self.IsRunCold(page.url) else
110 'warm_')
112 results.AddValue(scalar.ScalarValue(
113 results.current_page, '%stimes.page_load_time' % chart_name_prefix,
114 'ms', tab.EvaluateJavaScript('__pc_load_time'),
115 description='Average page load time. Measured from '
116 'performance.timing.navigationStart until the completion '
117 'time of a layout after the window.load event. Cold times '
118 'are the times when the page is loaded cold, i.e. without '
119 'loading it before, and warm times are times when the '
120 'page is loaded after being loaded previously.'))
122 self._has_loaded_page[page.url] += 1
124 self._power_metric.Stop(page, tab)
125 self._memory_metric.Stop(page, tab)
126 self._memory_metric.AddResults(tab, results)
127 self._power_metric.AddResults(tab, results)
129 self._cpu_metric.Stop(page, tab)
130 self._cpu_metric.AddResults(tab, results)
132 if self._report_speed_index:
133 def SpeedIndexIsFinished():
134 return self._speedindex_metric.IsFinished(tab)
135 util.WaitFor(SpeedIndexIsFinished, 60)
136 self._speedindex_metric.Stop(page, tab)
137 self._speedindex_metric.AddResults(
138 tab, results, chart_name=chart_name_prefix+'speed_index')
139 keychain_metric.KeychainMetric().AddResults(tab, results)
141 def IsRunCold(self, url):
142 return self.ShouldRunCold(url) or self._has_loaded_page[url] == 0
144 def ShouldRunCold(self, url):
145 # We do the warm runs first for two reasons. The first is so we can
146 # preserve any initial profile cache for as long as possible.
147 # The second is that, if we did cold runs first, we'd have a transition
148 # page set during which we wanted the run for each URL to both
149 # contribute to the cold data and warm the catch for the following
150 # warm run, and clearing the cache before the load of the following
151 # URL would eliminate the intended warmup for the previous URL.
152 return self._has_loaded_page[url] >= self._cold_run_start_index