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