Upstream version 5.34.104.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     WebInspector.showPanel("profiles");
10
11     function createTestEnvironmentInWorker()
12     {
13         if (!this.InspectorTest)
14             InspectorTest = {};
15
16         InspectorTest.assertEquals = function(expected, found, message)
17         {
18             if (expected === found)
19                 return;
20
21             var error;
22             if (message)
23                 error = "Failure (" + message + "):";
24             else
25                 error = "Failure:";
26             throw new Error(error + " expected <" + expected + "> found <" + found + ">");
27         }
28     }
29
30     function runTestSuiteInWorker()
31     {
32         var testSuite = [
33             function heapSnapshotNodeSimpleTest()
34             {
35                 var snapshot = InspectorTest.createJSHeapSnapshotMockObject();
36                 var nodeRoot = snapshot.createNode(snapshot._rootNodeIndex);
37                 InspectorTest.assertEquals("", nodeRoot.name(), "root name");
38                 InspectorTest.assertEquals("hidden", nodeRoot.type(), "root type");
39                 InspectorTest.assertEquals(2, nodeRoot.edgesCount(), "root edges");
40                 var nodeE = snapshot.createNode(15);
41                 InspectorTest.assertEquals("E", nodeE.name(), "E name");
42                 InspectorTest.assertEquals("object", nodeE.type(), "E type");
43                 InspectorTest.assertEquals(0, nodeE.edgesCount(), "E edges");
44             },
45
46             function heapSnapshotNodeIteratorTest()
47             {
48                 var snapshot = InspectorTest.createJSHeapSnapshotMockObject();
49                 var nodeRoot = snapshot.createNode(snapshot._rootNodeIndex);
50                 var iterator = new WebInspector.HeapSnapshotNodeIterator(nodeRoot);
51                 var names = [];
52                 for (iterator.rewind(); iterator.hasNext(); iterator.next())
53                     names.push(iterator.item().name());
54                 InspectorTest.assertEquals(",A,B,C,D,E", names.join(","), "node iterator");
55             },
56
57             function heapSnapshotEdgeSimpleTest()
58             {
59                 var snapshot = InspectorTest.createJSHeapSnapshotMockObject();
60                 var nodeRoot = snapshot.createNode(snapshot._rootNodeIndex);
61                 var edgesRoot = nodeRoot.rawEdges();
62                 InspectorTest.assertEquals(nodeRoot.edgesCount() * snapshot._edgeFieldsCount, edgesRoot.length, "rawEdges length");
63                 var edge = snapshot.createEdge(edgesRoot);
64                 InspectorTest.assertEquals("shortcut", edge.type(), "edge type");
65                 InspectorTest.assertEquals("a", edge.name(), "edge name");
66                 InspectorTest.assertEquals("A", edge.node().name(), "edge node name");
67             },
68
69             function heapSnapshotEdgeIteratorTest()
70             {
71                 var snapshot = InspectorTest.createJSHeapSnapshotMockObject();
72                 var nodeRoot = snapshot.createNode(snapshot._rootNodeIndex);
73                 var names = [];
74                 var iterator = nodeRoot.edges();
75                 for (iterator.rewind(); iterator.hasNext(); iterator.next())
76                     names.push(iterator.item().name());
77                 InspectorTest.assertEquals("a,b", names.join(","), "edge iterator");
78                 var nodeE = snapshot.createNode(15);
79                 InspectorTest.assertEquals(false, nodeE.edges().hasNext(), "empty edge iterator");
80             },
81
82             function heapSnapshotNodeAndEdgeTest()
83             {
84                 var snapshotMock = InspectorTest.createJSHeapSnapshotMockObject();
85                 var nodeRoot = snapshotMock.createNode(snapshotMock._rootNodeIndex);
86                 var names = [];
87
88                 function depthFirstTraversal(node)
89                 {
90                     names.push(node.name());
91                     for (var edges = node.edges(); edges.hasNext(); edges.next()) {
92                         names.push(edges.item().name());
93                         depthFirstTraversal(edges.item().node());
94                     }
95                 }
96
97                 depthFirstTraversal(nodeRoot);
98                 var reference = ",a,A,1,B,bc,C,ce,E,bd,D,ac,C,ce,E,b,B,bc,C,ce,E,bd,D";
99                 InspectorTest.assertEquals(reference, names.join(","), "mock traversal");
100
101                 // Now check against a real HeapSnapshot instance.
102                 names = [];
103                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
104                 depthFirstTraversal(snapshot.rootNode());
105                 InspectorTest.assertEquals(reference, names.join(","), "snapshot traversal");
106             },
107
108             function heapSnapshotSimpleTest()
109             {
110                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
111                 InspectorTest.assertEquals(6, snapshot.nodeCount, "node count");
112                 InspectorTest.assertEquals(20, snapshot.totalSize, "total size");
113             },
114
115             function heapSnapshotContainmentEdgeIndexesTest()
116             {
117                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
118                 var actual = snapshot._firstEdgeIndexes;
119                 var expected = [0, 6, 12, 18, 21, 21, 21];
120                 InspectorTest.assertEquals(expected.length, actual.length, "Edge indexes size");
121                 for (var i = 0; i < expected.length; ++i)
122                     InspectorTest.assertEquals(expected[i], actual[i], "Edge indexes");
123             },
124
125             function heapSnapshotPostOrderIndexTest()
126             {
127                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
128                 var postOrderIndex2NodeOrdinal = snapshot._buildPostOrderIndex().postOrderIndex2NodeOrdinal;
129                 var expected = [4,5,3,2,1,0];
130                 for (var i = 0; i < expected.length; ++i)
131                     InspectorTest.assertEquals(expected[i], postOrderIndex2NodeOrdinal[i], "Post ordered indexes");
132             },
133
134             function heapSnapshotDominatorsTreeTest()
135             {
136                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
137                 var result = snapshot._buildPostOrderIndex();
138                 var dominatorsTree = snapshot._buildDominatorTree(result.postOrderIndex2NodeOrdinal, result.nodeOrdinal2PostOrderIndex);
139                 var expected = [0, 0, 0, 0, 2, 3];
140                 for (var i = 0; i < expected.length; ++i)
141                     InspectorTest.assertEquals(expected[i], dominatorsTree[i], "Dominators Tree");
142             },
143
144             function heapSnapshotRetainedSizeTest()
145             {
146                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
147                 var actualRetainedSizes = new Array(snapshot.nodeCount);
148                 for (var nodeOrdinal = 0; nodeOrdinal < snapshot.nodeCount; ++nodeOrdinal)
149                     actualRetainedSizes[nodeOrdinal] = snapshot._retainedSizes[nodeOrdinal];
150                 var expectedRetainedSizes = [20, 2, 8, 10, 5, 6];
151                 InspectorTest.assertEquals(JSON.stringify(expectedRetainedSizes), JSON.stringify(actualRetainedSizes), "Retained sizes");
152             },
153
154             function heapSnapshotLargeRetainedSize(next)
155             {
156                 var builder = new InspectorTest.HeapSnapshotBuilder();
157                 var node = builder.rootNode;
158
159                 var iterations = 6;
160                 var nodeSize = 1000 * 1000 * 1000;
161                 for (var i = 0; i < 6; i++) {
162                     var newNode = new InspectorTest.HeapNode("Node" + i, nodeSize);
163                     node.linkNode(newNode, InspectorTest.HeapEdge.Type.element);
164                     node = newNode;
165                 }
166
167                 var snapshot = new WebInspector.JSHeapSnapshot(builder.generateSnapshot(), new WebInspector.HeapSnapshotProgress());
168                 InspectorTest.assertEquals(
169                     iterations * nodeSize, snapshot.rootNode().retainedSize(),
170                     "Ensure that root node retained size supports values exceeding 2^32 bytes.");
171             },
172
173             function heapSnapshotDominatedNodesTest()
174             {
175                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
176
177                 var expectedDominatedNodes = [21, 14, 7, 28, 35];
178                 var actualDominatedNodes = snapshot._dominatedNodes;
179                 InspectorTest.assertEquals(expectedDominatedNodes.length, actualDominatedNodes.length, "Dominated Nodes length");
180                 for (var i = 0; i < expectedDominatedNodes.length; ++i)
181                     InspectorTest.assertEquals(expectedDominatedNodes[i], actualDominatedNodes[i], "Dominated Nodes");
182
183                 var expectedDominatedNodeIndex = [0, 3, 3, 4, 5, 5, 5];
184                 var actualDominatedNodeIndex = snapshot._firstDominatedNodeIndex;
185                 InspectorTest.assertEquals(expectedDominatedNodeIndex.length, actualDominatedNodeIndex.length, "Dominated Nodes Index length");
186                 for (var i = 0; i < expectedDominatedNodeIndex.length; ++i)
187                     InspectorTest.assertEquals(expectedDominatedNodeIndex[i], actualDominatedNodeIndex[i], "Dominated Nodes Index");
188             },
189
190             function heapSnapshotPageOwnedTest(next)
191             {
192                 var builder = new InspectorTest.HeapSnapshotBuilder();
193                 var rootNode = builder.rootNode;
194
195                 var debuggerNode = new InspectorTest.HeapNode("Debugger");
196                 rootNode.linkNode(debuggerNode, InspectorTest.HeapEdge.Type.element);
197
198                 var windowNode = new InspectorTest.HeapNode("Window");
199                 rootNode.linkNode(windowNode, InspectorTest.HeapEdge.Type.shortcut);
200
201                 var pageOwnedNode = new InspectorTest.HeapNode("PageOwnedNode");
202                 windowNode.linkNode(pageOwnedNode, InspectorTest.HeapEdge.Type.element);
203                 debuggerNode.linkNode(pageOwnedNode, InspectorTest.HeapEdge.Type.property, "debugger2pageOwnedNode");
204
205                 var debuggerOwnedNode = new InspectorTest.HeapNode("debuggerOwnedNode");
206                 debuggerNode.linkNode(debuggerOwnedNode, InspectorTest.HeapEdge.Type.element);
207
208                 var snapshot = new WebInspector.JSHeapSnapshot(builder.generateSnapshot(), new WebInspector.HeapSnapshotProgress());
209                 snapshot._flags = new Array(snapshot.nodeCount);
210                 for (var i = 0; i < snapshot.nodeCount; ++i)
211                     snapshot._flags[i] = 0;
212                 snapshot._markPageOwnedNodes();
213
214                 var expectedFlags = [0, 0, 4, 4, 0];
215                 InspectorTest.assertEquals(
216                     JSON.stringify(expectedFlags),
217                     JSON.stringify(snapshot._flags),
218                     "We are expecting that only window(third element) and PageOwnedNode(forth element) have flag === 4.");
219             },
220
221             function heapSnapshotRetainersTest()
222             {
223                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
224                 var expectedRetainers = {
225                     "": [],
226                     "A": [""],
227                     "B": ["", "A"],
228                     "C": ["A", "B"],
229                     "D": ["B"],
230                     "E": ["C"]};
231                 for (var nodes = snapshot._allNodes(); nodes.hasNext(); nodes.next()) {
232                     var names = [];
233                     for (var retainers = nodes.item().retainers(); retainers.hasNext(); retainers.next())
234                         names.push(retainers.item().node().name());
235                     names.sort();
236                     InspectorTest.assertEquals(expectedRetainers[nodes.item().name()].join(","), names.join(","), "retainers of \"" + nodes.item().name() + "\"");
237                 }
238             },
239
240             function heapSnapshotAggregatesTest()
241             {
242                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
243                 var expectedAggregates = {
244                     "A": { count: 1, self: 2, maxRet: 2, type: "object", name: "A" },
245                     "B": { count: 1, self: 3, maxRet: 8, type: "object", name: "B" },
246                     "C": { count: 1, self: 4, maxRet: 10, type: "object", name: "C" },
247                     "D": { count: 1, self: 5, maxRet: 5, type: "object", name: "D" },
248                     "E": { count: 1, self: 6, maxRet: 6, type: "object", name: "E" }
249                 };
250                 var aggregates = snapshot.aggregates(false);
251                 for (var name in aggregates) {
252                     var aggregate = aggregates[name];
253                     var expectedAggregate = expectedAggregates[name];
254                     for (var parameter in expectedAggregate)
255                         InspectorTest.assertEquals(expectedAggregate[parameter], aggregate[parameter], "parameter " + parameter + " of \"" + name + "\"");
256                 }
257                 var expectedIndexes = {
258                               // Index of corresponding node in the raw snapshot:
259                    "A": [7],  // 14
260                    "B": [14], // 27
261                    "C": [21], // 40
262                    "D": [28], // 50
263                    "E": [35]  // 57
264                 };
265                 var indexes = snapshot.aggregates(true);
266                 for (var name in aggregates) {
267                     var aggregate = aggregates[name];
268                     var expectedIndex = expectedIndexes[name];
269                     InspectorTest.assertEquals(expectedIndex.join(","), aggregate.idxs.join(","), "indexes of \"" + name + "\"");
270                 }
271             },
272
273             function heapSnapshotFlagsTest()
274             {
275                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMockWithDOM(), new WebInspector.HeapSnapshotProgress());
276                 var expectedCanBeQueried = {
277                     "": false,
278                    "A": true,
279                    "B": true,
280                    "C": true,
281                    "D": true,
282                    "E": false,
283                    "F": false,
284                    "G": false,
285                    "H": false,
286                    "M": false,
287                    "N": false,
288                    "Window": true
289                 };
290                 for (var nodes = snapshot._allNodes(); nodes.hasNext(); nodes.next()) {
291                     var node = nodes.item();
292                     InspectorTest.assertEquals(expectedCanBeQueried[node.name()], node.canBeQueried(), "canBeQueried of \"" + node.name() + "\"");
293                 }
294             },
295
296             function heapSnapshotArraySliceTest()
297             {
298                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
299                 var root = snapshot.rootNode();
300                 var rawEdges = root.rawEdges();
301                 InspectorTest.assertEquals(6, rawEdges.length);
302                 InspectorTest.assertEquals(6, rawEdges.slice(0).length);
303                 InspectorTest.assertEquals(3, rawEdges.slice(3).length);
304                 InspectorTest.assertEquals(3, rawEdges.slice(3, 6).length);
305                 InspectorTest.assertEquals(3, rawEdges.slice(0, 3).length);
306                 InspectorTest.assertEquals(0, rawEdges.slice(3, 3).length);
307             },
308
309             function heapSnapshotNodesProviderTest()
310             {
311                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
312
313                 function nodeFilter(node)
314                 {
315                     return node.type() === "object" && node.name() !== "B" && node.name() !== "D";
316                 }
317
318                 var provider = new WebInspector.HeapSnapshotNodesProvider(snapshot, nodeFilter);
319                 // Sort by names in reverse order.
320                 InspectorTest.assertEquals(3, provider.length, "nodes provider length");
321                 provider.sort({fieldName1: "name", ascending1: false, fieldName2: "id", ascending2: false}, 0, 2, 3);
322                 InspectorTest.assertEquals(3, provider.length, "nodes provider length");
323                 var names = [];
324                 for (provider.rewind(); provider.hasNext(); provider.next())
325                     names.push(provider.item().name());
326                 InspectorTest.assertEquals("E,C,A", names.join(","), "nodes provider names");
327             },
328
329             function heapSnapshotEdgesProviderTest()
330             {
331                 var snapshot = new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress());
332
333                 function edgeFilter(edge)
334                 {
335                     return edge.name() === "b";
336                 }
337
338                 var provider = snapshot.createEdgesProviderForTest(snapshot.rootNodeIndex, edgeFilter);
339                 InspectorTest.assertEquals(1, provider.length, "edges provider length");
340                 provider.sort({fieldName1: "!edgeName", ascending1: false, fieldName2: "id", ascending2: false}, 0, 0, 1);
341                 InspectorTest.assertEquals(1, provider.length, "edges provider length");
342                 var names = [];
343                 for (provider.rewind(); provider.hasNext(); provider.next())
344                     names.push(provider.item().name());
345                 InspectorTest.assertEquals("b", names.join(","), "edges provider names");
346             },
347
348             function heapSnapshotLoaderTest()
349             {
350                 var source = InspectorTest.createHeapSnapshotMockRaw();
351                 var sourceStringified = JSON.stringify(source);
352                 var partSize = sourceStringified.length >> 3;
353
354                 var loader = new WebInspector.HeapSnapshotLoader();
355                 for (var i = 0, l = sourceStringified.length; i < l; i += partSize)
356                     loader.write(sourceStringified.slice(i, i + partSize));
357                 loader.close();
358                 var result = loader.buildSnapshot("JSHeapSnapshot");
359                 result._nodes = new Uint32Array(result._nodes);
360                 result._containmentEdges = new Uint32Array(result._containmentEdges);
361                 function assertSnapshotEquals(reference, actual)
362                 {
363                     InspectorTest.assertEquals(JSON.stringify(reference), JSON.stringify(actual));
364                 }
365                 assertSnapshotEquals(new WebInspector.JSHeapSnapshot(InspectorTest.createHeapSnapshotMock(), new WebInspector.HeapSnapshotProgress()), result);
366             },
367         ];
368
369         var result = [];
370         for (var i = 0; i < testSuite.length; i++) {
371             var test = testSuite[i];
372             result.push("");
373             result.push("Running: " + test.name);
374             try {
375                 test();
376             } catch (e) {
377                 result.push("FAIL: " + e);
378             }
379         }
380         return result.join("\n");
381     }
382
383     var proxy = new WebInspector.HeapSnapshotWorkerProxy(function(eventName, arg)
384     {
385         InspectorTest.addResult("Unexpected event from worker: " + eventName);
386     });
387     var source = "(" + createTestEnvironmentInWorker + ")();" +
388                  "(" + InspectorTest.createHeapSnapshotMockFactories + ")();" +
389                  "(" + runTestSuiteInWorker + ")();";
390     proxy.evaluateForTest(source, function(result)
391     {
392         InspectorTest.addResult(result);
393         InspectorTest.completeTest();
394     });
395 }
396
397 </script>
398 </head>
399
400 <body onload="runTest()">
401 <p>
402 This test checks HeapSnapshots module.
403 </p>
404
405 </body>
406 </html>