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 | 565 | protected AbstractResult(final Object paramElement) { |
71 | 565 | this.relatedElement = paramElement; |
72 | 565 | this.meterResults = new Hashtable<AbstractMeter, Collection<Double>>(); |
73 | |
|
74 | 565 | } |
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 | 160 | checkIfMeterExists(meter); |
92 | 160 | return this.meterResults.get(meter); |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public final Set<AbstractMeter> getRegisteredMeters() { |
101 | 95 | return this.meterResults.keySet(); |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public final double mean(final AbstractMeter meter) { |
113 | 150 | checkIfMeterExists(meter); |
114 | 150 | final AbstractUnivariateStatistic mean = new Mean(); |
115 | 150 | final CollectionDoubleCollection doubleColl = |
116 | |
new CollectionDoubleCollection(this.meterResults.get(meter)); |
117 | 150 | 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 | 35 | checkIfMeterExists(meter); |
129 | 35 | final AbstractUnivariateStatistic sqrSum = new SumOfSquares(); |
130 | 35 | final CollectionDoubleCollection doubleColl = |
131 | |
new CollectionDoubleCollection(this.meterResults.get(meter)); |
132 | 35 | 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 | 150 | checkIfMeterExists(meter); |
144 | 150 | final AbstractUnivariateStatistic stdDev = new StandardDeviation(); |
145 | 150 | final CollectionDoubleCollection doubleColl = |
146 | |
new CollectionDoubleCollection(this.meterResults.get(meter)); |
147 | 150 | 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 | 150 | checkIfMeterExists(meter); |
159 | 150 | final AbstractUnivariateStatistic sum = new Sum(); |
160 | 150 | final CollectionDoubleCollection doubleColl = |
161 | |
new CollectionDoubleCollection(this.meterResults.get(meter)); |
162 | 150 | 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 | 150 | checkIfMeterExists(meter); |
174 | 150 | final AbstractUnivariateStatistic min = new Min(); |
175 | 150 | final CollectionDoubleCollection doubleColl = |
176 | |
new CollectionDoubleCollection(this.meterResults.get(meter)); |
177 | 150 | 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 | 150 | checkIfMeterExists(meter); |
190 | 150 | final AbstractUnivariateStatistic conf05 = new Percentile(5.0); |
191 | 150 | final CollectionDoubleCollection doubleColl = |
192 | |
new CollectionDoubleCollection(this.meterResults.get(meter)); |
193 | 150 | 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 | 150 | checkIfMeterExists(meter); |
207 | 150 | final AbstractUnivariateStatistic conf95 = new Percentile(95.0); |
208 | 150 | final CollectionDoubleCollection doubleColl = |
209 | |
new CollectionDoubleCollection(this.meterResults.get(meter)); |
210 | 150 | 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 | 150 | checkIfMeterExists(meter); |
222 | 150 | final AbstractUnivariateStatistic max = new Max(); |
223 | 150 | final CollectionDoubleCollection doubleColl = |
224 | |
new CollectionDoubleCollection(this.meterResults.get(meter)); |
225 | 150 | 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 | 35 | checkIfMeterExists(meter); |
237 | 35 | return meterResults.get(meter).size(); |
238 | |
} |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
public final Object getRelatedElement() { |
246 | 435 | return relatedElement; |
247 | |
} |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
protected final void addData(final AbstractMeter meter, final double data) { |
258 | 38229 | checkIfMeterExists(meter); |
259 | 38229 | meterResults.get(meter).add(data); |
260 | 38229 | } |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
private void checkIfMeterExists(final AbstractMeter meter) { |
270 | 39509 | if (!meterResults.containsKey(meter)) { |
271 | 600 | meterResults.put(meter, new LinkedList<Double>()); |
272 | |
} |
273 | 39509 | } |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
@Override |
279 | |
public String toString() { |
280 | 0 | final StringBuilder builder = new StringBuilder(); |
281 | 0 | builder.append(getElementName()).append("\nmeters: ").append(getRegisteredMeters()).append( |
282 | |
"\nresults: ").append(meterResults); |
283 | 0 | return builder.toString(); |
284 | |
} |
285 | |
|
286 | |
} |