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