Home | History | Annotate | Download | only in measurements
      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 """Multi tab memory test.
      6 
      7 This test is a multi tab test, but we're interested in measurements for
      8 the entire test rather than each single page.
      9 """
     10 
     11 import logging
     12 
     13 from metrics import histogram_util
     14 from telemetry.page import page_measurement
     15 
     16 
     17 class MemoryPressure(page_measurement.PageMeasurement):
     18   def __init__(self, *args, **kwargs):
     19     super(MemoryPressure, self).__init__(*args, **kwargs)
     20     # _first_tab is used to access histograms
     21     self._is_first_page = True
     22     self._tab_count = 0
     23 
     24   # Allow histogram collection
     25   def CustomizeBrowserOptions(self, options):
     26     histogram_util.CustomizeBrowserOptions(options)
     27 
     28   # Open a new tab at each page
     29   def TabForPage(self, page, browser):
     30     return browser.tabs.New()
     31 
     32   def GetTabsHistogramCounts(self, tab):
     33     histogram_type = histogram_util.BROWSER_HISTOGRAM
     34     discard_count = histogram_util.GetHistogramCount(
     35       histogram_type, "Tabs.Discard.DiscardCount", tab)
     36     kill_count = histogram_util.GetHistogramCount(
     37       histogram_type, "Tabs.SadTab.KillCreated", tab)
     38     return (discard_count, kill_count)
     39 
     40   def MeasurePage(self, page, tab, results):
     41     # After navigating to each page, check if it triggered tab discards or
     42     # kills.
     43     (discard_count, kill_count) = self.GetTabsHistogramCounts(tab)
     44     # Done with this tab.  Disconnect cleanly from it to avoid a possible
     45     # TabCrashException if the tab is discarded or killed.
     46     tab.Disconnect()
     47     # Sanity check for first page
     48     if self._is_first_page:
     49       self._is_first_page = False
     50       assert discard_count == 0 and kill_count == 0
     51 
     52     self._tab_count += 1
     53     # End the test at the first kill or discard.
     54     if kill_count > 0 or discard_count > 0:
     55       logging.info("test ending at tab %d, discards = %d, kills = %d" %
     56         (self._tab_count, discard_count, kill_count))
     57       self.RequestExit()
     58