OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """The tab switching measurement. | 5 """The tab switching measurement. |
6 | 6 |
7 This measurement opens pages in different tabs. After all the tabs have opened, | 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 | 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. | 9 between when a tab was first requested to be shown, and when it was painted. |
10 Power usage is also measured. | 10 Power usage is also measured. |
11 """ | 11 """ |
12 | 12 |
| 13 import json |
13 import time | 14 import time |
14 | 15 |
15 from telemetry.core import util | 16 from telemetry.core import util |
16 from telemetry.page import page_test | 17 from telemetry.page import page_test |
17 from telemetry.value import histogram | 18 from telemetry.value import histogram |
18 from telemetry.value import histogram_util | 19 from telemetry.value import histogram_util |
19 | 20 |
20 from metrics import keychain_metric | 21 from metrics import keychain_metric |
21 from metrics import power | 22 from metrics import power |
22 | 23 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 | 56 |
56 return browser.tabs.New() | 57 return browser.tabs.New() |
57 | 58 |
58 def StopBrowserAfterPage(self, browser, page): | 59 def StopBrowserAfterPage(self, browser, page): |
59 # Restart the browser after the last page in the pageset. | 60 # Restart the browser after the last page in the pageset. |
60 return len(browser.tabs) >= len(page.page_set.pages) | 61 return len(browser.tabs) >= len(page.page_set.pages) |
61 | 62 |
62 def ValidateAndMeasurePage(self, page, tab, results): | 63 def ValidateAndMeasurePage(self, page, tab, results): |
63 """On the last tab, cycle through each tab that was opened and then record | 64 """On the last tab, cycle through each tab that was opened and then record |
64 a single histogram for the tab switching metric.""" | 65 a single histogram for the tab switching metric.""" |
65 if len(tab.browser.tabs) != len(page.page_set.pages): | 66 browser = tab.browser |
| 67 if len(browser.tabs) != len(page.page_set.pages): |
66 return | 68 return |
67 | 69 |
| 70 if browser.tabs < 2: |
| 71 raise Exception('Should have at least two tabs for tab switching') |
| 72 |
68 # Measure power usage of tabs after quiescence. | 73 # Measure power usage of tabs after quiescence. |
69 util.WaitFor(tab.HasReachedQuiescence, 60) | 74 util.WaitFor(tab.HasReachedQuiescence, 60) |
70 | 75 |
71 if tab.browser.platform.CanMonitorPower(): | 76 if browser.platform.CanMonitorPower(): |
72 self._power_metric.Start(page, tab) | 77 self._power_metric.Start(page, tab) |
73 time.sleep(TabSwitching.SAMPLE_TIME) | 78 time.sleep(TabSwitching.SAMPLE_TIME) |
74 self._power_metric.Stop(page, tab) | 79 self._power_metric.Stop(page, tab) |
75 self._power_metric.AddResults(tab, results,) | 80 self._power_metric.AddResults(tab, results,) |
76 | 81 |
77 histogram_name = 'MPArch.RWH_TabSwitchPaintDuration' | 82 histogram_name = 'MPArch.RWH_TabSwitchPaintDuration' |
78 histogram_type = histogram_util.BROWSER_HISTOGRAM | 83 histogram_type = histogram_util.BROWSER_HISTOGRAM |
79 display_name = 'MPArch_RWH_TabSwitchPaintDuration' | 84 display_name = 'MPArch_RWH_TabSwitchPaintDuration' |
80 first_histogram = histogram_util.GetHistogram( | 85 first_histogram = histogram_util.GetHistogram( |
81 histogram_type, histogram_name, tab) | 86 histogram_type, histogram_name, tab) |
82 prev_histogram = first_histogram | 87 prev_histogram = first_histogram |
83 | 88 |
84 for t in tab.browser.tabs: | 89 for tab_to_switch in browser.tabs: |
85 t.Activate() | 90 tab_to_switch.Activate() |
86 def _IsDone(): | 91 def _IsDone(): |
87 cur_histogram = histogram_util.GetHistogram( | 92 cur_histogram = histogram_util.GetHistogram( |
88 histogram_type, histogram_name, tab) | 93 histogram_type, histogram_name, tab_to_switch) |
89 diff_histogram = histogram_util.SubtractHistogram( | 94 diff_histogram = histogram_util.SubtractHistogram( |
90 cur_histogram, prev_histogram) | 95 cur_histogram, prev_histogram) |
91 return diff_histogram | 96 # TODO(deanliao): Add SubtractHistogramRawValue to process histogram |
| 97 # object instead of JSON string. |
| 98 diff_histogram_count = json.loads(diff_histogram).get('count', 0) |
| 99 return diff_histogram_count > 0 |
92 util.WaitFor(_IsDone, 30) | 100 util.WaitFor(_IsDone, 30) |
| 101 |
| 102 # We need to get histogram again instead of getting cur_histogram as |
| 103 # variables modified inside inner function cannot be retrieved. However, |
| 104 # inner function can see external scope's variables. |
93 prev_histogram = histogram_util.GetHistogram( | 105 prev_histogram = histogram_util.GetHistogram( |
94 histogram_type, histogram_name, tab) | 106 histogram_type, histogram_name, tab_to_switch) |
95 | 107 |
96 last_histogram = histogram_util.GetHistogram( | 108 last_histogram = prev_histogram |
97 histogram_type, histogram_name, tab) | 109 total_diff_histogram = histogram_util.SubtractHistogram(last_histogram, |
98 diff_histogram = histogram_util.SubtractHistogram(last_histogram, | 110 first_histogram) |
99 first_histogram) | |
100 | |
101 results.AddSummaryValue( | 111 results.AddSummaryValue( |
102 histogram.HistogramValue(None, display_name, 'ms', | 112 histogram.HistogramValue(None, display_name, 'ms', |
103 raw_value_json=diff_histogram, | 113 raw_value_json=total_diff_histogram, |
104 important=False)) | 114 important=False)) |
105 | 115 |
106 keychain_metric.KeychainMetric().AddResults(tab, results) | 116 keychain_metric.KeychainMetric().AddResults(tab, results) |
OLD | NEW |