test_HeapAnalyses_takeCensus_06.js (2956B)
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 take censuses by 6 // "allocationStack" and return a CensusTreeNode. 7 8 const BREAKDOWN = { 9 by: "objectClass", 10 then: { 11 by: "allocationStack", 12 then: { by: "count", count: true, bytes: true }, 13 noStack: { by: "count", count: true, bytes: true }, 14 }, 15 other: { by: "count", count: true, bytes: true }, 16 }; 17 18 add_task(async function () { 19 const g = newGlobal(); 20 const dbg = new Debugger(g); 21 22 // 5 allocation markers with no stack. 23 g.eval(` 24 this.markers = []; 25 for (var i = 0; i < 5; i++) { 26 markers.push(allocationMarker()); 27 } 28 `); 29 30 dbg.memory.allocationSamplingProbability = 1; 31 dbg.memory.trackingAllocationSites = true; 32 33 // 5 allocation markers at 5 stacks. 34 g.eval(` 35 (function shouldHaveCountOfOne() { 36 markers.push(allocationMarker()); 37 markers.push(allocationMarker()); 38 markers.push(allocationMarker()); 39 markers.push(allocationMarker()); 40 markers.push(allocationMarker()); 41 }()); 42 `); 43 44 // 5 allocation markers at 1 stack. 45 g.eval(` 46 (function shouldHaveCountOfFive() { 47 for (var i = 0; i < 5; i++) { 48 markers.push(allocationMarker()); 49 } 50 }()); 51 `); 52 53 const snapshotFilePath = saveNewHeapSnapshot({ debugger: dbg }); 54 55 const client = new HeapAnalysesClient(); 56 await client.readHeapSnapshot(snapshotFilePath); 57 ok(true, "Should have read the heap snapshot"); 58 59 const { report } = await client.takeCensus(snapshotFilePath, { 60 breakdown: BREAKDOWN, 61 }); 62 63 const { report: treeNode } = await client.takeCensus( 64 snapshotFilePath, 65 { 66 breakdown: BREAKDOWN, 67 }, 68 { 69 asTreeNode: true, 70 } 71 ); 72 73 const markers = treeNode.children.find(c => c.name === "AllocationMarker"); 74 ok(markers); 75 76 const noStack = markers.children.find(c => c.name === "noStack"); 77 equal(noStack.count, 5); 78 79 let numShouldHaveFiveFound = 0; 80 let numShouldHaveOneFound = 0; 81 82 function walk(node) { 83 if (node.children) { 84 node.children.forEach(walk); 85 } 86 87 if (!isSavedFrame(node.name)) { 88 return; 89 } 90 91 if (node.name.functionDisplayName === "shouldHaveCountOfFive") { 92 equal(node.count, 5, "shouldHaveCountOfFive should have count of five"); 93 numShouldHaveFiveFound++; 94 } 95 96 if (node.name.functionDisplayName === "shouldHaveCountOfOne") { 97 equal(node.count, 1, "shouldHaveCountOfOne should have count of one"); 98 numShouldHaveOneFound++; 99 } 100 } 101 markers.children.forEach(walk); 102 103 equal(numShouldHaveFiveFound, 1); 104 equal(numShouldHaveOneFound, 5); 105 106 compareCensusViewData( 107 BREAKDOWN, 108 report, 109 treeNode, 110 "Returning census as a tree node represents same data as the report" 111 ); 112 113 client.destroy(); 114 });