MeasurementCluster's addToSeries is slow
[WebKit-https.git] / Websites / perf.webkit.org / public / v3 / models / measurement-adaptor.js
1 'use strict';
2
3 class MeasurementAdaptor {
4     constructor(formatMap)
5     {
6         var nameMap = {};
7         formatMap.forEach(function (key, index) {
8             nameMap[key] = index;
9         });
10         this._idIndex = nameMap['id'];
11         this._commitTimeIndex = nameMap['commitTime'];
12         this._countIndex = nameMap['iterationCount'];
13         this._meanIndex = nameMap['mean'];
14         this._sumIndex = nameMap['sum'];
15         this._squareSumIndex = nameMap['squareSum'];
16         this._markedOutlierIndex = nameMap['markedOutlier'];
17         this._revisionsIndex = nameMap['revisions'];
18         this._buildIndex = nameMap['build'];
19         this._buildTimeIndex = nameMap['buildTime'];
20         this._buildNumberIndex = nameMap['buildNumber'];
21         this._builderIndex = nameMap['builder'];
22         this._metricIndex = nameMap['metric'];
23         this._configTypeIndex = nameMap['configType'];
24     }
25
26     extractId(row)
27     {
28         return row[this._idIndex];
29     }
30
31     isOutlier(row)
32     {
33         return row[this._markedOutlierIndex];
34     }
35
36     applyToAnalysisResults(row)
37     {
38         var adaptedRow = this.applyTo(row);
39         adaptedRow.metricId = row[this._metricIndex];
40         adaptedRow.configType = row[this._configTypeIndex];
41         return adaptedRow;
42     }
43
44     applyTo(row)
45     {
46         var id = row[this._idIndex];
47         var mean = row[this._meanIndex];
48         var sum = row[this._sumIndex];
49         var squareSum = row[this._squareSumIndex];
50         var buildId = row[this._buildIndex];
51         var builderId = row[this._builderIndex];
52         var cachedBuild = null;
53         var cachedInterval = null;
54
55         var self = this;
56         return {
57             id: id,
58             markedOutlier: row[this._markedOutlierIndex],
59             buildId: buildId,
60             metricId: null,
61             configType: null,
62             rootSet: function () { return MeasurementRootSet.ensureSingleton(id, row[self._revisionsIndex]); },
63             build: function () {
64                 if (cachedBuild == null)
65                     cachedBuild = new Build(buildId, Builder.findById(builderId), row[self._buildNumberIndex], row[self._buildTimeIndex]);
66                 return cachedBuild;
67             },
68             time: row[this._commitTimeIndex],
69             value: mean,
70             sum: sum,
71             squareSum: squareSum,
72             iterationCount: row[this._countIndex],
73             interval: function () {
74                 if (cachedInterval == null)
75                     cachedInterval = MeasurementAdaptor.computeConfidenceInterval(row[self._countIndex], mean, sum, squareSum);
76                 return cachedInterval;
77             }
78         };
79     }
80
81     static aggregateAnalysisResults(results)
82     {
83         var totalSum = 0;
84         var totalSquareSum = 0;
85         var totalIterationCount = 0;
86         var means = [];
87         for (var result of results) {
88             means.push(result.value);
89             totalSum += result.sum;
90             totalSquareSum += result.squareSum;
91             totalIterationCount += result.iterationCount;
92         }
93         var mean = totalSum / totalIterationCount;
94         var interval;
95         try {
96             interval = this.computeConfidenceInterval(totalIterationCount, mean, totalSum, totalSquareSum)
97         } catch (error) {
98             interval = this.computeConfidenceInterval(results.length, mean, Statistics.sum(means), Statistics.squareSum(means));
99         }
100         return { value: mean, interval: interval };
101     }
102
103     static computeConfidenceInterval(iterationCount, mean, sum, squareSum)
104     {
105         var delta = Statistics.confidenceIntervalDelta(0.95, iterationCount, sum, squareSum);
106         return isNaN(delta) ? null : [mean - delta, mean + delta];
107     }
108 }
109
110 if (typeof module != 'undefined')
111     module.exports.MeasurementAdaptor = MeasurementAdaptor;