Update how the benchmark is run
[WebKit-https.git] / PerformanceTests / Animometer / tests / resources / sampler.js
1 Experiment = Utilities.createClass(
2     function()
3     {
4         this._sum = 0;
5         this._squareSum = 0;
6         this._numberOfSamples = 0;
7         this._maxHeap = Algorithm.createMaxHeap(Experiment.defaults.CONCERN_SIZE);
8     }, {
9
10     sample: function(value)
11     {
12         this._sum += value;
13         this._squareSum += value * value;
14         this._maxHeap.push(value);
15         ++this._numberOfSamples;
16     },
17
18     mean: function()
19     {
20         return Statistics.sampleMean(this._numberOfSamples, this._sum);
21     },
22
23     standardDeviation: function()
24     {
25         return Statistics.unbiasedSampleStandardDeviation(this._numberOfSamples, this._sum, this._squareSum);
26     },
27
28     percentage: function()
29     {
30         var mean = this.mean();
31         return mean ? this.standardDeviation() * 100 / mean : 0;
32     },
33
34     concern: function(percentage)
35     {
36         var size = Math.ceil(this._numberOfSamples * percentage / 100);
37         var values = this._maxHeap.values(size);
38         return values.length ? values.reduce(function(a, b) { return a + b; }) / values.length : 0;
39     },
40
41     score: function(percentage)
42     {
43         return Statistics.geometricMean([this.mean(), Math.max(this.concern(percentage), 1)]);
44     }
45 });
46
47 Experiment.defaults =
48 {
49     CONCERN: 5,
50     CONCERN_SIZE: 100,
51 }
52
53 Sampler = Utilities.createClass(
54     function(seriesCount, expectedSampleCount, processor)
55     {
56         this._processor = processor;
57
58         this.samples = [];
59         for (var i = 0; i < seriesCount; ++i) {
60             var array = new Array(expectedSampleCount);
61             array.fill(0);
62             this.samples[i] = array;
63         }
64         this.sampleCount = 0;
65         this.marks = {};
66     }, {
67
68     record: function() {
69         // Assume that arguments.length == this.samples.length
70         for (var i = 0; i < arguments.length; i++) {
71             this.samples[i][this.sampleCount] = arguments[i];
72         }
73         ++this.sampleCount;
74     },
75
76     mark: function(comment, data) {
77         data = data || {};
78         // The mark exists after the last recorded sample
79         data.index = this.sampleCount;
80
81         this.marks[comment] = data;
82     },
83
84     process: function()
85     {
86         var results = {};
87
88         // Remove unused capacity
89         this.samples = this.samples.map(function(array) {
90             return array.slice(0, this.sampleCount);
91         }, this);
92
93         this._processor.processSamples(results);
94
95         return results;
96     }
97 });