summaryrefslogtreecommitdiff
path: root/chromium/third_party/trace-viewer/src/tcmalloc/heap_test.js
blob: a397c6b5214ad6134d452fce9c7c72cb7487bc21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

'use strict';

base.require('tcmalloc.heap');

base.unittest.testSuite('tcmalloc.heap', function() {
  var HeapSnapshot = tcmalloc.HeapSnapshot;

  // Tests total allocation count.
  test('totals', function() {
    var snapshot = new HeapSnapshot({}, 1, [
      {
        'current_allocs': 10,
        'total_allocs': 100,
        'current_bytes': 10000,
        'trace': '',
        'total_bytes': 100000
      },
      {
        'current_allocs': 2,
        'total_allocs': 22,
        'current_bytes': 200,
        'trace': 'TestObject::TestMethod ',
        'total_bytes': 2200
      }
    ]);
    snapshot.preInitialize();
    snapshot.initialize();

    // Base class got the timestamp.
    assertEquals(1, snapshot.ts);

    // The first entry in the list is for totals.
    assertEquals(10, snapshot.total_.currentAllocs);
    assertEquals(10000, snapshot.total_.currentBytes);
  });

  // Tests multi-level trace stacks.
  test('multiLevel', function() {
    var snapshot = new HeapSnapshot({}, 1, [
      {
        'current_allocs': 10,
        'total_allocs': 100,
        'current_bytes': 10000,
        'trace': '',
        'total_bytes': 100000
      },
      {
        'current_allocs': 2,
        'total_allocs': 22,
        'current_bytes': 200,
        'trace': 'TestObject::TestMethod ',
        'total_bytes': 2200
      },
      {
        'current_allocs': 3,
        'total_allocs': 33,
        'current_bytes': 300,
        'trace': 'TestObject2::TestMethod2  ',
        'total_bytes': 3300
      },
      {
        'current_allocs': 5,
        'total_allocs': 55,
        'current_bytes': 500,
        'trace': 'TestObject2::TestMethod2 TestObject3::TestMethod3 ',
        'total_bytes': 5500
      }
    ]);
    snapshot.preInitialize();
    snapshot.initialize();

    // Our heap has two top-level stacks.
    var heap = snapshot.heap_;
    var childKeys = Object.keys(heap.children);
    assertEquals(2, childKeys.length);
    // Both methods exist as children.
    assertNotEquals(-1, childKeys.indexOf('TestObject::TestMethod'));
    assertNotEquals(-1, childKeys.indexOf('TestObject2::TestMethod2'));

    // Verify the first trace entry stack.
    var trace = heap.children['TestObject::TestMethod'];
    assertEquals(2, trace.currentAllocs);
    assertEquals(200, trace.currentBytes);
    // One child for "(here)".
    assertEquals(1, Object.keys(trace.children).length);
    assertNotNull(trace.children['(here)']);

    // Verify the second trace entry stack.
    trace = heap.children['TestObject2::TestMethod2'];
    // Memory should have summed up.
    assertEquals(8, trace.currentAllocs);
    assertEquals(800, trace.currentBytes);
    // Two children, "(here)" and another stack.
    assertEquals(2, Object.keys(trace.children).length);
    assertNotNull(trace.children['TestObject3::TestMethod3']);
    assertNotNull(trace.children['(here)']);

    trace = trace.children['TestObject3::TestMethod3'];
    assertEquals(5, trace.currentAllocs);
    assertEquals(500, trace.currentBytes);
    assertEquals(1, Object.keys(trace.children).length);
  });
});