Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / test / gpu / gpu_tests / memory.py
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
7
8 MEMORY_LIMIT_MB = 256
9 SINGLE_TAB_LIMIT_MB = 128
10 WIGGLE_ROOM_MB = 4
11
12 test_harness_script = r"""
13   var domAutomationController = {};
14   domAutomationController._finished = false;
15
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
25     //    manager are made.
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.
28
29     // Pump the cycle 8 times (in principle it could take an infinite number
30     // of iterations to settle).
31
32     var rafCount = 0;
33     var totalRafCount = 8;
34
35     function pumpRAF() {
36       if (rafCount == totalRafCount) {
37         domAutomationController._finished = true;
38         return;
39       }
40       ++rafCount;
41       window.requestAnimationFrame(pumpRAF);
42     }
43     pumpRAF();
44   }
45
46   window.domAutomationController = domAutomationController;
47
48   window.addEventListener("load", function() {
49     useGpuMemory(%d);
50   }, false);
51 """ % MEMORY_LIMIT_MB
52
53 class MemoryValidator(page_test.PageTest):
54   def __init__(self):
55     super(MemoryValidator, self).__init__('ValidatePage')
56
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
63
64     if mb_used + WIGGLE_ROOM_MB < SINGLE_TAB_LIMIT_MB:
65       raise page_test.Failure('Memory allocation too low')
66
67     if mb_used - WIGGLE_ROOM_MB > MEMORY_LIMIT_MB:
68       raise page_test.Failure('Memory allocation too high')
69
70   def CustomizeBrowserOptions(self, options):
71     options.AppendExtraBrowserArgs('--enable-logging')
72     options.AppendExtraBrowserArgs(
73         '--force-gpu-mem-available-mb=%s' % MEMORY_LIMIT_MB)
74
75   def WillNavigateToPage(self, page, tab):
76     custom_categories = ['webkit.console', 'gpu']
77     tab.browser.StartTracing(','.join(custom_categories), 60)
78
79 class Memory(test.Test):
80   """Tests GPU memory limits"""
81   test = MemoryValidator
82   page_set = 'page_sets/memory_tests.json'
83
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
88     return page_set