[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / tools / perf / measurements / tab_switching.py
blobd4110e17251a6b834de81b0acd198d631a5224bb
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
3 # found in the LICENSE file.
5 """The tab switching measurement.
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
9 between when a tab was first requested to be shown, and when it was painted.
10 Power usage is also measured.
11 """
13 import time
15 from telemetry.core import util
16 from telemetry.page import page_test
17 from telemetry.value import histogram
18 from telemetry.value import histogram_util
20 from metrics import keychain_metric
21 from metrics import power
23 # TODO: Revisit this test once multitab support is finalized.
25 class TabSwitching(page_test.PageTest):
27 # Amount of time to measure, in seconds.
28 SAMPLE_TIME = 30
30 def __init__(self):
31 super(TabSwitching, self).__init__()
32 self._first_page_in_pageset = True
33 self._power_metric = None
35 def CustomizeBrowserOptions(self, options):
36 keychain_metric.KeychainMetric.CustomizeBrowserOptions(options)
38 options.AppendExtraBrowserArgs([
39 '--enable-stats-collection-bindings'
41 # Enable background networking so we can test its impact on power usage.
42 options.disable_background_networking = False
43 power.PowerMetric.CustomizeBrowserOptions(options)
45 def WillStartBrowser(self, platform):
46 self._first_page_in_pageset = True
47 self._power_metric = power.PowerMetric(platform, TabSwitching.SAMPLE_TIME)
49 def TabForPage(self, page, browser):
50 if self._first_page_in_pageset:
51 # The initial browser window contains a single tab, navigate that tab
52 # rather than creating a new one.
53 self._first_page_in_pageset = False
54 return browser.tabs[0]
56 return browser.tabs.New()
58 def StopBrowserAfterPage(self, browser, page):
59 # Restart the browser after the last page in the pageset.
60 return len(browser.tabs) >= len(page.page_set.pages)
62 def ValidateAndMeasurePage(self, page, tab, results):
63 """On the last tab, cycle through each tab that was opened and then record
64 a single histogram for the tab switching metric."""
65 if len(tab.browser.tabs) != len(page.page_set.pages):
66 return
68 # Measure power usage of tabs after quiescence.
69 util.WaitFor(tab.HasReachedQuiescence, 60)
71 if tab.browser.platform.CanMonitorPower():
72 self._power_metric.Start(page, tab)
73 time.sleep(TabSwitching.SAMPLE_TIME)
74 self._power_metric.Stop(page, tab)
75 self._power_metric.AddResults(tab, results,)
77 histogram_name = 'MPArch.RWH_TabSwitchPaintDuration'
78 histogram_type = histogram_util.BROWSER_HISTOGRAM
79 display_name = 'MPArch_RWH_TabSwitchPaintDuration'
80 first_histogram = histogram_util.GetHistogram(
81 histogram_type, histogram_name, tab)
82 prev_histogram = first_histogram
84 for i in xrange(len(tab.browser.tabs)):
85 t = tab.browser.tabs[i]
86 t.Activate()
87 def _IsDone():
88 cur_histogram = histogram_util.GetHistogram(
89 histogram_type, histogram_name, tab)
90 diff_histogram = histogram_util.SubtractHistogram(
91 cur_histogram, prev_histogram)
92 return diff_histogram
93 util.WaitFor(_IsDone, 30)
94 prev_histogram = histogram_util.GetHistogram(
95 histogram_type, histogram_name, tab)
97 last_histogram = histogram_util.GetHistogram(
98 histogram_type, histogram_name, tab)
99 diff_histogram = histogram_util.SubtractHistogram(last_histogram,
100 first_histogram)
102 results.AddSummaryValue(
103 histogram.HistogramValue(None, display_name, 'ms',
104 raw_value_json=diff_histogram,
105 important=False))
107 keychain_metric.KeychainMetric().AddResults(tab, results)