1 # Copyright (c) 2012 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 import memory_expectations
6 from telemetry import test
7 from telemetry.page import page_test
8 from telemetry.core.timeline import counter
9 from telemetry.core.timeline import model
12 SINGLE_TAB_LIMIT_MB = 128
15 test_harness_script = r"""
16 var domAutomationController = {};
17 domAutomationController._finished = false;
19 domAutomationController.send = function(msg) {
20 // This should wait until all effects of memory management complete.
21 // We will need to wait until all
22 // 1. pending commits from the main thread to the impl thread in the
23 // compositor complete (for visible compositors).
24 // 2. allocations that the renderer's impl thread will make due to the
25 // compositor and WebGL are completed.
26 // 3. pending GpuMemoryManager::Manage() calls to manage are made.
27 // 4. renderers' OnMemoryAllocationChanged callbacks in response to
29 // Each step in this sequence can cause trigger the next (as a 1-2-3-4-1
30 // cycle), so we will need to pump this cycle until it stabilizes.
32 // Pump the cycle 8 times (in principle it could take an infinite number
33 // of iterations to settle).
36 var totalRafCount = 8;
39 if (rafCount == totalRafCount) {
40 domAutomationController._finished = true;
44 window.requestAnimationFrame(pumpRAF);
49 window.domAutomationController = domAutomationController;
51 window.addEventListener("load", function() {
56 class _MemoryValidator(page_test.PageTest):
57 def ValidatePage(self, page, tab, results):
58 timeline_data = tab.browser.StopTracing()
59 timeline_model = model.TimelineModel(timeline_data)
60 for process in timeline_model.GetAllProcesses():
61 if 'gpu.GpuMemoryUsage' in process.counters:
62 counter = process.GetCounter('gpu', 'GpuMemoryUsage')
63 mb_used = counter.samples[-1] / 1048576
65 if mb_used + WIGGLE_ROOM_MB < SINGLE_TAB_LIMIT_MB:
66 raise page_test.Failure('Memory allocation too low')
68 if mb_used - WIGGLE_ROOM_MB > MEMORY_LIMIT_MB:
69 raise page_test.Failure('Memory allocation too high')
71 def CustomizeBrowserOptions(self, options):
72 options.AppendExtraBrowserArgs('--enable-logging')
73 options.AppendExtraBrowserArgs(
74 '--force-gpu-mem-available-mb=%s' % MEMORY_LIMIT_MB)
76 def WillNavigateToPage(self, page, tab):
77 custom_categories = ['webkit.console', 'gpu']
78 tab.browser.StartTracing(','.join(custom_categories), 60)
80 class Memory(test.Test):
81 """Tests GPU memory limits"""
82 test = _MemoryValidator
83 page_set = 'page_sets/memory_tests.py'
85 def CreateExpectations(self, page_set):
86 return memory_expectations.MemoryExpectations()
88 def CreatePageSet(self, options):
89 page_set = super(Memory, self).CreatePageSet(options)
90 for page in page_set.pages:
91 page.script_to_evaluate_on_commit = test_harness_script