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.util.Collections; |
25 |
|
import java.util.Iterator; |
26 |
|
import java.util.LinkedList; |
27 |
|
import java.util.List; |
28 |
|
|
29 |
|
import net.sf.infrared.base.util.Tree; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
public class ApplicationStatistics extends AbstractStatistics { |
43 |
|
private static final int DEFAULT_MAX_LAST_INVOCATIONS = 5; |
44 |
|
|
45 |
|
|
46 |
|
private AggregateOperationTree tree; |
47 |
|
|
48 |
|
|
49 |
5 |
private boolean hasStatistics = false; |
50 |
|
|
51 |
|
|
52 |
5 |
private int maxLastInvocations = DEFAULT_MAX_LAST_INVOCATIONS; |
53 |
|
|
54 |
|
|
55 |
|
private LinkedList lastInvocationsList; |
56 |
|
|
57 |
|
|
58 |
5 |
private long startTime = Long.MAX_VALUE; |
59 |
|
|
60 |
|
|
61 |
5 |
private long endTime = Long.MIN_VALUE; |
62 |
|
|
63 |
|
private LayerTimeRepository repository; |
64 |
|
|
65 |
|
public ApplicationStatistics(String applicationName, String instanceId) { |
66 |
5 |
super(applicationName, instanceId); |
67 |
5 |
reset(); |
68 |
5 |
} |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
public synchronized void reset() { |
74 |
5 |
repository = new LayerTimeRepository(); |
75 |
5 |
hasStatistics = false; |
76 |
5 |
tree = new AggregateOperationTree(); |
77 |
5 |
lastInvocationsList = new LinkedList(); |
78 |
5 |
startTime = Long.MAX_VALUE; |
79 |
5 |
endTime = Long.MIN_VALUE; |
80 |
5 |
} |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
public synchronized void merge(OperationStatistics opStats) { |
86 |
4 |
if (opStats == null) { |
87 |
1 |
return; |
88 |
|
} |
89 |
3 |
if ( (! getApplicationName().equals( opStats.getApplicationName() )) |
90 |
|
|| (! getInstanceId().equals( opStats.getInstanceId() )) ){ |
91 |
2 |
throw new IllegalArgumentException("Incorrect application name or instanceId:" + |
92 |
|
" Can't merge " + opStats + " to " + this); |
93 |
|
} |
94 |
|
|
95 |
1 |
mergeStartAndEndTimes(opStats); |
96 |
1 |
mergeLayerAndExecutionTimes(opStats); |
97 |
|
|
98 |
1 |
Tree tree = opStats.getOperationTree(); |
99 |
1 |
addToLastInvocations(tree); |
100 |
1 |
mergeTree(tree); |
101 |
1 |
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
public synchronized void merge(ApplicationStatistics otherStats) { |
107 |
0 |
if (otherStats == null) { |
108 |
0 |
return; |
109 |
|
} |
110 |
0 |
if ( (! getApplicationName().equals( otherStats.getApplicationName() )) |
111 |
|
|| (! getInstanceId().equals( otherStats.getInstanceId() )) ){ |
112 |
0 |
throw new IllegalArgumentException("Incorrect application name or instanceId:" + |
113 |
|
" Can't merge " + otherStats + " to " + this); |
114 |
|
} |
115 |
|
|
116 |
0 |
mergeStartAndEndTimes(otherStats); |
117 |
0 |
mergeLayerAndExecutionTimes(otherStats); |
118 |
|
|
119 |
0 |
AggregateOperationTree tree = otherStats.getTree(); |
120 |
0 |
mergeTree(tree); |
121 |
|
|
122 |
0 |
addToLastInvocations(otherStats.getLastInvocations()); |
123 |
0 |
} |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
public boolean hasStatistics() { |
129 |
2 |
return hasStatistics; |
130 |
|
} |
131 |
|
|
132 |
|
public List getLastInvocations() { |
133 |
4 |
return Collections.unmodifiableList(lastInvocationsList); |
134 |
|
} |
135 |
|
|
136 |
|
public AggregateOperationTree getTree() { |
137 |
0 |
return tree; |
138 |
|
} |
139 |
|
|
140 |
|
public int getMaxLastInvocations() { |
141 |
0 |
return maxLastInvocations; |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
public void setMaxLastInvocations(int max) { |
148 |
1 |
maxLastInvocations = max; |
149 |
1 |
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
public String[] getLayers() { |
155 |
0 |
return repository.getHierarchicalLayers(); |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
public long getTimeInLayer(String layer) { |
162 |
0 |
return repository.getTimeInHierarchicalLayer(layer); |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
public AggregateExecutionTime[] getExecutionsInLayer(String layer) { |
169 |
0 |
return repository.getExecutionsInHierarchicalLayer(layer); |
170 |
|
} |
171 |
|
|
172 |
|
public String toString() { |
173 |
2 |
return "Application Statistics[app=" + getApplicationName() + ", inst=" + getInstanceId() |
174 |
|
+ "] from " + startTime + ", until " + endTime; |
175 |
|
} |
176 |
|
|
177 |
|
public long getStartTime() { |
178 |
0 |
return startTime; |
179 |
|
} |
180 |
|
|
181 |
|
public long getEndTime() { |
182 |
0 |
return endTime; |
183 |
|
} |
184 |
|
|
185 |
|
void addToLastInvocations(Tree opTree) { |
186 |
4 |
if (opTree == null) { |
187 |
1 |
return; |
188 |
|
} |
189 |
|
|
190 |
3 |
if (lastInvocationsList.size() >= maxLastInvocations) { |
191 |
1 |
lastInvocationsList.removeLast(); |
192 |
|
} |
193 |
|
|
194 |
3 |
lastInvocationsList.addFirst(opTree); |
195 |
|
|
196 |
3 |
hasStatistics = true; |
197 |
3 |
} |
198 |
|
|
199 |
|
void addToLastInvocations(List trees) { |
200 |
0 |
for (Iterator i = trees.iterator(); i.hasNext();) { |
201 |
0 |
addToLastInvocations((Tree) i.next()); |
202 |
|
} |
203 |
0 |
} |
204 |
|
|
205 |
|
void mergeTree(Tree opTree) { |
206 |
1 |
if (opTree == null) { |
207 |
1 |
return; |
208 |
|
} |
209 |
|
|
210 |
0 |
this.tree.merge(opTree); |
211 |
0 |
hasStatistics = true; |
212 |
0 |
} |
213 |
|
|
214 |
|
void mergeTree(AggregateOperationTree aggTree) { |
215 |
0 |
if (aggTree == null) { |
216 |
0 |
return; |
217 |
|
} |
218 |
|
|
219 |
0 |
this.tree.merge(aggTree); |
220 |
0 |
hasStatistics = true; |
221 |
0 |
} |
222 |
|
|
223 |
|
void mergeStartAndEndTimes(OperationStatistics stats) { |
224 |
1 |
mergeStartAndEndTimes(stats.getStartTime(), stats.getEndTime()); |
225 |
1 |
} |
226 |
|
|
227 |
|
void mergeStartAndEndTimes(ApplicationStatistics stats) { |
228 |
0 |
mergeStartAndEndTimes(stats.getStartTime(), stats.getEndTime()); |
229 |
0 |
} |
230 |
|
|
231 |
|
void mergeStartAndEndTimes(long othersStartTime, class="keyword">long othersEndTime) { |
232 |
1 |
this.startTime = Math.min(class="keyword">this.startTime, othersStartTime); |
233 |
1 |
this.endTime = Math.max(class="keyword">this.endTime, othersEndTime); |
234 |
1 |
} |
235 |
|
|
236 |
|
void mergeLayerAndExecutionTimes(OperationStatistics stats) { |
237 |
1 |
String[] layers = stats.getLayers(); |
238 |
1 |
for (int i = 0; i < layers.length; i++) { |
239 |
0 |
String aLayer = layers[i]; |
240 |
0 |
mergeLayerTime(aLayer, stats.getTimeInLayer(aLayer)); |
241 |
0 |
mergeExecutionTimes(aLayer, stats.getExecutions(aLayer)); |
242 |
|
} |
243 |
1 |
} |
244 |
|
|
245 |
|
void mergeLayerAndExecutionTimes(ApplicationStatistics stats) { |
246 |
0 |
String[] layers = stats.getLayers(); |
247 |
0 |
for (int i = 0; i < layers.length; i++) { |
248 |
0 |
String aLayer = layers[i]; |
249 |
0 |
mergeLayerTime(aLayer, stats.getTimeInLayer(aLayer)); |
250 |
0 |
mergeExecutionTimes(aLayer, stats.getExecutionsInLayer(aLayer)); |
251 |
|
} |
252 |
0 |
} |
253 |
|
|
254 |
|
void mergeLayerTime(String layer, long time) { |
255 |
0 |
repository.mergeHierarchicalLayerTime(layer, time); |
256 |
0 |
hasStatistics = true; |
257 |
0 |
} |
258 |
|
|
259 |
|
void mergeExecutionTimes(String layerName, ExecutionTimer[] times) { |
260 |
0 |
repository.mergeExecutionTimes(layerName, times); |
261 |
0 |
hasStatistics = true; |
262 |
0 |
} |
263 |
|
|
264 |
|
void mergeExecutionTimes(String layerName, AggregateExecutionTime[] times) { |
265 |
0 |
repository.mergeExecutionTimes(layerName, times); |
266 |
0 |
hasStatistics = true; |
267 |
0 |
} |
268 |
|
} |