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 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 """ 11 12 from metrics import histogram_util 13 from telemetry.core import util 14 from telemetry.page import page_measurement 15 from telemetry.page import page_runner 16 17 # TODO: Revisit this test once multitab support is finalized. 18 19 class TabSwitching(page_measurement.PageMeasurement): 20 def CustomizeBrowserOptions(self, options): 21 options.AppendExtraBrowserArg('--enable-stats-collection-bindings') 22 options.AppendExtraBrowserArg('--dom-automation') 23 24 def CanRunForPage(self, page): 25 return not page.page_set.pages.index(page) 26 27 def DidNavigateToPage(self, page, tab): 28 for i in xrange(1, len(page.page_set.pages)): 29 t = tab.browser.tabs.New() 30 31 page_state = page_runner.PageState() 32 page_state.PreparePage(page.page_set.pages[i], t) 33 34 def MeasurePage(self, _, tab, results): 35 """Although this is called MeasurePage, we're actually using this function 36 to cycle through each tab that was opened via DidNavigateToPage and 37 thenrecord a single histogram for the tab switching metric. 38 """ 39 histogram_name = 'MPArch.RWH_TabSwitchPaintDuration' 40 histogram_type = 'getBrowserHistogram' 41 first_histogram = histogram_util.GetHistogramFromDomAutomation( 42 histogram_type, histogram_name, tab) 43 prev_histogram = first_histogram 44 45 for i in xrange(len(tab.browser.tabs)): 46 t = tab.browser.tabs[i] 47 t.Activate() 48 def _IsDone(): 49 cur_histogram = histogram_util.GetHistogramFromDomAutomation( 50 histogram_type, histogram_name, tab) 51 diff_histogram = histogram_util.SubtractHistogram( 52 cur_histogram, prev_histogram) 53 return diff_histogram 54 util.WaitFor(_IsDone, 30) 55 prev_histogram = histogram_util.GetHistogramFromDomAutomation( 56 histogram_type, histogram_name, tab) 57 58 last_histogram = histogram_util.GetHistogramFromDomAutomation( 59 histogram_type, histogram_name, tab) 60 diff_histogram = histogram_util.SubtractHistogram(last_histogram, 61 first_histogram) 62 63 results.AddSummary(histogram_name, '', diff_histogram, 64 data_type='unimportant-histogram') 65