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