1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
package net.sf.infrared.base.model; |
23 |
|
|
24 |
|
import java.io.Serializable; |
25 |
|
import java.util.ArrayList; |
26 |
|
import java.util.Collection; |
27 |
|
import java.util.Collections; |
28 |
|
import java.util.HashMap; |
29 |
|
import java.util.HashSet; |
30 |
|
import java.util.Iterator; |
31 |
|
import java.util.LinkedList; |
32 |
|
import java.util.List; |
33 |
|
import java.util.Map; |
34 |
|
import java.util.Map.Entry; |
35 |
|
import java.util.Set; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
1 |
public class StatisticsSnapshot implements Serializable, Cloneable { |
43 |
|
private static final int DEFAULT_MAX_LAST_INVOCATIONS = 5; |
44 |
|
|
45 |
|
private static final String SEPERATOR = " - on - "; |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
1 |
private Set applicationNames = new HashSet(); |
50 |
|
|
51 |
|
|
52 |
1 |
private AggregateOperationTree tree = new AggregateOperationTree(); |
53 |
|
|
54 |
1 |
private LayerTimeRepository repository = new LayerTimeRepository(); |
55 |
|
|
56 |
|
|
57 |
1 |
private boolean hasStatistics = false; |
58 |
|
|
59 |
|
|
60 |
1 |
private int maxLastInvocations = DEFAULT_MAX_LAST_INVOCATIONS; |
61 |
|
|
62 |
|
|
63 |
1 |
private LinkedList lastInvocationsList = new LinkedList(); |
64 |
|
|
65 |
|
|
66 |
1 |
private long startTime = Long.MAX_VALUE; |
67 |
|
|
68 |
|
|
69 |
1 |
private long endTime = Long.MIN_VALUE; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
public synchronized void merge(ApplicationStatistics stats) { |
86 |
0 |
if (stats == null) { |
87 |
0 |
return; |
88 |
|
} |
89 |
0 |
applicationNames.add(getApplicationName(stats)); |
90 |
|
|
91 |
0 |
mergeStartAndEndTimes(stats); |
92 |
0 |
mergeLayerAndExecutionTimes(stats); |
93 |
|
|
94 |
0 |
AggregateOperationTree tree = stats.getTree(); |
95 |
0 |
mergeTree(tree); |
96 |
0 |
addToLastInvocations(stats.getLastInvocations()); |
97 |
0 |
} |
98 |
|
|
99 |
|
public List getLastInvocations() { |
100 |
4 |
return Collections.unmodifiableList(lastInvocationsList); |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
public void setMaxLastInvocations(int max) { |
107 |
1 |
maxLastInvocations = max; |
108 |
1 |
} |
109 |
|
|
110 |
|
public AggregateOperationTree getTree() { |
111 |
0 |
return tree; |
112 |
|
} |
113 |
|
|
114 |
|
public long getStartTime() { |
115 |
0 |
return startTime; |
116 |
|
} |
117 |
|
|
118 |
|
public long getEndTime() { |
119 |
0 |
return endTime; |
120 |
|
} |
121 |
|
|
122 |
|
public String[] getHierarchicalLayers() { |
123 |
0 |
return repository.getHierarchicalLayers(); |
124 |
|
} |
125 |
|
|
126 |
|
public String[] getAbsoluteLayers() { |
127 |
0 |
return repository.getAbsoluteLayers(); |
128 |
|
} |
129 |
|
|
130 |
|
public long getTimeInHierarchicalLayer(String hLayer) { |
131 |
0 |
return repository.getTimeInHierarchicalLayer(hLayer); |
132 |
|
} |
133 |
|
|
134 |
|
public long getTimeInAbsoluteLayer(String aLayer) { |
135 |
0 |
return repository.getTimeInAbsoluteLayer(aLayer); |
136 |
|
} |
137 |
|
|
138 |
|
public AggregateExecutionTime[] getExecutionsInHierarchicalLayer(String hLayer) { |
139 |
0 |
return repository.getExecutionsInHierarchicalLayer(hLayer); |
140 |
|
} |
141 |
|
|
142 |
|
public AggregateExecutionTime[] getExecutionsInAbsoluteLayer(String aLayer) { |
143 |
0 |
return repository.getExecutionsInAbsoluteLayer(aLayer); |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
public Set getApplicationNames() { |
161 |
0 |
return Collections.unmodifiableSet(applicationNames); |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
public static StatisticsSnapshot createSnapshot(Collection appNames, |
172 |
|
Collection instanceIds, Map layerTimes, Map executionTimes, AggregateOperationTree tree, |
173 |
|
long startTime, class="keyword">long endTime) { |
174 |
0 |
StatisticsSnapshot stats = new StatisticsSnapshot(); |
175 |
0 |
for (Iterator i = layerTimes.entrySet().iterator(); i.hasNext();) { |
176 |
0 |
Map.Entry entry = (Map.Entry) i.next(); |
177 |
0 |
String layer = (String) entry.getKey(); |
178 |
0 |
LayerTime lt = (LayerTime) entry.getValue(); |
179 |
0 |
stats.mergeLayerTime(layer, lt.getTime()); |
180 |
|
} |
181 |
0 |
for (Iterator i = executionTimes.entrySet().iterator(); i.hasNext();) { |
182 |
0 |
Map.Entry entry = (Map.Entry) i.next(); |
183 |
0 |
String layer = (String) entry.getKey(); |
184 |
0 |
List executions = (List) entry.getValue(); |
185 |
0 |
stats.mergeExecutionTimes(layer, |
186 |
|
(AggregateExecutionTime[]) executions.toArray(new AggregateExecutionTime[0])); |
187 |
|
} |
188 |
0 |
stats.tree = tree; |
189 |
0 |
stats.startTime = startTime; |
190 |
0 |
stats.endTime = endTime; |
191 |
0 |
stats.applicationNames = new HashSet(appNames); |
192 |
0 |
return stats; |
193 |
|
} |
194 |
|
|
195 |
|
String getApplicationName(ApplicationStatistics stats) { |
196 |
0 |
return stats.getApplicationName() + SEPERATOR + stats.getInstanceId(); |
197 |
|
} |
198 |
|
|
199 |
|
void mergeTree(AggregateOperationTree tree) { |
200 |
0 |
if (tree != null) { |
201 |
0 |
this.tree.merge(tree); |
202 |
0 |
hasStatistics = true; |
203 |
|
} |
204 |
0 |
} |
205 |
|
|
206 |
|
void mergeLayerAndExecutionTimes(ApplicationStatistics stats) { |
207 |
0 |
String[] layers = stats.getLayers(); |
208 |
0 |
for (int i = 0; i < layers.length; i++) { |
209 |
0 |
String aLayer = layers[i]; |
210 |
0 |
mergeLayerTime(aLayer, stats.getTimeInLayer(aLayer)); |
211 |
0 |
mergeExecutionTimes(aLayer, stats.getExecutionsInLayer(aLayer)); |
212 |
|
} |
213 |
0 |
} |
214 |
|
|
215 |
|
void mergeLayerTime(String layerName, long time) { |
216 |
0 |
repository.mergeHierarchicalLayerTime(layerName, time); |
217 |
0 |
hasStatistics = true; |
218 |
0 |
} |
219 |
|
|
220 |
|
void mergeExecutionTimes(String layerName, AggregateExecutionTime[] times) { |
221 |
0 |
repository.mergeExecutionTimes(layerName, times); |
222 |
0 |
hasStatistics = true; |
223 |
0 |
} |
224 |
|
|
225 |
|
|
226 |
|
void mergeStartAndEndTimes(ApplicationStatistics stats) { |
227 |
0 |
startTime = Math.min(startTime, stats.getStartTime()); |
228 |
0 |
endTime = Math.max(endTime, stats.getEndTime()); |
229 |
0 |
} |
230 |
|
|
231 |
|
|
232 |
|
void addToLastInvocations(List trees) { |
233 |
3 |
if ( (trees == null) || (trees.isEmpty()) ) { |
234 |
0 |
return; |
235 |
|
} |
236 |
|
|
237 |
3 |
lastInvocationsList.addAll(trees); |
238 |
3 |
int size = lastInvocationsList.size(); |
239 |
|
|
240 |
3 |
if (size > maxLastInvocations) { |
241 |
4 |
for (int i = size; i > maxLastInvocations; i--) { |
242 |
3 |
lastInvocationsList.removeFirst(); |
243 |
|
} |
244 |
|
} |
245 |
|
|
246 |
3 |
hasStatistics = true; |
247 |
3 |
} |
248 |
|
} |