a4d72ffc07bd060a726bc65f9e44ac203f553531
[WebKit-https.git] / LayoutTests / inspector / timeline-test.js
1 var timelineAgentRecordType = {};
2
3 // Scrub values when printing out these properties in the record or data field.
4 var timelineNonDeterministicProps = { 
5     children : 1,
6     endTime : 1, 
7     height : 1,
8     identifier : 1,
9     startTime : 1,
10     width : 1,
11     stackTrace: 1,
12     url : 1,
13     usedHeapSize: 1,
14     totalHeapSize: 1,
15     mimeType : 1
16 };
17
18 function printTimelineRecords(performActions, typeName, formatter)
19 {
20     if (performActions) {
21         if (window.layoutTestController)
22             layoutTestController.setTimelineProfilingEnabled(true);
23         performActions();
24     }
25
26     evaluateInWebInspector("WebInspector.TimelineAgent.RecordType", function(result) {
27         timelineAgentRecordType = result;
28     });
29
30     evaluateInWebInspector("frontend_getTimelineResults", function(timelineRecords) {
31         try {
32             if (typeof(timelineRecords) === "string")
33                 output("Error fetching Timeline results: " + timelineRecords);
34             else {
35                 for (var i = 0; i < timelineRecords.length; ++i) {
36                     var record = timelineRecords[i];
37                     if (typeName && record.type === timelineAgentRecordType[typeName])
38                         printTimelineRecordProperties(record);
39                     if (formatter)
40                         formatter(record);
41                 }
42             }
43             if (window.layoutTestController)
44                 layoutTestController.setTimelineProfilingEnabled(false);
45             notifyDone();
46         } catch (e) {
47             console.log("An exception was caught: " + e.toString());
48             notifyDone(e.toString());
49         }
50     });
51 }
52
53 // Dump just the record name, indenting output on separate lines for subrecords
54 function dumpTimelineRecord(record, level) 
55 {
56     if (typeof level !== "number")
57         level = 0;
58     var prefix = "";
59     var suffix = "";
60     for (var i = 0; i < level ; ++i)
61         prefix = "----" + prefix;
62     if (level > 0)
63         prefix = prefix + "> ";
64     if (record.type === timelineAgentRecordType.MarkTimeline) {
65         suffix = " : " + record.data.message;
66     }
67     output(prefix + timelineAgentTypeToString(record.type) + suffix);
68
69     var numChildren = record.children ? record.children.length : 0;
70     for (var i = 0; i < numChildren; ++i)
71         dumpTimelineRecord(record.children[i], level + 1);
72 }
73
74 function dumpTimelineRecords(timelineRecords) {
75     for (var i = 0; i < timelineRecords.length; ++i)
76         dumpTimelineRecord(timelineRecords[i], 0);
77 }
78
79 function printTimelineRecordProperties(record)
80 {
81     output(timelineAgentTypeToString(record.type) + " Properties:");
82     // Use this recursive routine to print the properties
83     dumpObject(record, timelineNonDeterministicProps);
84 }
85
86 function timelineAgentTypeToString(numericType)
87 {
88     for (var prop in timelineAgentRecordType) {
89         if (timelineAgentRecordType[prop] === numericType)
90             return prop;
91     }
92     return undefined;
93 }
94
95 // Injected into Inspector window
96 function frontend_getTimelineResults() {
97     var result = [];
98     function addRecords(records)
99     {
100         if (!records)
101             return;
102         for (var i = 0; i < records.length; ++i) {
103             result.push(records[i].originalRecordForTests);
104             addRecords(records[i].children);
105         }
106     }
107     addRecords(WebInspector.panels.timeline._rootRecord.children);
108     return result;
109 }