| 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 telemetry.core import exceptions | 8 from telemetry.core import exceptions |
| 9 from telemetry.value import histogram_util | 9 from telemetry.value import histogram_util |
| 10 from telemetry.value import scalar | 10 from telemetry.value import scalar |
| 11 | 11 |
| 12 from metrics import Metric | 12 from metrics import Metric |
| 13 | 13 |
| 14 | 14 |
| 15 class StartupMetric(Metric): | 15 class StartupMetric(Metric): |
| 16 "A metric for browser startup time." | 16 "A metric for browser startup time." |
| 17 | 17 |
| 18 # Seconds to wait for page loading complete. | |
| 19 DEFAULT_LOADING_TIMEOUT = 90 | |
| 20 | |
| 21 HISTOGRAMS_TO_RECORD = { | 18 HISTOGRAMS_TO_RECORD = { |
| 22 'messageloop_start_time' : | 19 'messageloop_start_time' : |
| 23 'Startup.BrowserMessageLoopStartTimeFromMainEntry', | 20 'Startup.BrowserMessageLoopStartTimeFromMainEntry', |
| 24 'window_display_time' : 'Startup.BrowserWindowDisplay', | 21 'window_display_time' : 'Startup.BrowserWindowDisplay', |
| 25 'open_tabs_time' : 'Startup.BrowserOpenTabs'} | 22 'open_tabs_time' : 'Startup.BrowserOpenTabs'} |
| 26 | 23 |
| 27 def Start(self, page, tab): | 24 def Start(self, page, tab): |
| 28 raise NotImplementedError() | 25 raise NotImplementedError() |
| 29 | 26 |
| 30 def Stop(self, page, tab): | 27 def Stop(self, page, tab): |
| 31 raise NotImplementedError() | 28 raise NotImplementedError() |
| 32 | 29 |
| 33 def _GetBrowserMainEntryTime(self, tab): | 30 def _GetBrowserMainEntryTime(self, tab): |
| 34 """Returns the main entry time (in ms) of the browser.""" | 31 """Returns the main entry time (in ms) of the browser.""" |
| 35 histogram_type = histogram_util.BROWSER_HISTOGRAM | 32 histogram_type = histogram_util.BROWSER_HISTOGRAM |
| 36 high_bytes = histogram_util.GetHistogramSum( | 33 high_bytes = histogram_util.GetHistogramSum( |
| 37 histogram_type, | 34 histogram_type, |
| 38 'Startup.BrowserMainEntryTimeAbsoluteHighWord', | 35 'Startup.BrowserMainEntryTimeAbsoluteHighWord', |
| 39 tab) | 36 tab) |
| 40 low_bytes = histogram_util.GetHistogramSum( | 37 low_bytes = histogram_util.GetHistogramSum( |
| 41 histogram_type, | 38 histogram_type, |
| 42 'Startup.BrowserMainEntryTimeAbsoluteLowWord', | 39 'Startup.BrowserMainEntryTimeAbsoluteLowWord', |
| 43 tab) | 40 tab) |
| 44 if high_bytes == 0 and low_bytes == 0: | 41 if high_bytes == 0 and low_bytes == 0: |
| 45 return None | 42 return None |
| 46 return (int(high_bytes) << 32) | (int(low_bytes) << 1) | 43 return (int(high_bytes) << 32) | (int(low_bytes) << 1) |
| 47 | 44 |
| 48 def _RecordTabLoadTimes(self, tab, browser_main_entry_time_ms, results): | 45 def _RecordTabLoadTimes(self, tab, browser_main_entry_time_ms, results): |
| 49 """Records the tab load times for the browser. """ | 46 """Records the tab load times for the browser. """ |
| 47 tab_load_times = [] |
| 50 TabLoadTime = collections.namedtuple( | 48 TabLoadTime = collections.namedtuple( |
| 51 'TabLoadTime', | 49 'TabLoadTime', |
| 52 ['request_start_ms', 'load_end_ms']) | 50 ['load_start_ms', 'load_duration_ms', 'request_start_ms']) |
| 53 | 51 |
| 54 def RecordOneTab(t): | 52 def RecordTabLoadTime(t): |
| 55 def EvaluateInt(exp): | 53 try: |
| 56 val = t.EvaluateJavaScript(exp) | 54 t.WaitForDocumentReadyStateToBeComplete() |
| 57 if not val: | |
| 58 logging.warn('%s undefined' % exp) | |
| 59 return 0 | |
| 60 return int(val) | |
| 61 | 55 |
| 62 try: | 56 result = t.EvaluateJavaScript( |
| 63 t.WaitForJavaScriptExpression( | 57 'statsCollectionController.tabLoadTiming()') |
| 64 'window.performance.timing["loadEventEnd"] > 0', | 58 result = json.loads(result) |
| 65 self.DEFAULT_LOADING_TIMEOUT) | |
| 66 | 59 |
| 67 # EvaluateJavaScript(window.performance.timing) doesn't guarantee to | 60 if 'load_start_ms' not in result or 'load_duration_ms' not in result: |
| 68 # return the desired javascript object (crbug/472603). It may return an | 61 raise Exception("Outdated Chrome version, " |
| 69 # empty object. However getting individual field works. | 62 "statsCollectionController.tabLoadTiming() not present") |
| 70 # The behavior depends on Webkit implementation on different platforms. | 63 if result['load_duration_ms'] is None: |
| 71 load_event_end = EvaluateInt( | 64 tab_title = t.EvaluateJavaScript('document.title') |
| 72 'window.performance.timing["loadEventEnd"]') | 65 print "Page: ", tab_title, " didn't finish loading." |
| 73 request_start = EvaluateInt( | 66 return |
| 74 'window.performance.timing["requestStart"]') | |
| 75 | 67 |
| 76 return TabLoadTime(request_start, load_event_end) | 68 perf_timing = t.EvaluateJavaScript('window.performance.timing') |
| 69 if 'requestStart' not in perf_timing: |
| 70 perf_timing['requestStart'] = 0 # Exclude from benchmark results |
| 71 print 'requestStart is not supported by this browser' |
| 77 | 72 |
| 73 tab_load_times.append(TabLoadTime( |
| 74 int(result['load_start_ms']), |
| 75 int(result['load_duration_ms']), |
| 76 int(perf_timing['requestStart']))) |
| 78 except exceptions.TimeoutException: | 77 except exceptions.TimeoutException: |
| 79 # Low memory Android devices may not be able to load more than | 78 # Low memory Android devices may not be able to load more than |
| 80 # one tab at a time, so may timeout when the test attempts to | 79 # one tab at a time, so may timeout when the test attempts to |
| 81 # access a background tab. Ignore these tabs. | 80 # access a background tab. Ignore these tabs. |
| 82 logging.error("Tab timed out on JavaScript access") | 81 logging.error("Tab timed out on JavaScript access") |
| 83 | 82 |
| 84 # Only measure the foreground tab. We can't measure all tabs on Android | 83 # Only measure the foreground tab. We can't measure all tabs on Android |
| 85 # because on Android the data of the background tabs is loaded on demand, | 84 # because on Android the data of the background tabs is loaded on demand, |
| 86 # when the user switches to them, rather than during startup. In view of | 85 # when the user switches to them, rather than during startup. In view of |
| 87 # this, to get the same measures on all platform, we only measure the | 86 # this, to get the same measures on all platform, we only measure the |
| 88 # foreground tab on all platforms. | 87 # foreground tab on all platforms. |
| 89 foreground_tab_stats = RecordOneTab(tab.browser.foreground_tab) | |
| 90 | 88 |
| 91 if foreground_tab_stats: | 89 RecordTabLoadTime(tab.browser.foreground_tab) |
| 92 foreground_tab_load_complete = ( | 90 |
| 93 foreground_tab_stats.load_end_ms - browser_main_entry_time_ms) | 91 foreground_tab_stats = tab_load_times[0] |
| 92 foreground_tab_load_complete = ((foreground_tab_stats.load_start_ms + |
| 93 foreground_tab_stats.load_duration_ms) - browser_main_entry_time_ms) |
| 94 results.AddValue(scalar.ScalarValue( |
| 95 results.current_page, 'foreground_tab_load_complete', 'ms', |
| 96 foreground_tab_load_complete)) |
| 97 if (foreground_tab_stats.request_start_ms > 0): |
| 94 results.AddValue(scalar.ScalarValue( | 98 results.AddValue(scalar.ScalarValue( |
| 95 results.current_page, 'foreground_tab_load_complete', 'ms', | 99 results.current_page, 'foreground_tab_request_start', 'ms', |
| 96 foreground_tab_load_complete)) | 100 foreground_tab_stats.request_start_ms - browser_main_entry_time_ms)) |
| 97 if (foreground_tab_stats.request_start_ms > 0): | |
| 98 results.AddValue(scalar.ScalarValue( | |
| 99 results.current_page, 'foreground_tab_request_start', 'ms', | |
| 100 foreground_tab_stats.request_start_ms - browser_main_entry_time_ms)) | |
| 101 | 101 |
| 102 def AddResults(self, tab, results): | 102 def AddResults(self, tab, results): |
| 103 get_histogram_js = 'statsCollectionController.getBrowserHistogram("%s")' | 103 get_histogram_js = 'statsCollectionController.getBrowserHistogram("%s")' |
| 104 | 104 |
| 105 for display_name, histogram_name in self.HISTOGRAMS_TO_RECORD.iteritems(): | 105 for display_name, histogram_name in self.HISTOGRAMS_TO_RECORD.iteritems(): |
| 106 result = tab.EvaluateJavaScript(get_histogram_js % histogram_name) | 106 result = tab.EvaluateJavaScript(get_histogram_js % histogram_name) |
| 107 result = json.loads(result) | 107 result = json.loads(result) |
| 108 measured_time = 0 | 108 measured_time = 0 |
| 109 | 109 |
| 110 if 'sum' in result: | 110 if 'sum' in result: |
| 111 # For all the histograms logged here, there's a single entry so sum | 111 # For all the histograms logged here, there's a single entry so sum |
| 112 # is the exact value for that entry. | 112 # is the exact value for that entry. |
| 113 measured_time = result['sum'] | 113 measured_time = result['sum'] |
| 114 elif 'buckets' in result: | 114 elif 'buckets' in result: |
| 115 measured_time = \ | 115 measured_time = \ |
| 116 (result['buckets'][0]['high'] + result['buckets'][0]['low']) / 2 | 116 (result['buckets'][0]['high'] + result['buckets'][0]['low']) / 2 |
| 117 | 117 |
| 118 results.AddValue(scalar.ScalarValue( | 118 results.AddValue(scalar.ScalarValue( |
| 119 results.current_page, display_name, 'ms', measured_time)) | 119 results.current_page, display_name, 'ms', measured_time)) |
| 120 | 120 |
| 121 # Get tab load times. | 121 # Get tab load times. |
| 122 browser_main_entry_time_ms = self._GetBrowserMainEntryTime(tab) | 122 browser_main_entry_time_ms = self._GetBrowserMainEntryTime(tab) |
| 123 if (browser_main_entry_time_ms is None): | 123 if (browser_main_entry_time_ms is None): |
| 124 print "Outdated Chrome version, browser main entry time not supported." | 124 print "Outdated Chrome version, browser main entry time not supported." |
| 125 return | 125 return |
| 126 self._RecordTabLoadTimes(tab, browser_main_entry_time_ms, results) | 126 self._RecordTabLoadTimes(tab, browser_main_entry_time_ms, results) |
| OLD | NEW |