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