3 <script src=
"../../http/tests/inspector/inspector-test.js"></script>
4 <script src=
"heap-snapshot-test.js"></script>
10 function createTestEnvironmentInWorker()
12 if (!this.InspectorTest
)
15 InspectorTest
.assertEquals = function(expected
, found
, message
)
17 if (expected
=== found
)
22 error
= "Failure (" + message
+ "):";
25 throw new Error(error
+ " expected <" + expected
+ "> found <" + found
+ ">");
29 function runTestSuiteInWorker()
32 function postOrderIndexBug()
34 var builder
= new InspectorTest
.HeapSnapshotBuilder();
35 var node1
= new InspectorTest
.HeapNode("Node1");
36 var node2
= new InspectorTest
.HeapNode("Node2");
37 builder
.rootNode
.linkNode(node1
, InspectorTest
.HeapEdge
.Type
.internal);
38 builder
.rootNode
.linkNode(node2
, InspectorTest
.HeapEdge
.Type
.internal);
39 node2
.linkNode(node1
, InspectorTest
.HeapEdge
.Type
.internal);
40 var snapshot
= builder
.createJSHeapSnapshot();
41 var postOrderIndexes
= snapshot
._buildPostOrderIndex().nodeOrdinal2PostOrderIndex
;
42 var nodeOrdinals
= snapshot
._buildPostOrderIndex().postOrderIndex2NodeOrdinal
;
43 InspectorTest
.assertEquals(JSON
.stringify(new Uint32Array([2, 0, 1])), JSON
.stringify(postOrderIndexes
), "postOrderIndexes");
44 InspectorTest
.assertEquals(JSON
.stringify(new Uint32Array([1, 2, 0])), JSON
.stringify(nodeOrdinals
), "nodeOrdinals");
47 function heapSnapshotNodeSimpleTest()
49 var snapshot
= InspectorTest
.createJSHeapSnapshotMockObject();
50 var nodeRoot
= snapshot
.createNode(snapshot
._rootNodeIndex
);
51 InspectorTest
.assertEquals("", nodeRoot
.name(), "root name");
52 InspectorTest
.assertEquals("hidden", nodeRoot
.type(), "root type");
53 InspectorTest
.assertEquals(2, nodeRoot
.edgesCount(), "root edges");
54 var nodeE
= snapshot
.createNode(15);
55 InspectorTest
.assertEquals("E", nodeE
.name(), "E name");
56 InspectorTest
.assertEquals("object", nodeE
.type(), "E type");
57 InspectorTest
.assertEquals(0, nodeE
.edgesCount(), "E edges");
60 function heapSnapshotNodeIteratorTest()
62 var snapshot
= InspectorTest
.createJSHeapSnapshotMockObject();
63 var nodeRoot
= snapshot
.createNode(snapshot
._rootNodeIndex
);
64 var iterator
= new WebInspector
.HeapSnapshotNodeIterator(nodeRoot
);
66 for (; iterator
.hasNext(); iterator
.next())
67 names
.push(iterator
.item().name());
68 InspectorTest
.assertEquals(",A,B,C,D,E", names
.join(","), "node iterator");
71 function heapSnapshotEdgeSimpleTest()
73 var snapshot
= InspectorTest
.createJSHeapSnapshotMockObject();
74 var nodeRoot
= snapshot
.createNode(snapshot
._rootNodeIndex
);
75 var edgeIterator
= new WebInspector
.HeapSnapshotEdgeIterator(nodeRoot
);
76 InspectorTest
.assertEquals(true, edgeIterator
.hasNext(), "has edges");
77 var edge
= edgeIterator
.item();
78 InspectorTest
.assertEquals("shortcut", edge
.type(), "edge type");
79 InspectorTest
.assertEquals("a", edge
.name(), "edge name");
80 InspectorTest
.assertEquals("A", edge
.node().name(), "edge node name");
83 for (; edgeIterator
.hasNext(); edgeIterator
.next())
85 InspectorTest
.assertEquals(nodeRoot
.edgesCount(), edgesCount
, "edges count");
88 function heapSnapshotEdgeIteratorTest()
90 var snapshot
= InspectorTest
.createJSHeapSnapshotMockObject();
91 var nodeRoot
= snapshot
.createNode(snapshot
._rootNodeIndex
);
93 for (var iterator
= nodeRoot
.edges(); iterator
.hasNext(); iterator
.next())
94 names
.push(iterator
.item().name());
95 InspectorTest
.assertEquals("a,b", names
.join(","), "edge iterator");
96 var nodeE
= snapshot
.createNode(15);
97 InspectorTest
.assertEquals(false, nodeE
.edges().hasNext(), "empty edge iterator");
100 function heapSnapshotNodeAndEdgeTest()
102 var snapshotMock
= InspectorTest
.createJSHeapSnapshotMockObject();
103 var nodeRoot
= snapshotMock
.createNode(snapshotMock
._rootNodeIndex
);
106 function depthFirstTraversal(node
)
108 names
.push(node
.name());
109 for (var edges
= node
.edges(); edges
.hasNext(); edges
.next()) {
110 names
.push(edges
.item().name());
111 depthFirstTraversal(edges
.item().node());
115 depthFirstTraversal(nodeRoot
);
116 var reference
= ",a,A,1,B,bc,C,ce,E,bd,D,ac,C,ce,E,b,B,bc,C,ce,E,bd,D";
117 InspectorTest
.assertEquals(reference
, names
.join(","), "mock traversal");
119 // Now check against a real HeapSnapshot instance.
121 var snapshot
= new WebInspector
.JSHeapSnapshot(InspectorTest
.createHeapSnapshotMock(), new WebInspector
.HeapSnapshotProgress());
122 depthFirstTraversal(snapshot
.rootNode());
123 InspectorTest
.assertEquals(reference
, names
.join(","), "snapshot traversal");
126 function heapSnapshotSimpleTest()
128 var snapshot
= new WebInspector
.JSHeapSnapshot(InspectorTest
.createHeapSnapshotMock(), new WebInspector
.HeapSnapshotProgress());
129 InspectorTest
.assertEquals(6, snapshot
.nodeCount
, "node count");
130 InspectorTest
.assertEquals(20, snapshot
.totalSize
, "total size");
133 function heapSnapshotContainmentEdgeIndexesTest()
135 var snapshot
= new WebInspector
.JSHeapSnapshot(InspectorTest
.createHeapSnapshotMock(), new WebInspector
.HeapSnapshotProgress());
136 var actual
= snapshot
._firstEdgeIndexes
;
137 var expected
= [0, 6, 12, 18, 21, 21, 21];
138 InspectorTest
.assertEquals(expected
.length
, actual
.length
, "Edge indexes size");
139 for (var i
= 0; i
< expected
.length
; ++i
)
140 InspectorTest
.assertEquals(expected
[i
], actual
[i
], "Edge indexes");
143 function heapSnapshotPostOrderIndexTest()
145 var snapshot
= new WebInspector
.JSHeapSnapshot(InspectorTest
.createHeapSnapshotMock(), new WebInspector
.HeapSnapshotProgress());
146 var postOrderIndex2NodeOrdinal
= snapshot
._buildPostOrderIndex().postOrderIndex2NodeOrdinal
;
147 var expected
= [5,3,4,2,1,0];
148 for (var i
= 0; i
< expected
.length
; ++i
)
149 InspectorTest
.assertEquals(expected
[i
], postOrderIndex2NodeOrdinal
[i
], "Post ordered indexes");
152 function heapSnapshotDominatorsTreeTest()
154 var snapshot
= new WebInspector
.JSHeapSnapshot(InspectorTest
.createHeapSnapshotMock(), new WebInspector
.HeapSnapshotProgress());
155 var result
= snapshot
._buildPostOrderIndex();
156 var dominatorsTree
= snapshot
._buildDominatorTree(result
.postOrderIndex2NodeOrdinal
, result
.nodeOrdinal2PostOrderIndex
);
157 var expected
= [0, 0, 0, 0, 2, 3];
158 for (var i
= 0; i
< expected
.length
; ++i
)
159 InspectorTest
.assertEquals(expected
[i
], dominatorsTree
[i
], "Dominators Tree");
162 function heapSnapshotRetainedSizeTest()
164 var snapshot
= new WebInspector
.JSHeapSnapshot(InspectorTest
.createHeapSnapshotMock(), new WebInspector
.HeapSnapshotProgress());
165 var actualRetainedSizes
= new Array(snapshot
.nodeCount
);
166 for (var nodeOrdinal
= 0; nodeOrdinal
< snapshot
.nodeCount
; ++nodeOrdinal
)
167 actualRetainedSizes
[nodeOrdinal
] = snapshot
._retainedSizes
[nodeOrdinal
];
168 var expectedRetainedSizes
= [20, 2, 8, 10, 5, 6];
169 InspectorTest
.assertEquals(JSON
.stringify(expectedRetainedSizes
), JSON
.stringify(actualRetainedSizes
), "Retained sizes");
172 function heapSnapshotLargeRetainedSize(next
)
174 var builder
= new InspectorTest
.HeapSnapshotBuilder();
175 var node
= builder
.rootNode
;
178 var nodeSize
= 1000 * 1000 * 1000;
179 for (var i
= 0; i
< 6; i
++) {
180 var newNode
= new InspectorTest
.HeapNode("Node" + i
, nodeSize
);
181 node
.linkNode(newNode
, InspectorTest
.HeapEdge
.Type
.element
);
185 var snapshot
= builder
.createJSHeapSnapshot();
186 InspectorTest
.assertEquals(
187 iterations
* nodeSize
, snapshot
.rootNode().retainedSize(),
188 "Ensure that root node retained size supports values exceeding 2^32 bytes.");
191 function heapSnapshotDominatedNodesTest()
193 var snapshot
= new WebInspector
.JSHeapSnapshot(InspectorTest
.createHeapSnapshotMock(), new WebInspector
.HeapSnapshotProgress());
195 var expectedDominatedNodes
= [21, 14, 7, 28, 35];
196 var actualDominatedNodes
= snapshot
._dominatedNodes
;
197 InspectorTest
.assertEquals(expectedDominatedNodes
.length
, actualDominatedNodes
.length
, "Dominated Nodes length");
198 for (var i
= 0; i
< expectedDominatedNodes
.length
; ++i
)
199 InspectorTest
.assertEquals(expectedDominatedNodes
[i
], actualDominatedNodes
[i
], "Dominated Nodes");
201 var expectedDominatedNodeIndex
= [0, 3, 3, 4, 5, 5, 5];
202 var actualDominatedNodeIndex
= snapshot
._firstDominatedNodeIndex
;
203 InspectorTest
.assertEquals(expectedDominatedNodeIndex
.length
, actualDominatedNodeIndex
.length
, "Dominated Nodes Index length");
204 for (var i
= 0; i
< expectedDominatedNodeIndex
.length
; ++i
)
205 InspectorTest
.assertEquals(expectedDominatedNodeIndex
[i
], actualDominatedNodeIndex
[i
], "Dominated Nodes Index");
208 function heapSnapshotPageOwnedTest(next
)
210 var builder
= new InspectorTest
.HeapSnapshotBuilder();
211 var rootNode
= builder
.rootNode
;
213 var debuggerNode
= new InspectorTest
.HeapNode("Debugger");
214 rootNode
.linkNode(debuggerNode
, InspectorTest
.HeapEdge
.Type
.element
);
216 var windowNode
= new InspectorTest
.HeapNode("Window");
217 rootNode
.linkNode(windowNode
, InspectorTest
.HeapEdge
.Type
.shortcut
);
219 var pageOwnedNode
= new InspectorTest
.HeapNode("PageOwnedNode");
220 windowNode
.linkNode(pageOwnedNode
, InspectorTest
.HeapEdge
.Type
.element
);
221 debuggerNode
.linkNode(pageOwnedNode
, InspectorTest
.HeapEdge
.Type
.property
, "debugger2pageOwnedNode");
223 var debuggerOwnedNode
= new InspectorTest
.HeapNode("debuggerOwnedNode");
224 debuggerNode
.linkNode(debuggerOwnedNode
, InspectorTest
.HeapEdge
.Type
.element
);
226 var snapshot
= builder
.createJSHeapSnapshot();
227 snapshot
._flags
= new Array(snapshot
.nodeCount
);
228 for (var i
= 0; i
< snapshot
.nodeCount
; ++i
)
229 snapshot
._flags
[i
] = 0;
230 snapshot
._markPageOwnedNodes();
232 var expectedFlags
= [0, 0, 4, 4, 0];
233 InspectorTest
.assertEquals(
234 JSON
.stringify(expectedFlags
),
235 JSON
.stringify(snapshot
._flags
),
236 "We are expecting that only window(third element) and PageOwnedNode(forth element) have flag === 4.");
239 function heapSnapshotRetainersTest()
241 var snapshot
= new WebInspector
.JSHeapSnapshot(InspectorTest
.createHeapSnapshotMock(), new WebInspector
.HeapSnapshotProgress());
242 var expectedRetainers
= {
249 for (var nodes
= snapshot
._allNodes(); nodes
.hasNext(); nodes
.next()) {
251 for (var retainers
= nodes
.item().retainers(); retainers
.hasNext(); retainers
.next())
252 names
.push(retainers
.item().node().name());
254 InspectorTest
.assertEquals(expectedRetainers
[nodes
.item().name()].join(","), names
.join(","), "retainers of \"" + nodes
.item().name() + "\"");
258 function heapSnapshotAggregatesTest()
260 var snapshot
= new WebInspector
.JSHeapSnapshot(InspectorTest
.createHeapSnapshotMock(), new WebInspector
.HeapSnapshotProgress());
261 var expectedAggregates
= {
262 "A": { count
: 1, self
: 2, maxRet
: 2, type
: "object", name
: "A" },
263 "B": { count
: 1, self
: 3, maxRet
: 8, type
: "object", name
: "B" },
264 "C": { count
: 1, self
: 4, maxRet
: 10, type
: "object", name
: "C" },
265 "D": { count
: 1, self
: 5, maxRet
: 5, type
: "object", name
: "D" },
266 "E": { count
: 1, self
: 6, maxRet
: 6, type
: "object", name
: "E" }
268 var aggregates
= snapshot
.aggregates(false);
269 for (var name
in aggregates
) {
270 var aggregate
= aggregates
[name
];
271 var expectedAggregate
= expectedAggregates
[name
];
272 for (var parameter
in expectedAggregate
)
273 InspectorTest
.assertEquals(expectedAggregate
[parameter
], aggregate
[parameter
], "parameter " + parameter
+ " of \"" + name
+ "\"");
275 var expectedIndexes
= {
276 // Index of corresponding node in the raw snapshot:
283 var indexes
= snapshot
.aggregates(true);
284 for (var name
in aggregates
) {
285 var aggregate
= aggregates
[name
];
286 var expectedIndex
= expectedIndexes
[name
];
287 InspectorTest
.assertEquals(expectedIndex
.join(","), aggregate
.idxs
.join(","), "indexes of \"" + name
+ "\"");
291 function heapSnapshotFlagsTest()
293 var snapshot
= new WebInspector
.JSHeapSnapshot(InspectorTest
.createHeapSnapshotMockWithDOM(), new WebInspector
.HeapSnapshotProgress());
294 var expectedCanBeQueried
= {
308 for (var nodes
= snapshot
._allNodes(); nodes
.hasNext(); nodes
.next()) {
309 var node
= nodes
.item();
310 InspectorTest
.assertEquals(expectedCanBeQueried
[node
.name()], node
.canBeQueried(), "canBeQueried of \"" + node
.name() + "\"");
314 function heapSnapshotNodesProviderTest()
316 var snapshot
= new WebInspector
.JSHeapSnapshot(InspectorTest
.createHeapSnapshotMock(), new WebInspector
.HeapSnapshotProgress());
318 function nodeFilter(node
)
320 return node
.type() === "object" && node
.name() !== "B" && node
.name() !== "D";
323 var allNodeIndexes
= [];
324 for (var i
= 0; i
< snapshot
.nodes
.length
; i
+= snapshot
._nodeFieldCount
)
325 allNodeIndexes
.push(i
);
326 var provider
= new WebInspector
.HeapSnapshotNodesProvider(snapshot
, nodeFilter
, allNodeIndexes
);
327 // Sort by names in reverse order.
328 provider
.sortAndRewind({fieldName1
: "name", ascending1
: false, fieldName2
: "id", ascending2
: false});
329 var range
= provider
.serializeItemsRange(0, 3);
330 InspectorTest
.assertEquals(3, range
.totalLength
, "Node range total length");
331 InspectorTest
.assertEquals(0, range
.startPosition
, "Node range start position");
332 InspectorTest
.assertEquals(3, range
.endPosition
, "Node range end position");
333 var names
= range
.items
.map(function(item
)
337 InspectorTest
.assertEquals("E,C,A", names
.join(","), "nodes provider names");
340 function heapSnapshotEdgesProviderTest()
342 var snapshot
= new WebInspector
.JSHeapSnapshot(InspectorTest
.createHeapSnapshotMock(), new WebInspector
.HeapSnapshotProgress());
344 function edgeFilter(edge
)
346 return edge
.name() === "b";
349 var provider
= snapshot
.createEdgesProviderForTest(snapshot
.rootNodeIndex
, edgeFilter
);
350 provider
.sortAndRewind({fieldName1
: "!edgeName", ascending1
: false, fieldName2
: "id", ascending2
: false});
351 var range
= provider
.serializeItemsRange(0, 10);
352 InspectorTest
.assertEquals(1, range
.totalLength
, "Edge range total length");
353 InspectorTest
.assertEquals(0, range
.startPosition
, "Edge range start position");
354 InspectorTest
.assertEquals(1, range
.endPosition
, "Edge range end position");
355 var names
= range
.items
.map(function(item
)
359 InspectorTest
.assertEquals("b", names
.join(","), "edges provider names");
362 function heapSnapshotLoaderTest()
364 var source
= InspectorTest
.createHeapSnapshotMockRaw();
365 var sourceStringified
= JSON
.stringify(source
);
366 var partSize
= sourceStringified
.length
>> 3;
368 var loader
= new WebInspector
.HeapSnapshotLoader();
369 for (var i
= 0, l
= sourceStringified
.length
; i
< l
; i
+= partSize
)
370 loader
.write(sourceStringified
.slice(i
, i
+ partSize
));
372 var result
= loader
.buildSnapshot(false);
373 result
.nodes
= new Uint32Array(result
.nodes
);
374 result
.containmentEdges
= new Uint32Array(result
.containmentEdges
);
375 function assertSnapshotEquals(reference
, actual
)
377 InspectorTest
.assertEquals(JSON
.stringify(reference
), JSON
.stringify(actual
));
379 assertSnapshotEquals(new WebInspector
.JSHeapSnapshot(InspectorTest
.createHeapSnapshotMock(), new WebInspector
.HeapSnapshotProgress(), false), result
);
384 for (var i
= 0; i
< testSuite
.length
; i
++) {
385 var test
= testSuite
[i
];
387 result
.push("Running: " + test
.name
);
391 result
.push("FAIL: " + e
);
394 return result
.join("\n");
397 var proxy
= new WebInspector
.HeapSnapshotWorkerProxy(function(eventName
, arg
)
399 InspectorTest
.addResult("Unexpected event from worker: " + eventName
);
401 var source
= "(" + createTestEnvironmentInWorker
+ ")();" +
402 "(" + InspectorTest
.createHeapSnapshotMockFactories
+ ")();" +
403 "(" + runTestSuiteInWorker
+ ")();";
404 proxy
.evaluateForTest(source
, function(result
)
406 InspectorTest
.addResult(result
);
407 InspectorTest
.completeTest();
414 <body onload=
"runTest()">
416 This test checks HeapSnapshots module.