Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / tools / perf / measurements / tab_switching.py
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 Power usage is also measured.
11 """
12
13 import time
14
15 from metrics import power
16 from telemetry.core import util
17 from telemetry.page import page_test
18 from telemetry.value import histogram
19 from telemetry.value import histogram_util
20
21 # TODO: Revisit this test once multitab support is finalized.
22
23 class TabSwitching(page_test.PageTest):
24
25   # Amount of time to measure, in seconds.
26   SAMPLE_TIME = 30
27
28   def __init__(self):
29     super(TabSwitching, self).__init__()
30     self._first_page_in_pageset = True
31     self._power_metric = None
32
33   def CustomizeBrowserOptions(self, options):
34     options.AppendExtraBrowserArgs([
35         '--enable-stats-collection-bindings'
36     ])
37     # Enable background networking so we can test its impact on power usage.
38     options.disable_background_networking = False
39     power.PowerMetric.CustomizeBrowserOptions(options)
40
41   def WillStartBrowser(self, platform):
42     self._first_page_in_pageset = True
43     self._power_metric = power.PowerMetric(platform, TabSwitching.SAMPLE_TIME)
44
45   def TabForPage(self, page, browser):
46     if self._first_page_in_pageset:
47       # The initial browser window contains a single tab, navigate that tab
48       # rather than creating a new one.
49       self._first_page_in_pageset = False
50       return browser.tabs[0]
51
52     return browser.tabs.New()
53
54   def StopBrowserAfterPage(self, browser, page):
55     # Restart the browser after the last page in the pageset.
56     return len(browser.tabs) >= len(page.page_set.pages)
57
58   def ValidateAndMeasurePage(self, page, tab, results):
59     """On the last tab, cycle through each tab that was opened and then record
60     a single histogram for the tab switching metric."""
61     if len(tab.browser.tabs) != len(page.page_set.pages):
62       return
63
64     # Measure power usage of tabs after quiescence.
65     util.WaitFor(tab.HasReachedQuiescence, 60)
66
67     if tab.browser.platform.CanMonitorPower():
68       self._power_metric.Start(page, tab)
69       time.sleep(TabSwitching.SAMPLE_TIME)
70       self._power_metric.Stop(page, tab)
71       self._power_metric.AddResults(tab, results,)
72
73     histogram_name = 'MPArch.RWH_TabSwitchPaintDuration'
74     histogram_type = histogram_util.BROWSER_HISTOGRAM
75     display_name = 'MPArch_RWH_TabSwitchPaintDuration'
76     first_histogram = histogram_util.GetHistogram(
77         histogram_type, histogram_name, tab)
78     prev_histogram = first_histogram
79
80     for i in xrange(len(tab.browser.tabs)):
81       t = tab.browser.tabs[i]
82       t.Activate()
83       def _IsDone():
84         cur_histogram = histogram_util.GetHistogram(
85             histogram_type, histogram_name, tab)
86         diff_histogram = histogram_util.SubtractHistogram(
87             cur_histogram, prev_histogram)
88         return diff_histogram
89       util.WaitFor(_IsDone, 30)
90       prev_histogram = histogram_util.GetHistogram(
91           histogram_type, histogram_name, tab)
92
93     last_histogram = histogram_util.GetHistogram(
94         histogram_type, histogram_name, tab)
95     diff_histogram = histogram_util.SubtractHistogram(last_histogram,
96         first_histogram)
97
98     results.AddSummaryValue(
99         histogram.HistogramValue(None, display_name, 'ms',
100                                  raw_value_json=diff_histogram,
101                                  important=False))