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.
4
5"""The tab switching measurement.
6
7This measurement opens pages in different tabs. After all the tabs have opened,
8it cycles through each tab in sequence, and records a histogram of the time
9between when a tab was first requested to be shown, and when it was painted.
10"""
11
12import time
13
14from metrics import cpu
15from metrics import histogram_util
16from telemetry.core import util
17from telemetry.page import page_measurement
18
19# TODO: Revisit this test once multitab support is finalized.
20
21class TabSwitching(page_measurement.PageMeasurement):
22  def __init__(self):
23    super(TabSwitching, self).__init__()
24    self._cpu_metric = None
25
26  def CustomizeBrowserOptions(self, options):
27    options.AppendExtraBrowserArgs([
28        '--enable-stats-collection-bindings'
29    ])
30
31  def TabForPage(self, page, browser):
32    return browser.tabs.New()
33
34  def DidStartBrowser(self, browser):
35    self._cpu_metric = cpu.CpuMetric(browser)
36
37  def MeasurePage(self, page, tab, results):
38    """On the last tab, cycle through each tab that was opened and then record
39    a single histogram for the tab switching metric."""
40    if len(tab.browser.tabs) != len(page.page_set.pages):
41      return
42    self._cpu_metric.Start(page, tab)
43    time.sleep(.5)
44    self._cpu_metric.Stop(page, tab)
45    # Calculate the idle cpu load before any actions are done.
46    self._cpu_metric.AddResults(tab, results,
47                                'idle_cpu_utilization')
48
49    histogram_name = 'MPArch.RWH_TabSwitchPaintDuration'
50    histogram_type = histogram_util.BROWSER_HISTOGRAM
51    first_histogram = histogram_util.GetHistogram(
52        histogram_type, histogram_name, tab)
53    prev_histogram = first_histogram
54
55    for i in xrange(len(tab.browser.tabs)):
56      t = tab.browser.tabs[i]
57      t.Activate()
58      def _IsDone():
59        cur_histogram = histogram_util.GetHistogram(
60            histogram_type, histogram_name, tab)
61        diff_histogram = histogram_util.SubtractHistogram(
62            cur_histogram, prev_histogram)
63        return diff_histogram
64      util.WaitFor(_IsDone, 30)
65      prev_histogram = histogram_util.GetHistogram(
66          histogram_type, histogram_name, tab)
67
68    last_histogram = histogram_util.GetHistogram(
69        histogram_type, histogram_name, tab)
70    diff_histogram = histogram_util.SubtractHistogram(last_histogram,
71        first_histogram)
72
73    results.AddSummary(histogram_name, '', diff_histogram,
74        data_type='unimportant-histogram')
75