9eac9aefb8c50998adb0f965a71340ef2913b1dd
[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     }
12
13     Timer.prototype = {
14         start: function(name)
15         {
16             return {name: name, startTime: new Date()};
17         },
18
19         finish: function(cookie)
20         {
21             var endTime = new Date();
22             if (!this._times[cookie.name])
23                 this._times[cookie.name] = [];
24             this._times[cookie.name].push(endTime - cookie.startTime);
25         },
26
27         done: function()
28         {
29             var time = new Date();
30             if (time - this._testStartTime < executeTime)
31                 this._runTest();
32             else {
33                 this._dump();
34                 if (this._callback)
35                     this._callback();
36                 else
37                     InspectorTest.completeTest();
38             }
39         },
40
41         _runTest: function()
42         {
43             if (this._guard) {
44                 setTimeout(this._runTest.bind(this), 0);
45                 return;
46             }
47
48             this._guard = true;
49             var safeTest = InspectorTest.safeWrap(this._test);
50             safeTest(this);
51             this._guard = false;
52         },
53
54         _dump: function()
55         {
56             for (var testName in this._times) {
57                 var samples = this._times[testName];
58                 var stripNResults = Math.floor(samples.length / 10);
59                 samples.sort(function(a, b) { return a - b; });
60                 var sum = 0;
61                 for (var i = stripNResults; i < samples.length - stripNResults; ++i)
62                     sum += samples[i];
63                 InspectorTest.addResult("* " + testName + ": " + Math.floor(sum / (samples.length - stripNResults * 2)));
64                 InspectorTest.addResult(testName + " min/max/count: " + samples[0] + "/" + samples[samples.length-1] + "/" + samples.length);
65             }
66         }
67     }
68
69     var timer = new Timer(perfTest, callback);
70     timer._runTest();
71 }
72
73 InspectorTest.addBackendResponseSniffer = function(object, methodName, override, opt_sticky)
74 {
75     var originalMethod = InspectorTest.override(object, methodName, backendCall, opt_sticky);
76     function backendCall()
77     {
78         var args = Array.prototype.slice.call(arguments);
79         var callback = (args.length && typeof args[args.length - 1] === "function") ? args.pop() : 0;
80         args.push(function() {
81             callback.apply(null, arguments);
82             override.apply(null, arguments);
83         });
84         originalMethod.apply(object, args);
85     }
86 }
87
88
89 }