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.
14 from metrics
import cpu
15 from metrics
import histogram_util
16 from telemetry
.core
import util
17 from telemetry
.page
import page_measurement
18 from telemetry
.page
import page_runner
20 # TODO: Revisit this test once multitab support is finalized.
22 class TabSwitching(page_measurement
.PageMeasurement
):
24 super(TabSwitching
, self
).__init
__()
25 self
._cpu
_metric
= None
27 def CustomizeBrowserOptions(self
, options
):
28 options
.AppendExtraBrowserArgs([
29 '--enable-stats-collection-bindings'
32 def DidStartBrowser(self
, browser
):
33 self
._cpu
_metric
= cpu
.Cpu(browser
)
35 def CanRunForPage(self
, page
):
36 return not page
.page_set
.pages
.index(page
)
38 def DidNavigateToPage(self
, page
, tab
):
39 for i
in xrange(1, len(page
.page_set
.pages
)):
40 t
= tab
.browser
.tabs
.New()
42 # We create a test_stub to be able to call 'navigate_steps' on pages
43 test_stub
= page_measurement
.PageMeasurement()
44 page_state
= page_runner
.PageState()
45 page_state
.PreparePage(page
.page_set
.pages
[i
], t
)
46 page_state
.ImplicitPageNavigation(page
.page_set
.pages
[i
], t
, test_stub
)
48 # Start tracking cpu load after all pages have loaded.
49 self
._cpu
_metric
.Start(page
, tab
)
51 def MeasurePage(self
, page
, tab
, results
):
52 """Although this is called MeasurePage, we're actually using this function
53 to cycle through each tab that was opened via DidNavigateToPage and
54 then record a single histogram for the tab switching metric.
56 # Calculate the idle cpu load before any actions are done.
58 self
._cpu
_metric
.Stop(page
, tab
)
59 self
._cpu
_metric
.AddResults(tab
, results
,
60 'idle_cpu_utilization')
62 histogram_name
= 'MPArch.RWH_TabSwitchPaintDuration'
63 histogram_type
= histogram_util
.BROWSER_HISTOGRAM
64 first_histogram
= histogram_util
.GetHistogram(
65 histogram_type
, histogram_name
, tab
)
66 prev_histogram
= first_histogram
68 for i
in xrange(len(tab
.browser
.tabs
)):
69 t
= tab
.browser
.tabs
[i
]
72 cur_histogram
= histogram_util
.GetHistogram(
73 histogram_type
, histogram_name
, tab
)
74 diff_histogram
= histogram_util
.SubtractHistogram(
75 cur_histogram
, prev_histogram
)
77 util
.WaitFor(_IsDone
, 30)
78 prev_histogram
= histogram_util
.GetHistogram(
79 histogram_type
, histogram_name
, tab
)
81 last_histogram
= histogram_util
.GetHistogram(
82 histogram_type
, histogram_name
, tab
)
83 diff_histogram
= histogram_util
.SubtractHistogram(last_histogram
,
86 results
.AddSummary(histogram_name
, '', diff_histogram
,
87 data_type
='unimportant-histogram')