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 | } |
84 | |
|
85 | |
public boolean isEnabled() |
86 | |
{ |
87 | 0 | return enabled; |
88 | |
} |
89 | |
|
90 | |
public void logSummary() |
91 | |
{ |
92 | 0 | logSummary(new SimplePrinter(System.out)); |
93 | 0 | } |
94 | |
|
95 | |
public void logSummary(PrintWriter printer) |
96 | |
{ |
97 | 0 | printer.print(this); |
98 | 0 | } |
99 | |
|
100 | |
public void setEnabled(boolean b) |
101 | |
{ |
102 | 0 | this.enabled = b; |
103 | 0 | } |
104 | |
|
105 | |
public long getMaxExecutionTime() |
106 | |
{ |
107 | 0 | return maxExecutionTime; |
108 | |
} |
109 | |
|
110 | |
public long getMinExecutionTime() |
111 | |
{ |
112 | 0 | return minExecutionTime; |
113 | |
} |
114 | |
|
115 | |
public long getTotalExecutionTime() |
116 | |
{ |
117 | 0 | return totalExecTime; |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public long getExecutedEvents() |
124 | |
{ |
125 | 0 | return executedEvent; |
126 | |
} |
127 | |
|
128 | |
public synchronized void addExecutionTime(long time) |
129 | |
{ |
130 | 0 | if (statIntervalTimeEnabled) |
131 | |
{ |
132 | 0 | long currentTime = System.currentTimeMillis(); |
133 | 0 | if (currentIntervalStartTime == 0) |
134 | |
{ |
135 | 0 | currentIntervalStartTime = currentTime; |
136 | |
} |
137 | |
|
138 | 0 | if ((currentTime - currentIntervalStartTime) > intervalTime) |
139 | |
{ |
140 | 0 | clear(); |
141 | 0 | currentIntervalStartTime = currentTime; |
142 | |
} |
143 | |
} |
144 | |
|
145 | 0 | executedEvent++; |
146 | |
|
147 | 0 | totalExecTime += (time == 0 ? 1 : time); |
148 | |
|
149 | 0 | if (minExecutionTime == 0 || time < minExecutionTime) |
150 | |
{ |
151 | 0 | minExecutionTime = time; |
152 | |
} |
153 | 0 | if (maxExecutionTime == 0 || time > maxExecutionTime) |
154 | |
{ |
155 | 0 | maxExecutionTime = time; |
156 | |
} |
157 | 0 | averageExecutionTime = Math.round(totalExecTime / executedEvent); |
158 | 0 | } |
159 | |
|
160 | |
public long getAverageExecutionTime() |
161 | |
{ |
162 | 0 | return averageExecutionTime; |
163 | |
} |
164 | |
|
165 | |
} |