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