54b6df9bb974f65e035e0698b68c5fd545c3f6e7
[WebKit-https.git] / LayoutTests / inspector / performance / resources / 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             window.gc();
32             window.gc();
33             return console.memory.usedJSHeapSize;
34         },
35
36         done: function()
37         {
38             this._heapSizeDeltas.push(console.memory.usedJSHeapSize - this._jsHeapSize);
39             this._jsHeapSize = this._getJSHeapSize();
40
41             var time = new Date();
42             if (time - this._testStartTime < executeTime)
43                 this._runTest();
44             else {
45                 this._dump();
46                 if (this._callback)
47                     this._callback();
48                 else
49                     InspectorTest.completeTest();
50             }
51         },
52
53         _runTest: function()
54         {
55             if (this._guard) {
56                 setTimeout(this._runTest.bind(this), 0);
57                 return;
58             }
59
60             this._guard = true;
61             var safeTest = InspectorTest.safeWrap(this._test);
62             safeTest(this);
63             this._guard = false;
64         },
65
66         _dump: function()
67         {
68             for (var testName in this._times)
69                 this._dumpTestStats(testName, this._times[testName]);
70
71             var url = WebInspector.mainResource._documentURL;
72             var regExp = /([^\/]+)\.html/;
73             var matches = regExp.exec(url);
74             this._dumpTestStats("heap-delta-kb-" + matches[1], this._heapSizeDeltas, 1024);
75         },
76
77         _dumpTestStats: function(testName, samples, divider)
78         {
79             divider = divider || 1;
80             var stripNResults = Math.floor(samples.length / 10);
81             samples.sort(function(a, b) { return a - b; });
82             var sum = 0;
83             for (var i = stripNResults; i < samples.length - stripNResults; ++i)
84                 sum += samples[i];
85             InspectorTest.addResult("* " + testName + ": " + Math.floor(sum / (samples.length - stripNResults * 2) / divider));
86             InspectorTest.addResult(testName + " min/max/count: " + Math.floor(samples[0] / divider) + "/" + Math.floor(samples[samples.length-1] / divider) + "/" + samples.length);
87         }
88     }
89
90     var timer = new Timer(perfTest, callback);
91     timer._runTest();
92 }
93
94 InspectorTest.addBackendResponseSniffer = function(object, methodName, override, opt_sticky)
95 {
96     var originalMethod = InspectorTest.override(object, methodName, backendCall, opt_sticky);
97     function backendCall()
98     {
99         var args = Array.prototype.slice.call(arguments);
100         var callback = (args.length && typeof args[args.length - 1] === "function") ? args.pop() : 0;
101         args.push(function() {
102             callback.apply(null, arguments);
103             override.apply(null, arguments);
104         });
105         originalMethod.apply(object, args);
106     }
107 }
108
109 }