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.
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.
31 super(TabSwitching
, self
).__init
__()
32 self
.first_page_in_storyset
= 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_storyset
= True
47 self
._power
_metric
= power
.PowerMetric(platform
, TabSwitching
.SAMPLE_TIME
)
49 def TabForPage(self
, page
, browser
):
50 if self
.first_page_in_storyset
:
51 # The initial browser window contains a single tab, navigate that tab
52 # rather than creating a new one.
53 self
.first_page_in_storyset
= 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
.story_set
.stories
)
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
.story_set
.stories
):
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 t
in tab
.browser
.tabs
:
87 cur_histogram
= histogram_util
.GetHistogram(
88 histogram_type
, histogram_name
, tab
)
89 diff_histogram
= histogram_util
.SubtractHistogram(
90 cur_histogram
, prev_histogram
)
92 util
.WaitFor(_IsDone
, 30)
93 prev_histogram
= histogram_util
.GetHistogram(
94 histogram_type
, histogram_name
, tab
)
96 last_histogram
= histogram_util
.GetHistogram(
97 histogram_type
, histogram_name
, tab
)
98 diff_histogram
= histogram_util
.SubtractHistogram(last_histogram
,
101 results
.AddSummaryValue(
102 histogram
.HistogramValue(None, display_name
, 'ms',
103 raw_value_json
=diff_histogram
,
106 keychain_metric
.KeychainMetric().AddResults(tab
, results
)