Fix import error in mac_platform_backend.py
[chromium-blink-merge.git] / tools / perf / measurements / tab_switching.py
blob982f05f8c5d8ad6dc618f8d41ee3c73f8169da38
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 """
12 import time
14 from metrics import cpu
15 from metrics import histogram_util
16 from telemetry.core import util
17 from telemetry.page import page_measurement
19 # TODO: Revisit this test once multitab support is finalized.
21 class TabSwitching(page_measurement.PageMeasurement):
22 def __init__(self):
23 super(TabSwitching, self).__init__()
24 self._cpu_metric = None
26 def CustomizeBrowserOptions(self, options):
27 options.AppendExtraBrowserArgs([
28 '--enable-stats-collection-bindings'
31 def TabForPage(self, page, browser):
32 return browser.tabs.New()
34 def DidStartBrowser(self, browser):
35 self._cpu_metric = cpu.CpuMetric(browser)
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')
49 histogram_name = 'MPArch.RWH_TabSwitchPaintDuration'
50 histogram_type = histogram_util.BROWSER_HISTOGRAM
51 display_name = 'MPArch_RWH_TabSwitchPaintDuration'
52 first_histogram = histogram_util.GetHistogram(
53 histogram_type, histogram_name, tab)
54 prev_histogram = first_histogram
56 for i in xrange(len(tab.browser.tabs)):
57 t = tab.browser.tabs[i]
58 t.Activate()
59 def _IsDone():
60 cur_histogram = histogram_util.GetHistogram(
61 histogram_type, histogram_name, tab)
62 diff_histogram = histogram_util.SubtractHistogram(
63 cur_histogram, prev_histogram)
64 return diff_histogram
65 util.WaitFor(_IsDone, 30)
66 prev_histogram = histogram_util.GetHistogram(
67 histogram_type, histogram_name, tab)
69 last_histogram = histogram_util.GetHistogram(
70 histogram_type, histogram_name, tab)
71 diff_histogram = histogram_util.SubtractHistogram(last_histogram,
72 first_histogram)
74 results.AddSummary(display_name, '', diff_histogram,
75 data_type='unimportant-histogram')