1 // Copyright (c) 2013 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 5 'use strict'; 6 7 base.require('tcmalloc.heap'); 8 9 base.unittest.testSuite('tcmalloc.heap', function() { 10 var HeapSnapshot = tcmalloc.HeapSnapshot; 11 12 // Tests total allocation count. 13 test('totals', function() { 14 var snapshot = new HeapSnapshot({}, 1, [ 15 { 16 'current_allocs': 10, 17 'total_allocs': 100, 18 'current_bytes': 10000, 19 'trace': '', 20 'total_bytes': 100000 21 }, 22 { 23 'current_allocs': 2, 24 'total_allocs': 22, 25 'current_bytes': 200, 26 'trace': 'TestObject::TestMethod ', 27 'total_bytes': 2200 28 } 29 ]); 30 snapshot.preInitialize(); 31 snapshot.initialize(); 32 33 // Base class got the timestamp. 34 assertEquals(1, snapshot.ts); 35 36 // The first entry in the list is for totals. 37 assertEquals(10, snapshot.total_.currentAllocs); 38 assertEquals(10000, snapshot.total_.currentBytes); 39 }); 40 41 // Tests multi-level trace stacks. 42 test('multiLevel', function() { 43 var snapshot = new HeapSnapshot({}, 1, [ 44 { 45 'current_allocs': 10, 46 'total_allocs': 100, 47 'current_bytes': 10000, 48 'trace': '', 49 'total_bytes': 100000 50 }, 51 { 52 'current_allocs': 2, 53 'total_allocs': 22, 54 'current_bytes': 200, 55 'trace': 'TestObject::TestMethod ', 56 'total_bytes': 2200 57 }, 58 { 59 'current_allocs': 3, 60 'total_allocs': 33, 61 'current_bytes': 300, 62 'trace': 'TestObject2::TestMethod2 ', 63 'total_bytes': 3300 64 }, 65 { 66 'current_allocs': 5, 67 'total_allocs': 55, 68 'current_bytes': 500, 69 'trace': 'TestObject2::TestMethod2 TestObject3::TestMethod3 ', 70 'total_bytes': 5500 71 } 72 ]); 73 snapshot.preInitialize(); 74 snapshot.initialize(); 75 76 // Our heap has two top-level stacks. 77 var heap = snapshot.heap_; 78 var childKeys = Object.keys(heap.children); 79 assertEquals(2, childKeys.length); 80 // Both methods exist as children. 81 assertNotEquals(-1, childKeys.indexOf('TestObject::TestMethod')); 82 assertNotEquals(-1, childKeys.indexOf('TestObject2::TestMethod2')); 83 84 // Verify the first trace entry stack. 85 var trace = heap.children['TestObject::TestMethod']; 86 assertEquals(2, trace.currentAllocs); 87 assertEquals(200, trace.currentBytes); 88 // One child for "(here)". 89 assertEquals(1, Object.keys(trace.children).length); 90 assertNotNull(trace.children['(here)']); 91 92 // Verify the second trace entry stack. 93 trace = heap.children['TestObject2::TestMethod2']; 94 // Memory should have summed up. 95 assertEquals(8, trace.currentAllocs); 96 assertEquals(800, trace.currentBytes); 97 // Two children, "(here)" and another stack. 98 assertEquals(2, Object.keys(trace.children).length); 99 assertNotNull(trace.children['TestObject3::TestMethod3']); 100 assertNotNull(trace.children['(here)']); 101 102 trace = trace.children['TestObject3::TestMethod3']; 103 assertEquals(5, trace.currentAllocs); 104 assertEquals(500, trace.currentBytes); 105 assertEquals(1, Object.keys(trace.children).length); 106 }); 107 }); 108