| 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 29 matching lines...) Expand all Loading... |
| 52 # rather than creating a new one. | 53 # rather than creating a new one. |
| 53 self._first_page_in_pageset = False | 54 self._first_page_in_pageset = False |
| 54 return browser.tabs[0] | 55 return browser.tabs[0] |
| 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, last_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 = last_tab.browser |
| 67 if len(browser.tabs) != len(page.page_set.pages): |
| 66 return | 68 return |
| 67 | 69 |
| 68 # Measure power usage of tabs after quiescence. | 70 # Measure power usage of tabs after quiescence. |
| 69 util.WaitFor(tab.HasReachedQuiescence, 60) | 71 util.WaitFor(last_tab.HasReachedQuiescence, 60) |
| 70 | 72 |
| 71 if tab.browser.platform.CanMonitorPower(): | 73 if browser.platform.CanMonitorPower(): |
| 72 self._power_metric.Start(page, tab) | 74 self._power_metric.Start(page, last_tab) |
| 73 time.sleep(TabSwitching.SAMPLE_TIME) | 75 time.sleep(TabSwitching.SAMPLE_TIME) |
| 74 self._power_metric.Stop(page, tab) | 76 self._power_metric.Stop(page, last_tab) |
| 75 self._power_metric.AddResults(tab, results,) | 77 self._power_metric.AddResults(last_tab, results,) |
| 76 | 78 |
| 77 histogram_name = 'MPArch.RWH_TabSwitchPaintDuration' | 79 histogram_name = 'MPArch.RWH_TabSwitchPaintDuration' |
| 78 histogram_type = histogram_util.BROWSER_HISTOGRAM | 80 histogram_type = histogram_util.BROWSER_HISTOGRAM |
| 79 display_name = 'MPArch_RWH_TabSwitchPaintDuration' | 81 display_name = 'MPArch_RWH_TabSwitchPaintDuration' |
| 80 first_histogram = histogram_util.GetHistogram( | 82 first_histogram = histogram_util.GetHistogram( |
| 81 histogram_type, histogram_name, tab) | 83 histogram_type, histogram_name, last_tab) |
| 82 prev_histogram = first_histogram | 84 prev_histogram = first_histogram |
| 83 | 85 |
| 84 for i in xrange(len(tab.browser.tabs)): | 86 for i in xrange(len(browser.tabs)): |
| 85 t = tab.browser.tabs[i] | 87 # Note that browser.tabs iterable does not return tab object but tab id. |
| 86 t.Activate() | 88 # (http://crbug.com/473202) |
| 89 tab = browser.tabs[i] |
| 90 tab.Activate() |
| 87 def _IsDone(): | 91 def _IsDone(): |
| 88 cur_histogram = histogram_util.GetHistogram( | 92 cur_histogram = histogram_util.GetHistogram( |
| 89 histogram_type, histogram_name, tab) | 93 histogram_type, histogram_name, tab) |
| 90 diff_histogram = histogram_util.SubtractHistogram( | 94 diff_histogram = histogram_util.SubtractHistogram( |
| 91 cur_histogram, prev_histogram) | 95 cur_histogram, prev_histogram) |
| 92 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 |
| 93 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. |
| 94 prev_histogram = histogram_util.GetHistogram( | 105 prev_histogram = histogram_util.GetHistogram( |
| 95 histogram_type, histogram_name, tab) | 106 histogram_type, histogram_name, tab) |
| 96 | 107 |
| 97 last_histogram = histogram_util.GetHistogram( | 108 last_histogram = prev_histogram |
| 98 histogram_type, histogram_name, tab) | 109 total_diff_histogram = histogram_util.SubtractHistogram(last_histogram, |
| 99 diff_histogram = histogram_util.SubtractHistogram(last_histogram, | 110 first_histogram) |
| 100 first_histogram) | |
| 101 | |
| 102 results.AddSummaryValue( | 111 results.AddSummaryValue( |
| 103 histogram.HistogramValue(None, display_name, 'ms', | 112 histogram.HistogramValue(None, display_name, 'ms', |
| 104 raw_value_json=diff_histogram, | 113 raw_value_json=total_diff_histogram, |
| 105 important=False)) | 114 important=False)) |
| 106 | 115 |
| 107 keychain_metric.KeychainMetric().AddResults(tab, results) | 116 keychain_metric.KeychainMetric().AddResults(last_tab, results) |
| OLD | NEW |