summaryrefslogtreecommitdiffstats
path: root/devtools/client/memory/test/xpcshell/test_utils-get-snapshot-totals.js
blob: 46879af77dacbe227117725249bddebd63e9cea9 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Tests that we use the correct snapshot aggregate value
 * in `utils.getSnapshotTotals(snapshot)`
 */

const {
  censusDisplays,
  viewState,
  censusState,
} = require("resource://devtools/client/memory/constants.js");
const {
  getSnapshotTotals,
} = require("resource://devtools/client/memory/utils.js");
const {
  takeSnapshotAndCensus,
} = require("resource://devtools/client/memory/actions/snapshot.js");
const {
  setCensusDisplayAndRefresh,
} = require("resource://devtools/client/memory/actions/census-display.js");
const {
  changeView,
} = require("resource://devtools/client/memory/actions/view.js");

add_task(async function() {
  const front = new StubbedMemoryFront();
  const heapWorker = new HeapAnalysesClient();
  await front.attach();
  const store = Store();
  const { getState, dispatch } = store;

  dispatch(changeView(viewState.CENSUS));

  await dispatch(
    setCensusDisplayAndRefresh(heapWorker, censusDisplays.allocationStack)
  );

  dispatch(takeSnapshotAndCensus(front, heapWorker));
  await waitUntilCensusState(store, s => s.census, [censusState.SAVED]);

  ok(
    !getState().snapshots[0].census.display.inverted,
    "Snapshot is not inverted"
  );

  const census = getState().snapshots[0].census;
  let result = aggregate(census.report);
  const totalBytes = result.bytes;
  const totalCount = result.count;

  ok(totalBytes > 0, "counted up bytes in the census");
  ok(totalCount > 0, "counted up count in the census");

  result = getSnapshotTotals(getState().snapshots[0].census);
  equal(
    totalBytes,
    result.bytes,
    "getSnapshotTotals reuslted in correct bytes"
  );
  equal(
    totalCount,
    result.count,
    "getSnapshotTotals reuslted in correct count"
  );

  dispatch(
    setCensusDisplayAndRefresh(
      heapWorker,
      censusDisplays.invertedAllocationStack
    )
  );

  await waitUntilCensusState(store, s => s.census, [censusState.SAVING]);
  await waitUntilCensusState(store, s => s.census, [censusState.SAVED]);
  ok(getState().snapshots[0].census.display.inverted, "Snapshot is inverted");

  result = getSnapshotTotals(getState().snapshots[0].census);
  equal(
    totalBytes,
    result.bytes,
    "getSnapshotTotals reuslted in correct bytes when inverted"
  );
  equal(
    totalCount,
    result.count,
    "getSnapshotTotals reuslted in correct count when inverted"
  );
});

function aggregate(report) {
  let totalBytes = report.bytes;
  let totalCount = report.count;
  for (const child of report.children || []) {
    const { bytes, count } = aggregate(child);
    totalBytes += bytes;
    totalCount += count;
  }
  return { bytes: totalBytes, count: totalCount };
}