OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 import collections | 4 import collections |
5 import json | 5 import json |
6 import logging | 6 import logging |
7 | 7 |
8 from metrics import Metric | 8 from metrics import Metric |
9 from metrics import histogram_util | 9 from metrics import histogram_util |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 'Startup.BrowserMainEntryTimeAbsoluteHighWord', | 34 'Startup.BrowserMainEntryTimeAbsoluteHighWord', |
35 tab) | 35 tab) |
36 low_bytes = histogram_util.GetHistogramSum( | 36 low_bytes = histogram_util.GetHistogramSum( |
37 histogram_type, | 37 histogram_type, |
38 'Startup.BrowserMainEntryTimeAbsoluteLowWord', | 38 'Startup.BrowserMainEntryTimeAbsoluteLowWord', |
39 tab) | 39 tab) |
40 if high_bytes == 0 and low_bytes == 0: | 40 if high_bytes == 0 and low_bytes == 0: |
41 return None | 41 return None |
42 return (int(high_bytes) << 32) | (int(low_bytes) << 1) | 42 return (int(high_bytes) << 32) | (int(low_bytes) << 1) |
43 | 43 |
44 # pylint: disable=W0101 | |
45 def _RecordTabLoadTimes(self, tab, browser_main_entry_time_ms, results): | 44 def _RecordTabLoadTimes(self, tab, browser_main_entry_time_ms, results): |
46 """Records the tab load times for the browser. """ | 45 """Records the tab load times for the browser. """ |
47 tab_load_times = [] | 46 tab_load_times = [] |
48 TabLoadTime = collections.namedtuple( | 47 TabLoadTime = collections.namedtuple( |
49 'TabLoadTime', | 48 'TabLoadTime', |
50 ['load_start_ms', 'load_duration_ms', 'is_foreground_tab']) | 49 ['load_start_ms', 'load_duration_ms']) |
51 num_open_tabs = len(tab.browser.tabs) | 50 |
52 for i in xrange(num_open_tabs): | 51 def RecordTabLoadTime(t): |
53 try: | 52 try: |
54 t = tab.browser.tabs[i] | |
55 t.WaitForDocumentReadyStateToBeComplete() | 53 t.WaitForDocumentReadyStateToBeComplete() |
56 | 54 |
57 result = t.EvaluateJavaScript( | 55 result = t.EvaluateJavaScript( |
58 'statsCollectionController.tabLoadTiming()') | 56 'statsCollectionController.tabLoadTiming()') |
59 result = json.loads(result) | 57 result = json.loads(result) |
60 | 58 |
61 if 'load_start_ms' not in result or 'load_duration_ms' not in result: | 59 if 'load_start_ms' not in result or 'load_duration_ms' not in result: |
62 raise Exception("Outdated Chrome version, " | 60 raise Exception("Outdated Chrome version, " |
63 "statsCollectionController.tabLoadTiming() not present") | 61 "statsCollectionController.tabLoadTiming() not present") |
64 return | |
65 if result['load_duration_ms'] is None: | 62 if result['load_duration_ms'] is None: |
66 tab_title = t.EvaluateJavaScript('document.title') | 63 tab_title = t.EvaluateJavaScript('document.title') |
67 print "Page: ", tab_title, " didn't finish loading." | 64 print "Page: ", tab_title, " didn't finish loading." |
68 continue | 65 return |
69 | 66 |
70 is_foreground_tab = t.EvaluateJavaScript('!document.hidden') | |
71 tab_load_times.append(TabLoadTime( | 67 tab_load_times.append(TabLoadTime( |
72 int(result['load_start_ms']), | 68 int(result['load_start_ms']), |
73 int(result['load_duration_ms']), | 69 int(result['load_duration_ms']))) |
74 is_foreground_tab)) | |
75 except util.TimeoutException: | 70 except util.TimeoutException: |
76 # Low memory Android devices may not be able to load more than | 71 # Low memory Android devices may not be able to load more than |
77 # one tab at a time, so may timeout when the test attempts to | 72 # one tab at a time, so may timeout when the test attempts to |
78 # access a background tab. Ignore these tabs. | 73 # access a background tab. Ignore these tabs. |
79 logging.error("Tab number: %d timed out on JavaScript access" % i) | 74 logging.error("Tab timed out on JavaScript access") |
80 continue | |
81 | 75 |
82 # Postprocess results | 76 # Only measure the foreground tab. We can't measure all tabs on Android |
83 load_complete_times = ( | 77 # because on Android the data of the background tabs is loaded on demand, |
84 [t.load_start_ms + t.load_duration_ms for t in tab_load_times]) | 78 # when the user switches to them, rather than during startup. In view of |
85 load_complete_times.sort() | 79 # this, to get the same measures on all platform, we only measure the |
| 80 # foreground tab on all platforms. |
86 | 81 |
87 if 'android' in tab.browser.browser_type: | 82 RecordTabLoadTime(tab.browser.foreground_tab) |
88 # document.hidden is broken on Android - crbug.com/322544. | |
89 foreground_tab_stats = [tab_load_times[0]] | |
90 else: | |
91 foreground_tab_stats = ( | |
92 [t for t in tab_load_times if t.is_foreground_tab == True]) | |
93 if (len(foreground_tab_stats) != 1): | |
94 raise Exception ("More than one foreground tab? ", foreground_tab_stats) | |
95 foreground_tab_stats = foreground_tab_stats[0] | |
96 | 83 |
97 # Report load stats. | 84 foreground_tab_stats = tab_load_times[0] |
98 foreground_tab_load_complete = ((foreground_tab_stats.load_start_ms + | 85 foreground_tab_load_complete = ((foreground_tab_stats.load_start_ms + |
99 foreground_tab_stats.load_duration_ms) - browser_main_entry_time_ms) | 86 foreground_tab_stats.load_duration_ms) - browser_main_entry_time_ms) |
100 | |
101 results.Add( | 87 results.Add( |
102 'foreground_tab_load_complete', 'ms', foreground_tab_load_complete) | 88 'foreground_tab_load_complete', 'ms', foreground_tab_load_complete) |
103 | 89 |
104 if num_open_tabs > 1: | |
105 last_tab_load_complete = ( | |
106 load_complete_times[-1] - browser_main_entry_time_ms) | |
107 results.Add('last_tab_load_complete', 'ms', last_tab_load_complete) | |
108 | |
109 def AddResults(self, tab, results): | 90 def AddResults(self, tab, results): |
110 get_histogram_js = 'statsCollectionController.getBrowserHistogram("%s")' | 91 get_histogram_js = 'statsCollectionController.getBrowserHistogram("%s")' |
111 | 92 |
112 for display_name, histogram_name in self.HISTOGRAMS_TO_RECORD.iteritems(): | 93 for display_name, histogram_name in self.HISTOGRAMS_TO_RECORD.iteritems(): |
113 result = tab.EvaluateJavaScript(get_histogram_js % histogram_name) | 94 result = tab.EvaluateJavaScript(get_histogram_js % histogram_name) |
114 result = json.loads(result) | 95 result = json.loads(result) |
115 measured_time = 0 | 96 measured_time = 0 |
116 | 97 |
117 if 'sum' in result: | 98 if 'sum' in result: |
118 # For all the histograms logged here, there's a single entry so sum | 99 # For all the histograms logged here, there's a single entry so sum |
119 # is the exact value for that entry. | 100 # is the exact value for that entry. |
120 measured_time = result['sum'] | 101 measured_time = result['sum'] |
121 elif 'buckets' in result: | 102 elif 'buckets' in result: |
122 measured_time = \ | 103 measured_time = \ |
123 (result['buckets'][0]['high'] + result['buckets'][0]['low']) / 2 | 104 (result['buckets'][0]['high'] + result['buckets'][0]['low']) / 2 |
124 | 105 |
125 results.Add(display_name, 'ms', measured_time) | 106 results.Add(display_name, 'ms', measured_time) |
126 | 107 |
127 # Get tab load times. | 108 # Get tab load times. |
128 browser_main_entry_time_ms = self._GetBrowserMainEntryTime(tab) | 109 browser_main_entry_time_ms = self._GetBrowserMainEntryTime(tab) |
129 if (browser_main_entry_time_ms is None): | 110 if (browser_main_entry_time_ms is None): |
130 print "Outdated Chrome version, browser main entry time not supported." | 111 print "Outdated Chrome version, browser main entry time not supported." |
131 return | 112 return |
132 self._RecordTabLoadTimes(tab, browser_main_entry_time_ms, results) | 113 self._RecordTabLoadTimes(tab, browser_main_entry_time_ms, results) |
OLD | NEW |