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