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 | |
cylee1
2015/04/07 09:37:50
Default timeout in the origianl code
t.WaitForDo
| |
20 | |
18 HISTOGRAMS_TO_RECORD = { | 21 HISTOGRAMS_TO_RECORD = { |
19 'messageloop_start_time' : | 22 'messageloop_start_time' : |
20 'Startup.BrowserMessageLoopStartTimeFromMainEntry', | 23 'Startup.BrowserMessageLoopStartTimeFromMainEntry', |
21 'window_display_time' : 'Startup.BrowserWindowDisplay', | 24 'window_display_time' : 'Startup.BrowserWindowDisplay', |
22 'open_tabs_time' : 'Startup.BrowserOpenTabs'} | 25 'open_tabs_time' : 'Startup.BrowserOpenTabs'} |
23 | 26 |
24 def Start(self, page, tab): | 27 def Start(self, page, tab): |
25 raise NotImplementedError() | 28 raise NotImplementedError() |
26 | 29 |
27 def Stop(self, page, tab): | 30 def Stop(self, page, tab): |
28 raise NotImplementedError() | 31 raise NotImplementedError() |
29 | 32 |
30 def _GetBrowserMainEntryTime(self, tab): | 33 def _GetBrowserMainEntryTime(self, tab): |
31 """Returns the main entry time (in ms) of the browser.""" | 34 """Returns the main entry time (in ms) of the browser.""" |
32 histogram_type = histogram_util.BROWSER_HISTOGRAM | 35 histogram_type = histogram_util.BROWSER_HISTOGRAM |
33 high_bytes = histogram_util.GetHistogramSum( | 36 high_bytes = histogram_util.GetHistogramSum( |
34 histogram_type, | 37 histogram_type, |
35 'Startup.BrowserMainEntryTimeAbsoluteHighWord', | 38 'Startup.BrowserMainEntryTimeAbsoluteHighWord', |
36 tab) | 39 tab) |
37 low_bytes = histogram_util.GetHistogramSum( | 40 low_bytes = histogram_util.GetHistogramSum( |
38 histogram_type, | 41 histogram_type, |
39 'Startup.BrowserMainEntryTimeAbsoluteLowWord', | 42 'Startup.BrowserMainEntryTimeAbsoluteLowWord', |
40 tab) | 43 tab) |
41 if high_bytes == 0 and low_bytes == 0: | 44 if high_bytes == 0 and low_bytes == 0: |
42 return None | 45 return None |
43 return (int(high_bytes) << 32) | (int(low_bytes) << 1) | 46 return (int(high_bytes) << 32) | (int(low_bytes) << 1) |
44 | 47 |
45 def _RecordTabLoadTimes(self, tab, browser_main_entry_time_ms, results): | 48 def _RecordTabLoadTimes(self, tab, browser_main_entry_time_ms, results): |
46 """Records the tab load times for the browser. """ | 49 """Records the tab load times for the browser. """ |
47 tab_load_times = [] | |
48 TabLoadTime = collections.namedtuple( | 50 TabLoadTime = collections.namedtuple( |
49 'TabLoadTime', | 51 'TabLoadTime', |
50 ['load_start_ms', 'load_duration_ms', 'request_start_ms']) | 52 ['request_start_ms', 'load_end_ms']) |
51 | 53 |
52 def RecordTabLoadTime(t): | 54 def RecordOneTab(t): |
55 def EvaluateInt(exp): | |
56 val = t.EvaluateJavaScript(exp) | |
57 if not val: | |
58 logging.warn('%s undefined' % exp) | |
59 return 0 | |
60 return int(val) | |
61 | |
53 try: | 62 try: |
54 t.WaitForDocumentReadyStateToBeComplete() | 63 t.WaitForJavaScriptExpression( |
64 'window.performance.timing["loadEventEnd"] > 0', | |
65 self.DEFAULT_LOADING_TIMEOUT) | |
55 | 66 |
56 result = t.EvaluateJavaScript( | 67 # EvaluateJavaScript(window.performance.timing) doesn't guarantee to |
57 'statsCollectionController.tabLoadTiming()') | 68 # return the desired javascript object (crbug/472603). It may return an |
58 result = json.loads(result) | 69 # empty object. However getting individual field works. |
70 # The behavior depends on Webkit implementation on different platforms. | |
71 load_event_end = EvaluateInt( | |
72 'window.performance.timing["loadEventEnd"]') | |
73 request_start = EvaluateInt( | |
74 'window.performance.timing["requestStart"]') | |
59 | 75 |
60 if 'load_start_ms' not in result or 'load_duration_ms' not in result: | 76 return TabLoadTime(request_start, load_event_end) |
61 raise Exception("Outdated Chrome version, " | |
62 "statsCollectionController.tabLoadTiming() not present") | |
63 if result['load_duration_ms'] is None: | |
64 tab_title = t.EvaluateJavaScript('document.title') | |
65 print "Page: ", tab_title, " didn't finish loading." | |
66 return | |
67 | 77 |
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' | |
72 | |
73 tab_load_times.append(TabLoadTime( | |
74 int(result['load_start_ms']), | |
75 int(result['load_duration_ms']), | |
76 int(perf_timing['requestStart']))) | |
77 except exceptions.TimeoutException: | 78 except exceptions.TimeoutException: |
78 # Low memory Android devices may not be able to load more than | 79 # Low memory Android devices may not be able to load more than |
79 # one tab at a time, so may timeout when the test attempts to | 80 # one tab at a time, so may timeout when the test attempts to |
80 # access a background tab. Ignore these tabs. | 81 # access a background tab. Ignore these tabs. |
81 logging.error("Tab timed out on JavaScript access") | 82 logging.error("Tab timed out on JavaScript access") |
82 | 83 |
83 # Only measure the foreground tab. We can't measure all tabs on Android | 84 # Only measure the foreground tab. We can't measure all tabs on Android |
84 # because on Android the data of the background tabs is loaded on demand, | 85 # because on Android the data of the background tabs is loaded on demand, |
85 # when the user switches to them, rather than during startup. In view of | 86 # when the user switches to them, rather than during startup. In view of |
86 # this, to get the same measures on all platform, we only measure the | 87 # this, to get the same measures on all platform, we only measure the |
87 # foreground tab on all platforms. | 88 # foreground tab on all platforms. |
89 foreground_tab_stats = RecordOneTab(tab.browser.foreground_tab) | |
88 | 90 |
89 RecordTabLoadTime(tab.browser.foreground_tab) | 91 if foreground_tab_stats: |
90 | 92 foreground_tab_load_complete = ( |
91 foreground_tab_stats = tab_load_times[0] | 93 foreground_tab_stats.load_end_ms - browser_main_entry_time_ms) |
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): | |
98 results.AddValue(scalar.ScalarValue( | 94 results.AddValue(scalar.ScalarValue( |
99 results.current_page, 'foreground_tab_request_start', 'ms', | 95 results.current_page, 'foreground_tab_load_complete', 'ms', |
100 foreground_tab_stats.request_start_ms - browser_main_entry_time_ms)) | 96 foreground_tab_load_complete)) |
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 |