summaryrefslogtreecommitdiffstats
path: root/devtools/client/performance/test/unit/test_tree-model-allocations-02.js
blob: cfc5c40484d6909e3ef53e30041bfd4579cb193b (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

/**
 * Tests that the tree model calculates correct costs/percentages for
 * allocation frame nodes. Inverted version of test_tree-model-allocations-01.js
 */

function run_test() {
  run_next_test();
}

add_task(function () {
  let { ThreadNode } = require("devtools/client/performance/modules/logic/tree-model");
  const { getProfileThreadFromAllocations } = require("devtools/shared/performance/recording-utils");
  let allocationData = getProfileThreadFromAllocations(TEST_DATA);
  let thread = new ThreadNode(allocationData, { invertTree: true, startTime: 0,
                                                endTime: 1000 });

  /* eslint-disable max-len */
  /**
   * Values are in order according to:
   * +-------------+------------+-------------+-------------+------------------------------+
   * | Self Bytes  | Self Count | Total Bytes | Total Count | Function                     |
   * +-------------+------------+-------------+-------------+------------------------------+
   * | 1790272 41% | 8307   17% | 1790372 42% | 8317    18% | V someFunc @ a.j:345:6       |
   * |     100  1% | 10      1% |     100  1% |   10     1% |   > callerFunc @ b.j:765:34  |
   * +-------------+------------+-------------+-------------+------------------------------+
   */
  /* eslint-enable max-len */
  [
    [700, 70, 1, 33, 700, 70, 1, 33, "z (C:5:6)", [
      [0, 0, 0, 0, 700, 70, 1, 33, "y (B:3:4)", [
        [0, 0, 0, 0, 700, 70, 1, 33, "x (A:1:2)"]
      ]]
    ]],
    [200, 20, 1, 33, 200, 20, 1, 33, "y (B:3:4)", [
      [0, 0, 0, 0, 200, 20, 1, 33, "x (A:1:2)"]
    ]],
    [100, 10, 1, 33, 100, 10, 1, 33, "x (A:1:2)"]
  ].forEach(compareFrameInfo(thread));
});

function compareFrameInfo(root, parent) {
  parent = parent || root;
  let fields = [
    "selfSize", "selfSizePercentage", "selfCount", "selfCountPercentage",
    "totalSize", "totalSizePercentage", "totalCount", "totalCountPercentage"
  ];

  return function (def) {
    let children;

    if (Array.isArray(def[def.length - 1])) {
      children = def.pop();
    }

    let name = def.pop();
    let expected = def;

    let node = getFrameNodePath(parent, name);
    let data = node.getInfo({ root, allocations: true });

    fields.forEach((field, i) => {
      let actual = data[field];
      if (/percentage/i.test(field)) {
        actual = Number.parseInt(actual, 10);
      }
      equal(actual, expected[i], `${name} has correct ${field}: ${expected[i]}`);
    });

    if (children) {
      children.forEach(compareFrameInfo(root, node));
    }
  };
}

var TEST_DATA = {
  sites: [0, 1, 2, 3],
  timestamps: [0, 150, 200, 250],
  sizes: [0, 100, 200, 700],
  frames: [{
    source: "(root)"
  }, {
    source: "A",
    line: 1,
    column: 2,
    functionDisplayName: "x",
    parent: 0
  }, {
    source: "B",
    line: 3,
    column: 4,
    functionDisplayName: "y",
    parent: 1
  }, {
    source: "C",
    line: 5,
    column: 6,
    functionDisplayName: "z",
    parent: 2
  }
  ]
};