1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.perfidix.result;
28
29 import java.util.Collection;
30 import java.util.Hashtable;
31 import java.util.LinkedList;
32 import java.util.Map;
33 import java.util.Set;
34
35 import org.apache.commons.collections.primitives.adapters.CollectionDoubleCollection;
36 import org.apache.commons.math.stat.descriptive.AbstractUnivariateStatistic;
37 import org.apache.commons.math.stat.descriptive.moment.Mean;
38 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
39 import org.apache.commons.math.stat.descriptive.rank.Max;
40 import org.apache.commons.math.stat.descriptive.rank.Min;
41 import org.apache.commons.math.stat.descriptive.rank.Percentile;
42 import org.apache.commons.math.stat.descriptive.summary.Sum;
43 import org.apache.commons.math.stat.descriptive.summary.SumOfSquares;
44 import org.perfidix.meter.AbstractMeter;
45
46
47
48
49
50
51
52
53
54 public abstract class AbstractResult {
55
56
57 private transient final Object relatedElement;
58
59
60
61
62 private transient final Map<AbstractMeter, Collection<Double>> meterResults;
63
64
65
66
67
68
69
70 protected AbstractResult(final Object paramElement) {
71 this.relatedElement = paramElement;
72 this.meterResults = new Hashtable<AbstractMeter, Collection<Double>>();
73
74 }
75
76
77
78
79
80
81 public abstract String getElementName();
82
83
84
85
86
87
88
89
90 public final Collection<Double> getResultSet(final AbstractMeter meter) {
91 checkIfMeterExists(meter);
92 return this.meterResults.get(meter);
93 }
94
95
96
97
98
99
100 public final Set<AbstractMeter> getRegisteredMeters() {
101 return this.meterResults.keySet();
102 }
103
104
105
106
107
108
109
110
111
112 public final double mean(final AbstractMeter meter) {
113 checkIfMeterExists(meter);
114 final AbstractUnivariateStatistic mean = new Mean();
115 final CollectionDoubleCollection doubleColl =
116 new CollectionDoubleCollection(this.meterResults.get(meter));
117 return mean.evaluate(doubleColl.toArray(), 0, doubleColl.toArray().length);
118 }
119
120
121
122
123
124
125
126
127 public final double squareSum(final AbstractMeter meter) {
128 checkIfMeterExists(meter);
129 final AbstractUnivariateStatistic sqrSum = new SumOfSquares();
130 final CollectionDoubleCollection doubleColl =
131 new CollectionDoubleCollection(this.meterResults.get(meter));
132 return sqrSum.evaluate(doubleColl.toArray(), 0, doubleColl.toArray().length);
133 }
134
135
136
137
138
139
140
141
142 public final double getStandardDeviation(final AbstractMeter meter) {
143 checkIfMeterExists(meter);
144 final AbstractUnivariateStatistic stdDev = new StandardDeviation();
145 final CollectionDoubleCollection doubleColl =
146 new CollectionDoubleCollection(this.meterResults.get(meter));
147 return stdDev.evaluate(doubleColl.toArray(), 0, doubleColl.toArray().length);
148 }
149
150
151
152
153
154
155
156
157 public final double sum(final AbstractMeter meter) {
158 checkIfMeterExists(meter);
159 final AbstractUnivariateStatistic sum = new Sum();
160 final CollectionDoubleCollection doubleColl =
161 new CollectionDoubleCollection(this.meterResults.get(meter));
162 return sum.evaluate(doubleColl.toArray(), 0, doubleColl.toArray().length);
163 }
164
165
166
167
168
169
170
171
172 public final double min(final AbstractMeter meter) {
173 checkIfMeterExists(meter);
174 final AbstractUnivariateStatistic min = new Min();
175 final CollectionDoubleCollection doubleColl =
176 new CollectionDoubleCollection(this.meterResults.get(meter));
177 return min.evaluate(doubleColl.toArray(), 0, doubleColl.toArray().length);
178 }
179
180
181
182
183
184
185
186
187
188 public final double getConf05(final AbstractMeter meter) {
189 checkIfMeterExists(meter);
190 final AbstractUnivariateStatistic conf05 = new Percentile(5.0);
191 final CollectionDoubleCollection doubleColl =
192 new CollectionDoubleCollection(this.meterResults.get(meter));
193 return conf05.evaluate(doubleColl.toArray(), 0, doubleColl.toArray().length);
194
195 }
196
197
198
199
200
201
202
203
204
205 public final double getConf95(final AbstractMeter meter) {
206 checkIfMeterExists(meter);
207 final AbstractUnivariateStatistic conf95 = new Percentile(95.0);
208 final CollectionDoubleCollection doubleColl =
209 new CollectionDoubleCollection(this.meterResults.get(meter));
210 return conf95.evaluate(doubleColl.toArray(), 0, doubleColl.toArray().length);
211 }
212
213
214
215
216
217
218
219
220 public final double max(final AbstractMeter meter) {
221 checkIfMeterExists(meter);
222 final AbstractUnivariateStatistic max = new Max();
223 final CollectionDoubleCollection doubleColl =
224 new CollectionDoubleCollection(this.meterResults.get(meter));
225 return max.evaluate(doubleColl.toArray(), 0, doubleColl.toArray().length);
226 }
227
228
229
230
231
232
233
234
235 public final int getNumberOfResult(final AbstractMeter meter) {
236 checkIfMeterExists(meter);
237 return meterResults.get(meter).size();
238 }
239
240
241
242
243
244
245 public final Object getRelatedElement() {
246 return relatedElement;
247 }
248
249
250
251
252
253
254
255
256
257 protected final void addData(final AbstractMeter meter, final double data) {
258 checkIfMeterExists(meter);
259 meterResults.get(meter).add(data);
260 }
261
262
263
264
265
266
267
268
269 private void checkIfMeterExists(final AbstractMeter meter) {
270 if (!meterResults.containsKey(meter)) {
271 meterResults.put(meter, new LinkedList<Double>());
272 }
273 }
274
275
276
277
278 @Override
279 public String toString() {
280 final StringBuilder builder = new StringBuilder();
281 builder.append(getElementName()).append("\nmeters: ").append(getRegisteredMeters()).append(
282 "\nresults: ").append(meterResults);
283 return builder.toString();
284 }
285
286 }