1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.management.stats; |
12 | |
|
13 | |
import org.mule.api.management.stats.Statistics; |
14 | |
import org.mule.management.stats.printers.SimplePrinter; |
15 | |
import org.mule.util.StringUtils; |
16 | |
|
17 | |
import java.io.PrintWriter; |
18 | |
|
19 | |
import org.apache.commons.logging.Log; |
20 | |
import org.apache.commons.logging.LogFactory; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
public class ComponentStatistics implements Statistics |
26 | |
{ |
27 | |
|
28 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
private static final long serialVersionUID = -2086999226732861674L; |
34 | |
|
35 | 0 | private long minExecutionTime = 0; |
36 | 0 | private long maxExecutionTime = 0; |
37 | 0 | private long averageExecutionTime = 0; |
38 | 0 | private long executedEvent = 0; |
39 | 0 | private long totalExecTime = 0; |
40 | 0 | private boolean enabled = false; |
41 | 0 | private long intervalTime = 0; |
42 | 0 | private long currentIntervalStartTime = 0; |
43 | 0 | private boolean statIntervalTimeEnabled = false; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public ComponentStatistics() |
55 | 0 | { |
56 | 0 | String intervalTimeString = System.getProperty("statIntervalTime"); |
57 | 0 | if (StringUtils.isBlank(intervalTimeString)) |
58 | |
{ |
59 | 0 | statIntervalTimeEnabled = false; |
60 | |
} |
61 | |
else |
62 | |
{ |
63 | |
try |
64 | |
{ |
65 | 0 | intervalTime = Integer.parseInt(intervalTimeString); |
66 | 0 | statIntervalTimeEnabled = true; |
67 | |
} |
68 | 0 | catch (NumberFormatException e) |
69 | |
{ |
70 | |
|
71 | 0 | statIntervalTimeEnabled = false; |
72 | 0 | logger.warn("Couldn't parse statIntervalTime: " + intervalTimeString + ". Disabled."); |
73 | 0 | } |
74 | |
} |
75 | 0 | } |
76 | |
|
77 | |
public void clear() |
78 | |
{ |
79 | 0 | minExecutionTime = 0; |
80 | 0 | maxExecutionTime = 0; |
81 | 0 | executedEvent = 0; |
82 | 0 | totalExecTime = 0; |
83 | 0 | averageExecutionTime = 0; |
84 | 0 | } |
85 | |
|
86 | |
public boolean isEnabled() |
87 | |
{ |
88 | 0 | return enabled; |
89 | |
} |
90 | |
|
91 | |
public void logSummary() |
92 | |
{ |
93 | 0 | logSummary(new SimplePrinter(System.out)); |
94 | 0 | } |
95 | |
|
96 | |
public void logSummary(PrintWriter printer) |
97 | |
{ |
98 | 0 | printer.print(this); |
99 | 0 | } |
100 | |
|
101 | |
public void setEnabled(boolean b) |
102 | |
{ |
103 | 0 | this.enabled = b; |
104 | 0 | } |
105 | |
|
106 | |
public long getMaxExecutionTime() |
107 | |
{ |
108 | 0 | return maxExecutionTime; |
109 | |
} |
110 | |
|
111 | |
public long getMinExecutionTime() |
112 | |
{ |
113 | 0 | return minExecutionTime; |
114 | |
} |
115 | |
|
116 | |
public long getTotalExecutionTime() |
117 | |
{ |
118 | 0 | return totalExecTime; |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public long getExecutedEvents() |
125 | |
{ |
126 | 0 | return executedEvent; |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public synchronized void addExecutionBranchTime(boolean first, long branch, long total) |
136 | |
{ |
137 | 0 | if (statIntervalTimeEnabled) |
138 | |
{ |
139 | 0 | long currentTime = System.currentTimeMillis(); |
140 | 0 | if (currentIntervalStartTime == 0) |
141 | |
{ |
142 | 0 | currentIntervalStartTime = currentTime; |
143 | |
} |
144 | |
|
145 | 0 | if ((currentTime - currentIntervalStartTime) > intervalTime) |
146 | |
{ |
147 | 0 | clear(); |
148 | 0 | currentIntervalStartTime = currentTime; |
149 | |
} |
150 | |
} |
151 | |
|
152 | 0 | if (first) |
153 | |
{ |
154 | 0 | executedEvent++; |
155 | |
} |
156 | |
|
157 | 0 | totalExecTime += ProcessingTime.getEffectiveTime(branch); |
158 | 0 | long effectiveTotal = ProcessingTime.getEffectiveTime(total); |
159 | 0 | if (maxExecutionTime == 0 || effectiveTotal > maxExecutionTime) |
160 | |
{ |
161 | 0 | maxExecutionTime = effectiveTotal; |
162 | |
} |
163 | 0 | averageExecutionTime = Math.round(totalExecTime / executedEvent); |
164 | 0 | } |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
public synchronized void addCompleteExecutionTime(long time) |
170 | |
{ |
171 | 0 | long effectiveTime = ProcessingTime.getEffectiveTime(time); |
172 | 0 | if (minExecutionTime == 0 || effectiveTime < minExecutionTime) |
173 | |
{ |
174 | 0 | minExecutionTime = effectiveTime; |
175 | |
} |
176 | 0 | } |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
public synchronized void addExecutionTime(long time) |
184 | |
{ |
185 | 0 | if (statIntervalTimeEnabled) |
186 | |
{ |
187 | 0 | long currentTime = System.currentTimeMillis(); |
188 | 0 | if (currentIntervalStartTime == 0) |
189 | |
{ |
190 | 0 | currentIntervalStartTime = currentTime; |
191 | |
} |
192 | |
|
193 | 0 | if ((currentTime - currentIntervalStartTime) > intervalTime) |
194 | |
{ |
195 | 0 | clear(); |
196 | 0 | currentIntervalStartTime = currentTime; |
197 | |
} |
198 | |
} |
199 | |
|
200 | 0 | executedEvent++; |
201 | |
|
202 | 0 | long effectiveTime = ProcessingTime.getEffectiveTime(time); |
203 | 0 | totalExecTime += effectiveTime; |
204 | |
|
205 | 0 | if (minExecutionTime == 0 || effectiveTime < minExecutionTime) |
206 | |
{ |
207 | 0 | minExecutionTime = time; |
208 | |
} |
209 | 0 | if (maxExecutionTime == 0 || effectiveTime > maxExecutionTime) |
210 | |
{ |
211 | 0 | maxExecutionTime = time; |
212 | |
} |
213 | 0 | averageExecutionTime = Math.round(totalExecTime / executedEvent); |
214 | 0 | } |
215 | |
|
216 | |
public long getAverageExecutionTime() |
217 | |
{ |
218 | 0 | return averageExecutionTime; |
219 | |
} |
220 | |
|
221 | |
} |