903cf14fa43739debba04c0e0589de5e3ef01f4b
[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 histogram_util
16 from metrics import power
17 from telemetry.core import util
18 from telemetry.page import page_test
19 from telemetry.value import histogram
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     power.PowerMetric.CustomizeBrowserOptions(options)
38
39   def WillStartBrowser(self, browser):
40     self._first_page_in_pageset = True
41     self._power_metric = power.PowerMetric(browser, TabSwitching.SAMPLE_TIME)
42
43   def TabForPage(self, page, browser):
44     if self._first_page_in_pageset:
45       # The initial browser window contains a single tab, navigate that tab
46       # rather than creating a new one.
47       self._first_page_in_pageset = False
48       return browser.tabs[0]
49
50     return browser.tabs.New()
51
52   def StopBrowserAfterPage(self, browser, page):
53     # Restart the browser after the last page in the pageset.
54     return len(browser.tabs) >= len(page.page_set.pages)
55
56   def ValidateAndMeasurePage(self, page, tab, results):
57     """On the last tab, cycle through each tab that was opened and then record
58     a single histogram for the tab switching metric."""
59     if len(tab.browser.tabs) != len(page.page_set.pages):
60       return
61
62     # Measure power usage of tabs after quiescence.
63     util.WaitFor(tab.HasReachedQuiescence, 60)
64
65     if tab.browser.platform.CanMonitorPower():
66       self._power_metric.Start(page, tab)
67       time.sleep(TabSwitching.SAMPLE_TIME)
68       self._power_metric.Stop(page, tab)
69       self._power_metric.AddResults(tab, results,)
70
71     histogram_name = 'MPArch.RWH_TabSwitchPaintDuration'
72     histogram_type = histogram_util.BROWSER_HISTOGRAM
73     display_name = 'MPArch_RWH_TabSwitchPaintDuration'
74     first_histogram = histogram_util.GetHistogram(
75         histogram_type, histogram_name, tab)
76     prev_histogram = first_histogram
77
78     for i in xrange(len(tab.browser.tabs)):
79       t = tab.browser.tabs[i]
80       t.Activate()
81       def _IsDone():
82         cur_histogram = histogram_util.GetHistogram(
83             histogram_type, histogram_name, tab)
84         diff_histogram = histogram_util.SubtractHistogram(
85             cur_histogram, prev_histogram)
86         return diff_histogram
87       util.WaitFor(_IsDone, 30)
88       prev_histogram = histogram_util.GetHistogram(
89           histogram_type, histogram_name, tab)
90
91     last_histogram = histogram_util.GetHistogram(
92         histogram_type, histogram_name, tab)
93     diff_histogram = histogram_util.SubtractHistogram(last_histogram,
94         first_histogram)
95
96     results.AddSummaryValue(
97         histogram.HistogramValue(None, display_name, '',
98                                  raw_value_json=diff_histogram,
99                                  important=False))