tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_HeapAnalyses_getCensusIndividuals_01.js (3097B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 "use strict";
      4 
      5 // Test that the HeapAnalyses{Client,Worker} can get census individuals.
      6 
      7 const COUNT = { by: "count", count: true, bytes: true };
      8 
      9 const CENSUS_BREAKDOWN = {
     10  by: "coarseType",
     11  objects: COUNT,
     12  strings: COUNT,
     13  scripts: COUNT,
     14  other: COUNT,
     15  domNode: COUNT,
     16 };
     17 
     18 const LABEL_BREAKDOWN = {
     19  by: "internalType",
     20  then: COUNT,
     21 };
     22 
     23 const MAX_INDIVIDUALS = 10;
     24 
     25 add_task(async function () {
     26  const client = new HeapAnalysesClient();
     27 
     28  const snapshotFilePath = saveNewHeapSnapshot();
     29  await client.readHeapSnapshot(snapshotFilePath);
     30  ok(true, "Should have read the heap snapshot");
     31 
     32  const dominatorTreeId = await client.computeDominatorTree(snapshotFilePath);
     33  ok(true, "Should have computed dominator tree");
     34 
     35  const { report } = await client.takeCensus(
     36    snapshotFilePath,
     37    { breakdown: CENSUS_BREAKDOWN },
     38    { asTreeNode: true }
     39  );
     40  ok(report, "Should get a report");
     41 
     42  let nodesWithLeafIndicesFound = 0;
     43 
     44  await (async function assertCanGetIndividuals(censusNode) {
     45    if (censusNode.reportLeafIndex !== undefined) {
     46      nodesWithLeafIndicesFound++;
     47 
     48      const response = await client.getCensusIndividuals({
     49        dominatorTreeId,
     50        indices: DevToolsUtils.isSet(censusNode.reportLeafIndex)
     51          ? censusNode.reportLeafIndex
     52          : new Set([censusNode.reportLeafIndex]),
     53        censusBreakdown: CENSUS_BREAKDOWN,
     54        labelBreakdown: LABEL_BREAKDOWN,
     55        maxRetainingPaths: 1,
     56        maxIndividuals: MAX_INDIVIDUALS,
     57      });
     58 
     59      dumpn(`response = ${JSON.stringify(response, null, 4)}`);
     60 
     61      equal(
     62        response.nodes.length,
     63        Math.min(MAX_INDIVIDUALS, censusNode.count),
     64        "response.nodes.length === Math.min(MAX_INDIVIDUALS, censusNode.count)"
     65      );
     66 
     67      let lastRetainedSize = Infinity;
     68      for (const individual of response.nodes) {
     69        equal(
     70          typeof individual.nodeId,
     71          "number",
     72          "individual.nodeId should be a number"
     73        );
     74        Assert.lessOrEqual(
     75          individual.retainedSize,
     76          lastRetainedSize,
     77          "individual.retainedSize <= lastRetainedSize"
     78        );
     79        lastRetainedSize = individual.retainedSize;
     80        ok(
     81          individual.shallowSize,
     82          "individual.shallowSize should exist and be non-zero"
     83        );
     84        ok(individual.shortestPaths, "individual.shortestPaths should exist");
     85        ok(
     86          individual.shortestPaths.nodes,
     87          "individual.shortestPaths.nodes should exist"
     88        );
     89        ok(
     90          individual.shortestPaths.edges,
     91          "individual.shortestPaths.edges should exist"
     92        );
     93        ok(individual.label, "individual.label should exist");
     94      }
     95    }
     96 
     97    if (censusNode.children) {
     98      for (const child of censusNode.children) {
     99        await assertCanGetIndividuals(child);
    100      }
    101    }
    102  })(report);
    103 
    104  equal(
    105    nodesWithLeafIndicesFound,
    106    4,
    107    "Should have found a leaf for each coarse type"
    108  );
    109 
    110  client.destroy();
    111 });