Web Inspector: speed up _calculateRetainedSizes function
[WebKit-https.git] / PerformanceTests / inspector / performance-test.js
1 var initialize_TimeTracker = function() {
2
3 InspectorTest.runPerformanceTest = function(perfTest, executeTime, callback)
4 {
5     var Timer = function(test, callback)
6     {
7         this._callback = callback;
8         this._test = test;
9         this._times = {};
10         this._testStartTime = new Date();
11         this._heapSizeDeltas = [];
12         this._jsHeapSize = this._getJSHeapSize();
13     }
14
15     Timer.prototype = {
16         start: function(name)
17         {
18             return {name: name, startTime: new Date()};
19         },
20
21         finish: function(cookie)
22         {
23             var endTime = new Date();
24             if (!this._times[cookie.name])
25                 this._times[cookie.name] = [];
26             this._times[cookie.name].push(endTime - cookie.startTime);
27         },
28
29         _getJSHeapSize: function()
30         {
31             if (window.gc) {
32                 window.gc();
33                 window.gc();
34             }
35             return console.memory.usedJSHeapSize;
36         },
37
38         done: function(groupName)
39         {
40             var newJSHeapSize = this._getJSHeapSize();
41             this._heapSizeDeltas.push(newJSHeapSize - this._jsHeapSize);
42             this._jsHeapSize = newJSHeapSize;
43
44             var time = new Date();
45             if (time - this._testStartTime < executeTime)
46                 this._runTest();
47             else {
48                 if (this._complete)
49                     return;
50                 this._complete = true;
51
52                 this._dump(groupName);
53                 if (this._callback)
54                     this._callback();
55                 else
56                     InspectorTest.completeTest();
57             }
58         },
59
60         _runTest: function()
61         {
62             if (this._guard) {
63                 setTimeout(this._runTest.bind(this), 0);
64                 return;
65             }
66
67             this._guard = true;
68             var safeTest = InspectorTest.safeWrap(this._test);
69             safeTest(this);
70             this._guard = false;
71         },
72
73         _dump: function(groupName)
74         {
75             for (var testName in this._times)
76                 InspectorTest.dumpTestStats(groupName, testName, this._times[testName], "ms");
77
78             var url = WebInspector.inspectedPageURL;
79             var regExp = /([^\/]+)\.html/;
80             var matches = regExp.exec(url);
81             InspectorTest.dumpTestStats("heap-delta", matches[1], this._heapSizeDeltas, "kB", 1024);
82         },
83     }
84
85     InspectorTest.timer = new Timer(perfTest, callback);
86     InspectorTest.timer._runTest();
87 }
88
89 InspectorTest.measureFunction = function(object, functionName)
90 {
91     function measure() {
92         var timer = InspectorTest.timer;
93         var cookie;
94         if (timer)
95             cookie = timer.start(functionName);
96         var result = func.apply(this, arguments);
97
98         if (timer)
99             timer.finish(cookie);
100         return result;
101     }
102     var func = object[functionName];
103     object[functionName] = measure;
104 }
105
106 InspectorTest.mark = function(markerName)
107 {
108     var timer = InspectorTest.timer;
109     if (!timer)
110         return;
111
112     if (InspectorTest.lastMarkCookie)
113         timer.finish(InspectorTest.lastMarkCookie);
114
115     InspectorTest.lastMarkCookie = markerName ? timer.start(markerName) : null;
116 }
117
118 InspectorTest.dumpTestStats = function(groupName, testName, samples, units, divider)
119 {
120     divider = divider || 1;
121     var stripNResults = Math.floor(samples.length / 10);
122     samples.sort(function(a, b) { return a - b; });
123     var sum = 0;
124     for (var i = stripNResults; i < samples.length - stripNResults; ++i)
125         sum += samples[i];
126     InspectorTest.addResult("RESULT " + groupName + ': ' + testName + "= " + Math.floor(sum / (samples.length - stripNResults * 2) / divider) + " " + units);
127 }
128
129 InspectorTest.addBackendResponseSniffer = function(object, methodName, override, opt_sticky)
130 {
131     var originalMethod = InspectorTest.override(object, methodName, backendCall, opt_sticky);
132     function backendCall()
133     {
134         var args = Array.prototype.slice.call(arguments);
135         var callback = (args.length && typeof args[args.length - 1] === "function") ? args.pop() : 0;
136         args.push(function() {
137             callback.apply(null, arguments);
138             override.apply(null, arguments);
139         });
140         originalMethod.apply(object, args);
141     }
142 }
143
144 }