test_HeapSnapshot_computeShortestPaths_01.js (2714B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 "use strict"; 4 5 // Sanity test that we can compute shortest paths. 6 // 7 // Because the actual heap graph is too unpredictable and likely to drastically 8 // change as various implementation bits change, we don't test exact paths 9 // here. See js/src/jsapi-tests/testUbiNode.cpp for such tests, where we can 10 // control the specific graph shape and structure and so testing exact paths is 11 // reliable. 12 13 function run_test() { 14 const path = ChromeUtils.saveHeapSnapshot({ runtime: true }); 15 const snapshot = ChromeUtils.readHeapSnapshot(path); 16 17 const dominatorTree = snapshot.computeDominatorTree(); 18 const dominatedByRoot = dominatorTree 19 .getImmediatelyDominated(dominatorTree.root) 20 .slice(0, 10); 21 ok(dominatedByRoot); 22 ok(dominatedByRoot.length); 23 24 const targetSet = new Set(dominatedByRoot); 25 26 const shortestPaths = snapshot.computeShortestPaths( 27 dominatorTree.root, 28 dominatedByRoot, 29 2 30 ); 31 ok(shortestPaths); 32 ok(shortestPaths instanceof Map); 33 Assert.strictEqual(shortestPaths.size, targetSet.size); 34 35 for (const [target, paths] of shortestPaths) { 36 ok(targetSet.has(target), "We should only get paths for our targets"); 37 targetSet.delete(target); 38 39 ok( 40 !!paths.length, 41 "We must have at least one path, since the target is dominated by the root" 42 ); 43 Assert.lessOrEqual( 44 paths.length, 45 2, 46 "Should not have recorded more paths than the max requested" 47 ); 48 49 dumpn("---------------------"); 50 dumpn("Shortest paths for 0x" + target.toString(16) + ":"); 51 for (const pth of paths) { 52 dumpn(" path ="); 53 for (const part of pth) { 54 dumpn( 55 " predecessor: 0x" + 56 part.predecessor.toString(16) + 57 "; edge: " + 58 part.edge 59 ); 60 } 61 } 62 dumpn("---------------------"); 63 64 for (const path2 of paths) { 65 ok(!!path2.length, "Cannot have zero length paths"); 66 Assert.strictEqual( 67 path2[0].predecessor, 68 dominatorTree.root, 69 "The first predecessor is always our start node" 70 ); 71 72 for (const part of path2) { 73 ok(part.predecessor, "Each part of a path has a predecessor"); 74 ok( 75 !!snapshot.describeNode( 76 { by: "count", count: true, bytes: true }, 77 part.predecessor 78 ), 79 "The predecessor is in the heap snapshot" 80 ); 81 ok("edge" in part, "Each part has an (potentially null) edge property"); 82 } 83 } 84 } 85 86 Assert.strictEqual( 87 targetSet.size, 88 0, 89 "We found paths for all of our targets" 90 ); 91 92 do_test_finished(); 93 }