| 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 | 13 import time |
| 15 | 14 |
| 16 from telemetry.core import util | |
| 17 from telemetry.page import legacy_page_test | 15 from telemetry.page import legacy_page_test |
| 18 from telemetry.value import histogram | 16 from telemetry.value import histogram |
| 19 from telemetry.value import histogram_util | 17 from telemetry.value import histogram_util |
| 20 | 18 |
| 21 from metrics import keychain_metric | 19 from metrics import keychain_metric |
| 22 from metrics import power | 20 from metrics import power |
| 23 | 21 |
| 22 |
| 24 # TODO: Revisit this test once multitab support is finalized. | 23 # TODO: Revisit this test once multitab support is finalized. |
| 25 | 24 |
| 26 | 25 |
| 27 class TabSwitching(legacy_page_test.LegacyPageTest): | 26 class TabSwitching(legacy_page_test.LegacyPageTest): |
| 28 | 27 |
| 29 # Amount of time to measure, in seconds. | 28 # Amount of time to measure, in seconds. |
| 30 SAMPLE_TIME = 30 | 29 SAMPLE_TIME = 30 |
| 31 | 30 |
| 32 def __init__(self): | 31 def __init__(self): |
| 33 super(TabSwitching, self).__init__() | 32 super(TabSwitching, self).__init__() |
| 34 self.first_page_in_storyset = True | 33 self.first_page_in_storyset = True |
| 35 self._power_metric = None | 34 self._power_metric = None |
| 35 self._first_histogram = None |
| 36 | 36 |
| 37 def CustomizeBrowserOptions(self, options): | 37 def CustomizeBrowserOptions(self, options): |
| 38 keychain_metric.KeychainMetric.CustomizeBrowserOptions(options) | 38 keychain_metric.KeychainMetric.CustomizeBrowserOptions(options) |
| 39 | 39 |
| 40 options.AppendExtraBrowserArgs([ | 40 options.AppendExtraBrowserArgs([ |
| 41 '--enable-stats-collection-bindings' | 41 '--enable-stats-collection-bindings' |
| 42 ]) | 42 ]) |
| 43 # Enable background networking so we can test its impact on power usage. | 43 # Enable background networking so we can test its impact on power usage. |
| 44 options.disable_background_networking = False | 44 options.disable_background_networking = False |
| 45 power.PowerMetric.CustomizeBrowserOptions(options) | 45 power.PowerMetric.CustomizeBrowserOptions(options) |
| 46 | 46 |
| 47 def WillStartBrowser(self, platform): | 47 def WillStartBrowser(self, platform): |
| 48 self.first_page_in_storyset = True | |
| 49 self._power_metric = power.PowerMetric(platform, TabSwitching.SAMPLE_TIME) | 48 self._power_metric = power.PowerMetric(platform, TabSwitching.SAMPLE_TIME) |
| 50 | 49 |
| 51 def TabForPage(self, page, browser): | 50 @classmethod |
| 52 del page # unused | 51 def _GetTabSwitchHistogram(cls, tab_to_switch): |
| 53 if self.first_page_in_storyset: | 52 histogram_name = 'MPArch.RWH_TabSwitchPaintDuration' |
| 54 # The initial browser window contains a single tab, navigate that tab | 53 histogram_type = histogram_util.BROWSER_HISTOGRAM |
| 55 # rather than creating a new one. | 54 return histogram_util.GetHistogram( |
| 56 self.first_page_in_storyset = False | 55 histogram_type, histogram_name, tab_to_switch) |
| 57 return browser.tabs[0] | |
| 58 | 56 |
| 59 return browser.tabs.New() | 57 def DidNavigateToPage(self, page, tab): |
| 60 | 58 """record the starting histogram""" |
| 61 def StopBrowserAfterPage(self, browser, page): | 59 self._first_histogram = self._GetTabSwitchHistogram(tab) |
| 62 # Restart the browser after the last page in the pageset. | |
| 63 return len(browser.tabs) >= len(page.story_set.stories) | |
| 64 | 60 |
| 65 def ValidateAndMeasurePage(self, page, tab, results): | 61 def ValidateAndMeasurePage(self, page, tab, results): |
| 66 """On the last tab, cycle through each tab that was opened and then record | 62 """record the ending histogram for the tab switching metric.""" |
| 67 a single histogram for the tab switching metric.""" | 63 if tab.browser.platform.CanMonitorPower(): |
| 68 browser = tab.browser | |
| 69 if len(browser.tabs) != len(page.story_set.stories): | |
| 70 return | |
| 71 | |
| 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) | 64 self._power_metric.Start(page, tab) |
| 80 time.sleep(TabSwitching.SAMPLE_TIME) | 65 time.sleep(TabSwitching.SAMPLE_TIME) |
| 81 self._power_metric.Stop(page, tab) | 66 self._power_metric.Stop(page, tab) |
| 82 self._power_metric.AddResults(tab, results,) | 67 self._power_metric.AddResults(tab, results,) |
| 83 | 68 |
| 84 histogram_name = 'MPArch.RWH_TabSwitchPaintDuration' | 69 last_histogram = self._GetTabSwitchHistogram(tab) |
| 85 histogram_type = histogram_util.BROWSER_HISTOGRAM | 70 total_diff_histogram = histogram_util.SubtractHistogram(last_histogram, |
| 71 self._first_histogram) |
| 86 display_name = 'MPArch_RWH_TabSwitchPaintDuration' | 72 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( | 73 results.AddSummaryValue( |
| 115 histogram.HistogramValue(None, display_name, 'ms', | 74 histogram.HistogramValue(None, display_name, 'ms', |
| 116 raw_value_json=total_diff_histogram, | 75 raw_value_json=total_diff_histogram, |
| 117 important=False)) | 76 important=False)) |
| 118 | 77 |
| 119 keychain_metric.KeychainMetric().AddResults(tab, results) | 78 keychain_metric.KeychainMetric().AddResults(tab, results) |
| 120 | 79 |
| 121 def DidRunPage(self, platform): | 80 def DidRunPage(self, platform): |
| 122 del platform # unused | 81 del platform # unused |
| 123 self._power_metric.Close() | 82 self._power_metric.Close() |
| OLD | NEW |