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