Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / inspector / profiler / heap-snapshot.html
1 <html>
2 <head>
3 <script src="../../http/tests/inspector/inspector-test.js"></script>
4 <script src="heap-snapshot-test.js"></script>
5 <script>
6
7 function test()
8 {
9
10     function createTestEnvironmentInWorker()
11     {
12         if (!this.InspectorTest)
13             InspectorTest = {};
14
15         InspectorTest.assertEquals = function(expected, found, message)
16         {
17             if (expected === found)
18                 return;
19
20             var error;
21             if (message)
22                 error = "Failure (" + message + "):";
23             else
24                 error = "Failure:";
25             throw new Error(error + " expected <" + expected + "> found <" + found + ">");
26         }
27     }
28
29     function runTestSuiteInWorker()
30     {
31         var testSuite = [
32             function postOrderIndexBug()
33             {
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");
45             },
46
47             function heapSnapshotNodeSimpleTest()
48             {
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");
58             },
59
60             function heapSnapshotNodeIteratorTest()
61             {
62                 var snapshot = InspectorTest.createJSHeapSnapshotMockObject();
63                 var nodeRoot = snapshot.createNode(snapshot._rootNodeIndex);
64                 var iterator = new WebInspector.HeapSnapshotNodeIterator(nodeRoot);
65                 var names = [];
66                 for (; iterator.hasNext(); iterator.next())
67                     names.push(iterator.item().name());
68                 InspectorTest.assertEquals(",A,B,C,D,E", names.join(","), "node iterator");
69             },
70
71             function heapSnapshotEdgeSimpleTest()
72             {
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");
81
82                 var edgesCount = 0;
83                 for (; edgeIterator.hasNext(); edgeIterator.next())
84                     ++edgesCount;
85                 InspectorTest.assertEquals(nodeRoot.edgesCount(), edgesCount, "edges count");
86             },
87
88             function heapSnapshotEdgeIteratorTest()
89             {
90                 var snapshot = InspectorTest.createJSHeapSnapshotMockObject();
91                 var nodeRoot = snapshot.createNode(snapshot._rootNodeIndex);
92                 var names = [];
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");
98             },
99
100             function heapSnapshotNodeAndEdgeTest()
101             {
102                 var snapshotMock = InspectorTest.createJSHeapSnapshotMockObject();
103                 var nodeRoot = snapshotMock.createNode(snapshotMock._rootNodeIndex);
104                 var names = [];
105
106                 function depthFirstTraversal(node)
107                 {
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());
112                     }
113                 }
114
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");
118
119                 // Now check against a real HeapSnapshot instance.
120                 names = [];
121                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
122                 depthFirstTraversal(snapshot.rootNode());
123                 InspectorTest.assertEquals(reference, names.join(","), "snapshot traversal");
124             },
125
126             function heapSnapshotSimpleTest()
127             {
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");
131             },
132
133             function heapSnapshotContainmentEdgeIndexesTest()
134             {
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");
141             },
142
143             function heapSnapshotPostOrderIndexTest()
144             {
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");
150             },
151
152             function heapSnapshotDominatorsTreeTest()
153             {
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");
160             },
161
162             function heapSnapshotRetainedSizeTest()
163             {
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");
170             },
171
172             function heapSnapshotLargeRetainedSize(next)
173             {
174                 var builder = new InspectorTest.HeapSnapshotBuilder();
175                 var node = builder.rootNode;
176
177                 var iterations = 6;
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);
182                     node = newNode;
183                 }
184
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.");
189             },
190
191             function heapSnapshotDominatedNodesTest()
192             {
193                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
194
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");
200
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");
206             },
207
208             function heapSnapshotPageOwnedTest(next)
209             {
210                 var builder = new InspectorTest.HeapSnapshotBuilder();
211                 var rootNode = builder.rootNode;
212
213                 var debuggerNode = new InspectorTest.HeapNode("Debugger");
214                 rootNode.linkNode(debuggerNode, InspectorTest.HeapEdge.Type.element);
215
216                 var windowNode = new InspectorTest.HeapNode("Window");
217                 rootNode.linkNode(windowNode, InspectorTest.HeapEdge.Type.shortcut);
218
219                 var pageOwnedNode = new InspectorTest.HeapNode("PageOwnedNode");
220                 windowNode.linkNode(pageOwnedNode, InspectorTest.HeapEdge.Type.element);
221                 debuggerNode.linkNode(pageOwnedNode, InspectorTest.HeapEdge.Type.property, "debugger2pageOwnedNode");
222
223                 var debuggerOwnedNode = new InspectorTest.HeapNode("debuggerOwnedNode");
224                 debuggerNode.linkNode(debuggerOwnedNode, InspectorTest.HeapEdge.Type.element);
225
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();
231
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.");
237             },
238
239             function heapSnapshotRetainersTest()
240             {
241                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
242                 var expectedRetainers = {
243                     "": [],
244                     "A": [""],
245                     "B": ["", "A"],
246                     "C": ["A", "B"],
247                     "D": ["B"],
248                     "E": ["C"]};
249                 for (var nodes = snapshot._allNodes(); nodes.hasNext(); nodes.next()) {
250                     var names = [];
251                     for (var retainers = nodes.item().retainers(); retainers.hasNext(); retainers.next())
252                         names.push(retainers.item().node().name());
253                     names.sort();
254                     InspectorTest.assertEquals(expectedRetainers[nodes.item().name()].join(","), names.join(","), "retainers of \"" + nodes.item().name() + "\"");
255                 }
256             },
257
258             function heapSnapshotAggregatesTest()
259             {
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" }
267                 };
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 + "\"");
274                 }
275                 var expectedIndexes = {
276                               // Index of corresponding node in the raw snapshot:
277                    "A": [7],  // 14
278                    "B": [14], // 27
279                    "C": [21], // 40
280                    "D": [28], // 50
281                    "E": [35]  // 57
282                 };
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 + "\"");
288                 }
289             },
290
291             function heapSnapshotFlagsTest()
292             {
293                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMockWithDOM(), new WebInspector.HeapSnapshotProgress());
294                 var expectedCanBeQueried = {
295                     "": false,
296                    "A": true,
297                    "B": true,
298                    "C": true,
299                    "D": true,
300                    "E": false,
301                    "F": false,
302                    "G": false,
303                    "H": false,
304                    "M": false,
305                    "N": false,
306                    "Window": true
307                 };
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() + "\"");
311                 }
312             },
313
314             function heapSnapshotNodesProviderTest()
315             {
316                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
317
318                 function nodeFilter(node)
319                 {
320                     return node.type() === "object" && node.name() !== "B" && node.name() !== "D";
321                 }
322
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)
334                 {
335                     return item.name;
336                 });
337                 InspectorTest.assertEquals("E,C,A", names.join(","), "nodes provider names");
338             },
339
340             function heapSnapshotEdgesProviderTest()
341             {
342                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
343
344                 function edgeFilter(edge)
345                 {
346                     return edge.name() === "b";
347                 }
348
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)
356                 {
357                     return item.name;
358                 });
359                 InspectorTest.assertEquals("b", names.join(","), "edges provider names");
360             },
361
362             function heapSnapshotLoaderTest()
363             {
364                 var source = InspectorTest.createHeapSnapshotMockRaw();
365                 var sourceStringified = JSON.stringify(source);
366                 var partSize = sourceStringified.length >> 3;
367
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));
371                 loader.close();
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)
376                 {
377                     InspectorTest.assertEquals(JSON.stringify(reference), JSON.stringify(actual));
378                 }
379                 assertSnapshotEquals(new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress(), false), result);
380             },
381         ];
382
383         var result = [];
384         for (var i = 0; i < testSuite.length; i++) {
385             var test = testSuite[i];
386             result.push("");
387             result.push("Running: " + test.name);
388             try {
389                 test();
390             } catch (e) {
391                 result.push("FAIL: " + e);
392             }
393         }
394         return result.join("\n");
395     }
396
397     var proxy = new WebInspector.HeapSnapshotWorkerProxy(function(eventName, arg)
398     {
399         InspectorTest.addResult("Unexpected event from worker: " + eventName);
400     });
401     var source = "(" + createTestEnvironmentInWorker + ")();" +
402                  "(" + InspectorTest.createHeapSnapshotMockFactories + ")();" +
403                  "(" + runTestSuiteInWorker + ")();";
404     proxy.evaluateForTest(source, function(result)
405     {
406         InspectorTest.addResult(result);
407         InspectorTest.completeTest();
408     });
409 }
410
411 </script>
412 </head>
413
414 <body onload="runTest()">
415 <p>
416 This test checks HeapSnapshots module.
417 </p>
418
419 </body>
420 </html>