Web Inspector: UI performance test for network panel is incorrect. The time is includ...
[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                 if (this._complete)
47                     return;
48                 this._complete = true;
49
50                 this._dump();
51                 if (this._callback)
52                     this._callback();
53                 else
54                     InspectorTest.completeTest();
55             }
56         },
57
58         _runTest: function()
59         {
60             if (this._guard) {
61                 setTimeout(this._runTest.bind(this), 0);
62                 return;
63             }
64
65             this._guard = true;
66             var safeTest = InspectorTest.safeWrap(this._test);
67             safeTest(this);
68             this._guard = false;
69         },
70
71         _dump: function()
72         {
73             for (var testName in this._times)
74                 InspectorTest.dumpTestStats(testName, this._times[testName]);
75
76             var url = WebInspector.mainResource._documentURL;
77             var regExp = /([^\/]+)\.html/;
78             var matches = regExp.exec(url);
79             InspectorTest.dumpTestStats("heap-delta-kb-" + matches[1], this._heapSizeDeltas, 1024);
80         },
81
82     }
83
84     InspectorTest.timer = new Timer(perfTest, callback);
85     InspectorTest.timer._runTest();
86 }
87
88 InspectorTest.mark = function(markerName)
89 {
90     var timer = InspectorTest.timer;
91     if (!timer)
92         return;
93
94     if (InspectorTest.lastMarkCookie)
95         timer.finish(InspectorTest.lastMarkCookie);
96
97     InspectorTest.lastMarkCookie = markerName ? timer.start(markerName) : null;
98 }
99
100 InspectorTest.dumpTestStats = function(testName, samples, divider)
101 {
102     divider = divider || 1;
103     var stripNResults = Math.floor(samples.length / 10);
104     samples.sort(function(a, b) { return a - b; });
105     var sum = 0;
106     for (var i = stripNResults; i < samples.length - stripNResults; ++i)
107         sum += samples[i];
108     InspectorTest.addResult("* " + testName + ": " + Math.floor(sum / (samples.length - stripNResults * 2) / divider));
109     InspectorTest.addResult(testName + " min/max/count: " + Math.floor(samples[0] / divider) + "/" + Math.floor(samples[samples.length-1] / divider) + "/" + samples.length);
110 }
111
112 InspectorTest.addBackendResponseSniffer = function(object, methodName, override, opt_sticky)
113 {
114     var originalMethod = InspectorTest.override(object, methodName, backendCall, opt_sticky);
115     function backendCall()
116     {
117         var args = Array.prototype.slice.call(arguments);
118         var callback = (args.length && typeof args[args.length - 1] === "function") ? args.pop() : 0;
119         args.push(function() {
120             callback.apply(null, arguments);
121             override.apply(null, arguments);
122         });
123         originalMethod.apply(object, args);
124     }
125 }
126
127 }