1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.performance;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.io.Serializable;
24 import org.apache.commons.math.stat.descriptive.SummaryStatistics;
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class Statistics implements Serializable {
39 private HashMap<StatisticsKey,SummaryStatistics> data =
40 new HashMap <StatisticsKey,SummaryStatistics>();
41
42
43
44
45
46
47
48
49
50 public synchronized void addStatistics(SummaryStatistics stats,
51 String process, String type) {
52 StatisticsKey key = new StatisticsKey(process, type);
53 data.put(key, stats);
54 }
55
56
57
58
59
60
61
62
63
64
65 public synchronized SummaryStatistics getStatistics(String process,
66 String type) {
67 StatisticsKey key = new StatisticsKey(process, type);
68 return data.get(key);
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82 public synchronized List<SummaryStatistics> getStatisticsByType(String type) {
83 ArrayList<SummaryStatistics> result = new ArrayList<SummaryStatistics>();
84 Iterator<StatisticsKey> it = data.keySet().iterator();
85 while (it.hasNext()) {
86 StatisticsKey key = it.next();
87 if (key.type.equals(type)) {
88 result.add(data.get(key));
89 }
90 }
91 return result;
92 }
93
94
95
96
97
98
99
100
101
102 public synchronized List<SummaryStatistics> getStatisticsByProcess(
103 String process) {
104 ArrayList<SummaryStatistics> result = new ArrayList<SummaryStatistics>();
105 Iterator<StatisticsKey> it = data.keySet().iterator();
106 while (it.hasNext()) {
107 StatisticsKey key = it.next();
108 if (key.process.equals(process)) {
109 result.add(data.get(key));
110 }
111 }
112 return result;
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 public synchronized SummaryStatistics getMeanSummary(String type) {
132 SummaryStatistics result = new SummaryStatistics();
133 Iterator<StatisticsKey> it = data.keySet().iterator();
134 while (it.hasNext()) {
135 StatisticsKey key = it.next();
136 if (key.type.equals(type)) {
137 result.addValue(data.get(key).getMean());
138 }
139 }
140 return result;
141 }
142
143
144
145
146
147
148
149
150
151
152 public synchronized SummaryStatistics getStdSummary(String type) {
153 SummaryStatistics result = new SummaryStatistics();
154 Iterator<StatisticsKey> it = data.keySet().iterator();
155 while (it.hasNext()) {
156 StatisticsKey key = it.next();
157 if (key.type.equals(type)) {
158 result.addValue(data.get(key).getStandardDeviation());
159 }
160 }
161 return result;
162 }
163
164
165
166
167
168
169
170
171
172
173 public synchronized SummaryStatistics getMinSummary(String type) {
174 SummaryStatistics result = new SummaryStatistics();
175 Iterator<StatisticsKey> it = data.keySet().iterator();
176 while (it.hasNext()) {
177 StatisticsKey key = it.next();
178 if (key.type.equals(type)) {
179 result.addValue(data.get(key).getMin());
180 }
181 }
182 return result;
183 }
184
185
186
187
188
189
190
191
192
193
194 public synchronized SummaryStatistics getMaxSummary(String type) {
195 SummaryStatistics result = new SummaryStatistics();
196 Iterator<StatisticsKey> it = data.keySet().iterator();
197 while (it.hasNext()) {
198 StatisticsKey key = it.next();
199 if (key.type.equals(type)) {
200 result.addValue(data.get(key).getMax());
201 }
202 }
203 return result;
204 }
205
206
207
208
209
210
211 public synchronized List<String> getProcesses() {
212 ArrayList<String> result = new ArrayList<String>();
213 Iterator<StatisticsKey> it = data.keySet().iterator();
214 while (it.hasNext()) {
215 String currProcess = it.next().process;
216 if (!result.contains(currProcess)) {
217 result.add(currProcess);
218 }
219 }
220 return result;
221 }
222
223
224
225
226
227
228 public synchronized List<String> getTypes() {
229 ArrayList<String> result = new ArrayList<String>();
230 Iterator<StatisticsKey> it = data.keySet().iterator();
231 while (it.hasNext()) {
232 String currType = it.next().type;
233 if (!result.contains(currType)) {
234 result.add(currType);
235 }
236 }
237 return result;
238 }
239
240
241
242
243
244
245
246 public synchronized String displayOverallSummary() {
247 Iterator<String> metricsIterator = getTypes().iterator();
248 StringBuffer buffer = new StringBuffer();
249 while (metricsIterator.hasNext()) {
250 String metric = metricsIterator.next();
251 buffer.append("Overall statistics for the mean ");
252 buffer.append(metric.toUpperCase());
253 buffer.append("\n");
254 buffer.append(getMeanSummary(metric).toString());
255 buffer.append("********************************************\n");
256 buffer.append("Overall statistics for the standard deviation ");
257 buffer.append(metric.toUpperCase());
258 buffer.append("\n");
259 buffer.append(getStdSummary(metric).toString());
260 buffer.append("********************************************\n");
261 buffer.append("Overall statistics for the min ");
262 buffer.append(metric.toUpperCase());
263 buffer.append("\n");
264 buffer.append(getMinSummary(metric).toString());
265 buffer.append("********************************************\n");
266 buffer.append("Overall statistics for the max ");
267 buffer.append(metric.toUpperCase());
268 buffer.append("\n");
269 buffer.append(getMaxSummary(metric).toString());
270 buffer.append("********************************************\n");
271 }
272 return buffer.toString();
273 }
274
275
276
277
278
279
280
281
282 public synchronized String displayProcessStatistics(String process) {
283 Iterator<String> metricsIterator = getTypes().iterator();
284 StringBuffer buffer = new StringBuffer();
285 while (metricsIterator.hasNext()) {
286 String metric = metricsIterator.next();
287 buffer.append("*********************************************\n");
288 buffer.append(metric.toUpperCase());
289 buffer.append(" for ");
290 buffer.append(process);
291 buffer.append(" ");
292 buffer.append(getStatistics(process, metric).toString());
293 buffer.append("\n********************************************\n");
294 }
295 return buffer.toString();
296 }
297
298
299
300
301
302 private static class StatisticsKey implements Serializable {
303 public StatisticsKey(String process, String type) {
304 this.process = process;
305 this.type = type;
306 }
307 private String process = null;
308 private String type = null;
309 public boolean equals(Object obj) {
310 if (!(obj instanceof StatisticsKey) || obj == null) {
311 return false;
312 } else {
313 StatisticsKey other = (StatisticsKey) obj;
314 return (other.process.equals(this.process)) &&
315 (other.type.equals(this.type));
316 }
317 }
318 public int hashCode() {
319 return 7 + 11 * process.hashCode() + 17 * type.hashCode();
320 }
321 public String getType() {
322 return type;
323 }
324 public String getProcess() {
325 return process;
326 }
327 }
328
329 }