11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)# Copyright 2013 The Chromium Authors. All rights reserved.
21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)# found in the LICENSE file.
41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)"""Multi tab memory test.
61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)This test is a multi tab test, but we're interested in measurements for
81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)the entire test rather than each single page.
91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)"""
101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)import logging
121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccifrom telemetry.value import histogram_util
146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)from telemetry.page import page_test
151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class MemoryPressure(page_test.PageTest):
181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  def __init__(self, *args, **kwargs):
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    super(MemoryPressure, self).__init__(*args, **kwargs)
201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    # _first_tab is used to access histograms
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    self._is_first_page = True
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    self._tab_count = 0
231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  # Allow histogram collection
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  def CustomizeBrowserOptions(self, options):
261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    histogram_util.CustomizeBrowserOptions(options)
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  # Open a new tab at each page
291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  def TabForPage(self, page, browser):
301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return browser.tabs.New()
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  def GetTabsHistogramCounts(self, tab):
331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    histogram_type = histogram_util.BROWSER_HISTOGRAM
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    discard_count = histogram_util.GetHistogramCount(
351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      histogram_type, "Tabs.Discard.DiscardCount", tab)
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    kill_count = histogram_util.GetHistogramCount(
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      histogram_type, "Tabs.SadTab.KillCreated", tab)
381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return (discard_count, kill_count)
391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  def ValidateAndMeasurePage(self, page, tab, results):
411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    # After navigating to each page, check if it triggered tab discards or
421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    # kills.
431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    (discard_count, kill_count) = self.GetTabsHistogramCounts(tab)
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    # Sanity check for first page
461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if self._is_first_page:
471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      self._is_first_page = False
481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      assert discard_count == 0 and kill_count == 0
491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    self._tab_count += 1
511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    # End the test at the first kill or discard.
521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if kill_count > 0 or discard_count > 0:
531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      logging.info("test ending at tab %d, discards = %d, kills = %d" %
541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        (self._tab_count, discard_count, kill_count))
551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      self.RequestExit()
56