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 | 4 |
5 """The tab switching measurement. | 5 """The tab switching measurement. |
6 | 6 |
7 This measurement opens pages in different tabs. After all the tabs have opened, | 7 This measurement opens pages in different tabs. After all the tabs have opened, |
8 it cycles through each tab in sequence, and records a histogram of the time | 8 it cycles through each tab in sequence, and records a histogram of the time |
9 between when a tab was first requested to be shown, and when it was painted. | 9 between when a tab was first requested to be shown, and when it was painted. |
10 Power usage is also measured. | 10 Power usage is also measured. |
11 """ | 11 """ |
12 | 12 |
13 import json | |
14 import time | |
15 | |
16 from telemetry.core import util | |
17 from telemetry.page import legacy_page_test | 13 from telemetry.page import legacy_page_test |
18 from telemetry.value import histogram | 14 from telemetry.value import histogram |
19 from telemetry.value import histogram_util | 15 from telemetry.value import histogram_util |
20 | 16 |
21 from metrics import keychain_metric | 17 from metrics import keychain_metric |
22 from metrics import power | |
23 | |
24 # TODO: Revisit this test once multitab support is finalized. | |
25 | 18 |
26 | 19 |
27 class TabSwitching(legacy_page_test.LegacyPageTest): | 20 class TabSwitching(legacy_page_test.LegacyPageTest): |
28 | |
29 # Amount of time to measure, in seconds. | |
30 SAMPLE_TIME = 30 | |
31 | |
32 def __init__(self): | 21 def __init__(self): |
33 super(TabSwitching, self).__init__() | 22 super(TabSwitching, self).__init__() |
34 self.first_page_in_storyset = True | 23 self._first_histogram = None |
35 self._power_metric = None | |
36 | 24 |
37 def CustomizeBrowserOptions(self, options): | 25 def CustomizeBrowserOptions(self, options): |
38 keychain_metric.KeychainMetric.CustomizeBrowserOptions(options) | 26 keychain_metric.KeychainMetric.CustomizeBrowserOptions(options) |
39 | 27 |
40 options.AppendExtraBrowserArgs([ | 28 options.AppendExtraBrowserArgs(['--enable-stats-collection-bindings']) |
41 '--enable-stats-collection-bindings' | |
42 ]) | |
43 # Enable background networking so we can test its impact on power usage. | |
44 options.disable_background_networking = False | |
45 power.PowerMetric.CustomizeBrowserOptions(options) | |
46 | 29 |
47 def WillStartBrowser(self, platform): | 30 @classmethod |
48 self.first_page_in_storyset = True | 31 def _GetTabSwitchHistogram(cls, tab_to_switch): |
49 self._power_metric = power.PowerMetric(platform, TabSwitching.SAMPLE_TIME) | 32 histogram_name = 'MPArch.RWH_TabSwitchPaintDuration' |
| 33 histogram_type = histogram_util.BROWSER_HISTOGRAM |
| 34 return histogram_util.GetHistogram( |
| 35 histogram_type, histogram_name, tab_to_switch) |
50 | 36 |
51 def TabForPage(self, page, browser): | 37 def DidNavigateToPage(self, page, tab): |
52 del page # unused | 38 """record the starting histogram""" |
53 if self.first_page_in_storyset: | 39 self._first_histogram = self._GetTabSwitchHistogram(tab) |
54 # The initial browser window contains a single tab, navigate that tab | |
55 # rather than creating a new one. | |
56 self.first_page_in_storyset = False | |
57 return browser.tabs[0] | |
58 | |
59 return browser.tabs.New() | |
60 | |
61 def StopBrowserAfterPage(self, browser, page): | |
62 # Restart the browser after the last page in the pageset. | |
63 return len(browser.tabs) >= len(page.story_set.stories) | |
64 | 40 |
65 def ValidateAndMeasurePage(self, page, tab, results): | 41 def ValidateAndMeasurePage(self, page, tab, results): |
66 """On the last tab, cycle through each tab that was opened and then record | 42 """record the ending histogram for the tab switching metric.""" |
67 a single histogram for the tab switching metric.""" | 43 last_histogram = self._GetTabSwitchHistogram(tab) |
68 browser = tab.browser | 44 total_diff_histogram = histogram_util.SubtractHistogram(last_histogram, |
69 if len(browser.tabs) != len(page.story_set.stories): | 45 self._first_histogram) |
70 return | |
71 | 46 |
72 if browser.tabs < 2: | |
73 raise Exception('Should have at least two tabs for tab switching') | |
74 | |
75 # Measure power usage of tabs after quiescence. | |
76 util.WaitFor(tab.HasReachedQuiescence, 60) | |
77 | |
78 if browser.platform.CanMonitorPower(): | |
79 self._power_metric.Start(page, tab) | |
80 time.sleep(TabSwitching.SAMPLE_TIME) | |
81 self._power_metric.Stop(page, tab) | |
82 self._power_metric.AddResults(tab, results,) | |
83 | |
84 histogram_name = 'MPArch.RWH_TabSwitchPaintDuration' | |
85 histogram_type = histogram_util.BROWSER_HISTOGRAM | |
86 display_name = 'MPArch_RWH_TabSwitchPaintDuration' | 47 display_name = 'MPArch_RWH_TabSwitchPaintDuration' |
87 first_histogram = histogram_util.GetHistogram( | |
88 histogram_type, histogram_name, tab) | |
89 prev_histogram = first_histogram | |
90 | |
91 for tab_to_switch in browser.tabs: | |
92 tab_to_switch.Activate() | |
93 def _IsDone(): | |
94 # pylint: disable=W0640 | |
95 cur_histogram = histogram_util.GetHistogram( | |
96 histogram_type, histogram_name, tab_to_switch) | |
97 diff_histogram = histogram_util.SubtractHistogram( | |
98 cur_histogram, prev_histogram) | |
99 # TODO(deanliao): Add SubtractHistogramRawValue to process histogram | |
100 # object instead of JSON string. | |
101 diff_histogram_count = json.loads(diff_histogram).get('count', 0) | |
102 return diff_histogram_count > 0 | |
103 util.WaitFor(_IsDone, 30) | |
104 | |
105 # We need to get histogram again instead of getting cur_histogram as | |
106 # variables modified inside inner function cannot be retrieved. However, | |
107 # inner function can see external scope's variables. | |
108 prev_histogram = histogram_util.GetHistogram( | |
109 histogram_type, histogram_name, tab_to_switch) | |
110 | |
111 last_histogram = prev_histogram | |
112 total_diff_histogram = histogram_util.SubtractHistogram(last_histogram, | |
113 first_histogram) | |
114 results.AddSummaryValue( | 48 results.AddSummaryValue( |
115 histogram.HistogramValue(None, display_name, 'ms', | 49 histogram.HistogramValue(None, display_name, 'ms', |
116 raw_value_json=total_diff_histogram, | 50 raw_value_json=total_diff_histogram, |
117 important=False)) | 51 important=False)) |
118 | 52 |
119 keychain_metric.KeychainMetric().AddResults(tab, results) | 53 keychain_metric.KeychainMetric().AddResults(tab, results) |
120 | |
121 def DidRunPage(self, platform): | |
122 del platform # unused | |
123 self._power_metric.Close() | |
OLD | NEW |