Chromium Code Reviews| 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 | 44 # pylint: disable=W0101 |
| 45 def _RecordTabLoadTimes(self, tab, browser_main_entry_time_ms, results): | 45 def _RecordTabLoadTimes(self, tab, browser_main_entry_time_ms, results): |
| 46 """Records the tab load times for the browser. """ | 46 """Records the tab load times for the browser. """ |
| 47 tab_load_times = [] | 47 tab_load_times = [] |
| 48 TabLoadTime = collections.namedtuple( | 48 TabLoadTime = collections.namedtuple( |
| 49 'TabLoadTime', | 49 'TabLoadTime', |
| 50 ['load_start_ms', 'load_duration_ms', 'is_foreground_tab']) | 50 ['load_start_ms', 'load_duration_ms', 'is_foreground_tab']) |
| 51 num_open_tabs = len(tab.browser.tabs) | 51 |
| 52 for i in xrange(num_open_tabs): | 52 def RecordTabLoadTime(t): |
| 53 try: | 53 try: |
| 54 t = tab.browser.tabs[i] | |
| 55 t.WaitForDocumentReadyStateToBeComplete() | 54 t.WaitForDocumentReadyStateToBeComplete() |
| 56 | 55 |
| 57 result = t.EvaluateJavaScript( | 56 result = t.EvaluateJavaScript( |
| 58 'statsCollectionController.tabLoadTiming()') | 57 'statsCollectionController.tabLoadTiming()') |
| 59 result = json.loads(result) | 58 result = json.loads(result) |
| 60 | 59 |
| 61 if 'load_start_ms' not in result or 'load_duration_ms' not in result: | 60 if 'load_start_ms' not in result or 'load_duration_ms' not in result: |
| 62 raise Exception("Outdated Chrome version, " | 61 raise Exception("Outdated Chrome version, " |
| 63 "statsCollectionController.tabLoadTiming() not present") | 62 "statsCollectionController.tabLoadTiming() not present") |
| 64 return | |
| 65 if result['load_duration_ms'] is None: | 63 if result['load_duration_ms'] is None: |
| 66 tab_title = t.EvaluateJavaScript('document.title') | 64 tab_title = t.EvaluateJavaScript('document.title') |
| 67 print "Page: ", tab_title, " didn't finish loading." | 65 print "Page: ", tab_title, " didn't finish loading." |
| 68 continue | 66 return |
| 69 | 67 |
| 70 is_foreground_tab = t.EvaluateJavaScript('!document.hidden') | 68 is_foreground_tab = t.EvaluateJavaScript('!document.hidden') |
| 71 tab_load_times.append(TabLoadTime( | 69 tab_load_times.append(TabLoadTime( |
| 72 int(result['load_start_ms']), | 70 int(result['load_start_ms']), |
| 73 int(result['load_duration_ms']), | 71 int(result['load_duration_ms']), |
| 74 is_foreground_tab)) | 72 is_foreground_tab)) |
| 75 except util.TimeoutException: | 73 except util.TimeoutException: |
| 76 # Low memory Android devices may not be able to load more than | 74 # 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 | 75 # one tab at a time, so may timeout when the test attempts to |
| 78 # access a background tab. Ignore these tabs. | 76 # access a background tab. Ignore these tabs. |
| 79 logging.error("Tab number: %d timed out on JavaScript access" % i) | 77 logging.error("Tab number: %d timed out on JavaScript access" % 0) |
| 80 continue | |
| 81 | |
| 82 # Postprocess results | |
| 83 load_complete_times = ( | |
| 84 [t.load_start_ms + t.load_duration_ms for t in tab_load_times]) | |
| 85 load_complete_times.sort() | |
| 86 | 78 |
| 87 if 'android' in tab.browser.browser_type: | 79 if 'android' in tab.browser.browser_type: |
| 88 # document.hidden is broken on Android - crbug.com/322544. | 80 # On android the background tabs are loaded on demand, so wont have been |
| 89 foreground_tab_stats = [tab_load_times[0]] | 81 # loaded at this stage; only record the foreground tab. Also |
| 82 # document.hidden is broken on Android - crbug.com/322544 so assume this | |
| 83 # is tab[0] | |
|
jeremy
2014/02/13 09:40:15
Can you stick in a comment explaining in detail th
| |
| 84 RecordTabLoadTime(tab.browser.tabs[0]) | |
| 85 | |
| 86 foreground_tab_stats = tab_load_times[0] | |
| 87 foreground_tab_load_complete = ((foreground_tab_stats.load_start_ms + | |
| 88 foreground_tab_stats.load_duration_ms) - browser_main_entry_time_ms) | |
| 89 results.Add( | |
| 90 'foreground_tab_load_complete', 'ms', foreground_tab_load_complete) | |
| 90 else: | 91 else: |
| 92 num_open_tabs = len(tab.browser.tabs) | |
| 93 for i in xrange(num_open_tabs): | |
| 94 RecordTabLoadTime(tab.browser.tabs[i]) | |
| 95 # Postprocess results | |
| 96 load_complete_times = ( | |
| 97 [t.load_start_ms + t.load_duration_ms for t in tab_load_times]) | |
| 98 load_complete_times.sort() | |
| 99 | |
| 91 foreground_tab_stats = ( | 100 foreground_tab_stats = ( |
| 92 [t for t in tab_load_times if t.is_foreground_tab == True]) | 101 [t for t in tab_load_times if t.is_foreground_tab == True]) |
| 93 if (len(foreground_tab_stats) != 1): | 102 if (len(foreground_tab_stats) != 1): |
| 94 raise Exception ("More than one foreground tab? ", foreground_tab_stats) | 103 raise Exception ("More than one foreground tab? ", foreground_tab_stats) |
| 95 foreground_tab_stats = foreground_tab_stats[0] | 104 foreground_tab_stats = foreground_tab_stats[0] |
| 96 | 105 |
| 97 # Report load stats. | 106 # Report load stats. |
| 98 foreground_tab_load_complete = ((foreground_tab_stats.load_start_ms + | 107 foreground_tab_load_complete = ((foreground_tab_stats.load_start_ms + |
| 99 foreground_tab_stats.load_duration_ms) - browser_main_entry_time_ms) | 108 foreground_tab_stats.load_duration_ms) - browser_main_entry_time_ms) |
| 100 | 109 |
| 101 results.Add( | 110 results.Add( |
| 102 'foreground_tab_load_complete', 'ms', foreground_tab_load_complete) | 111 'foreground_tab_load_complete', 'ms', foreground_tab_load_complete) |
| 103 | 112 |
| 104 if num_open_tabs > 1: | 113 if num_open_tabs > 1: |
| 105 last_tab_load_complete = ( | 114 last_tab_load_complete = ( |
| 106 load_complete_times[-1] - browser_main_entry_time_ms) | 115 load_complete_times[-1] - browser_main_entry_time_ms) |
| 107 results.Add('last_tab_load_complete', 'ms', last_tab_load_complete) | 116 results.Add('last_tab_load_complete', 'ms', last_tab_load_complete) |
| 108 | 117 |
| 109 def AddResults(self, tab, results): | 118 def AddResults(self, tab, results): |
| 110 get_histogram_js = 'statsCollectionController.getBrowserHistogram("%s")' | 119 get_histogram_js = 'statsCollectionController.getBrowserHistogram("%s")' |
| 111 | 120 |
| 112 for display_name, histogram_name in self.HISTOGRAMS_TO_RECORD.iteritems(): | 121 for display_name, histogram_name in self.HISTOGRAMS_TO_RECORD.iteritems(): |
| 113 result = tab.EvaluateJavaScript(get_histogram_js % histogram_name) | 122 result = tab.EvaluateJavaScript(get_histogram_js % histogram_name) |
| 114 result = json.loads(result) | 123 result = json.loads(result) |
| 115 measured_time = 0 | 124 measured_time = 0 |
| 116 | 125 |
| 117 if 'sum' in result: | 126 if 'sum' in result: |
| 118 # For all the histograms logged here, there's a single entry so sum | 127 # For all the histograms logged here, there's a single entry so sum |
| 119 # is the exact value for that entry. | 128 # is the exact value for that entry. |
| 120 measured_time = result['sum'] | 129 measured_time = result['sum'] |
| 121 elif 'buckets' in result: | 130 elif 'buckets' in result: |
| 122 measured_time = \ | 131 measured_time = \ |
| 123 (result['buckets'][0]['high'] + result['buckets'][0]['low']) / 2 | 132 (result['buckets'][0]['high'] + result['buckets'][0]['low']) / 2 |
| 124 | 133 |
| 125 results.Add(display_name, 'ms', measured_time) | 134 results.Add(display_name, 'ms', measured_time) |
| 126 | 135 |
| 127 # Get tab load times. | 136 # Get tab load times. |
| 128 browser_main_entry_time_ms = self._GetBrowserMainEntryTime(tab) | 137 browser_main_entry_time_ms = self._GetBrowserMainEntryTime(tab) |
| 129 if (browser_main_entry_time_ms is None): | 138 if (browser_main_entry_time_ms is None): |
| 130 print "Outdated Chrome version, browser main entry time not supported." | 139 print "Outdated Chrome version, browser main entry time not supported." |
| 131 return | 140 return |
| 132 self._RecordTabLoadTimes(tab, browser_main_entry_time_ms, results) | 141 self._RecordTabLoadTimes(tab, browser_main_entry_time_ms, results) |
| OLD | NEW |