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
5import collections
6
7from measurements import startup
8from metrics import cpu
9from metrics import startup_metric
10from telemetry.core import util
11from telemetry.value import histogram_util
12
13
14class SessionRestore(startup.Startup):
15  """Performs a measurement of Chromium's Session restore performance.
16
17  This test is meant to be run against a generated profile.
18  This test inherits support for the --warm or --cold command line options -
19  see startup.py for details.
20  """
21
22  def __init__(self, action_name_to_run = ''):
23    super(SessionRestore, self).__init__(action_name_to_run=action_name_to_run)
24    self.close_tabs_before_run = False
25    self._cpu_metric = None
26
27  def CustomizeBrowserOptions(self, options):
28    super(SessionRestore, self).CustomizeBrowserOptions(options)
29    histogram_util.CustomizeBrowserOptions(options)
30    options.AppendExtraBrowserArgs([
31        '--restore-last-session'
32    ])
33
34  def TabForPage(self, page, browser):
35    # Detect that the session restore has completed.
36    util.WaitFor(lambda: browser.tabs and
37                 histogram_util.GetHistogramCount(
38                     histogram_util.BROWSER_HISTOGRAM,
39                     'SessionRestore.AllTabsLoaded',
40                     browser.foreground_tab),
41                 60)
42    return browser.foreground_tab
43
44  def CanRunForPage(self, page):
45    # No matter how many pages in the pageset, just perform one test iteration.
46    return page.page_set.pages.index(page) == 0
47
48  def RunNavigateSteps(self, page, tab):
49    # Overriden so that no page navigation occurs.
50    pass
51
52  def ValidatePageSet(self, page_set):
53    wpr_archive_names_to_page_urls = collections.defaultdict(list)
54    # Construct the map from pages' wpr archive names to pages' urls.
55    for page in page_set:
56      if page.is_local:
57        continue
58      wpr_archive_name = page_set.WprFilePathForPage(page)
59      wpr_archive_names_to_page_urls[wpr_archive_name].append(page.url)
60
61    # Reject any pageset that contains more than one WPR archive.
62    if len(wpr_archive_names_to_page_urls.keys()) > 1:
63      raise Exception("Invalid pageset: more than 1 WPR archive found.: " +
64          repr(wpr_archive_names_to_page_urls))
65
66  def DidStartBrowser(self, browser):
67    self._cpu_metric = cpu.CpuMetric(browser)
68    self._cpu_metric.Start(None, None)
69
70  def ValidateAndMeasurePage(self, page, tab, results):
71    tab.WaitForDocumentReadyStateToBeComplete()
72
73    # Record CPU usage from browser start to when the foreground page is loaded.
74    self._cpu_metric.Stop(None, None)
75    self._cpu_metric.AddResults(tab, results, 'cpu_utilization')
76
77    startup_metric.StartupMetric().AddResults(tab, results)
78
79    # TODO(jeremy): Measure time to load - first, last and frontmost tab here.
80