View Javadoc

1   /*
2    * $Id: ComponentStatistics.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.management.stats;
12  
13  import org.mule.management.stats.printers.SimplePrinter;
14  
15  import java.io.PrintWriter;
16  
17  public class ComponentStatistics implements Statistics
18  {
19      /**
20       * Serial version
21       */
22      private static final long serialVersionUID = -2086999226732861674L;
23  
24      private String name;
25      private long totalExecTime = 0;
26      private long receivedEventSync = 0;
27      private long receivedEventASync = 0;
28      private long queuedEvent = 0;
29      private long maxQueuedEvent = 0;
30      private long averageQueueSize = 0;
31      private long totalQueuedEvent = 0;
32      private long sentEventSync = 0;
33      private long sentReplyToEvent = 0;
34      private long sentEventASync = 0;
35      private long executedEvent = 0;
36      private long executionError = 0;
37      private long fatalError = 0;
38      private long minExecutionTime = 0;
39      private long maxExecutionTime = 0;
40      private long averageExecutionTime = 0;
41      private boolean enabled = false;
42  
43      private int threadPoolSize = 0;
44      private long samplePeriod = 0;
45  
46      private RouterStatistics inboundRouterStat = null;
47      private RouterStatistics outboundRouterStat = null;
48  
49      /**
50       * The constructor
51       *
52       * @param name
53       */
54      public ComponentStatistics(String name, int threadPoolSize)
55      {
56          super();
57          this.name = name;
58  
59          this.threadPoolSize = threadPoolSize;
60          clear();
61      }
62  
63      /**
64       * Are statistics logged
65       */
66      public boolean isEnabled()
67      {
68          return enabled;
69      }
70  
71      /**
72       * Enable statistics logs (this is a dynamic parameter)
73       */
74      public synchronized void setEnabled(boolean b)
75      {
76          enabled = b;
77  
78          if (inboundRouterStat != null)
79          {
80              inboundRouterStat.setEnabled(b);
81          }
82          if (outboundRouterStat != null)
83          {
84              outboundRouterStat.setEnabled(b);
85          }
86      }
87  
88      public synchronized void incReceivedEventSync()
89      {
90          receivedEventSync++;
91      }
92  
93      public synchronized void incReceivedEventASync()
94      {
95          receivedEventASync++;
96      }
97  
98      public synchronized void incExecutionError()
99      {
100         executionError++;
101     }
102 
103     public synchronized void incFatalError()
104     {
105         fatalError++;
106     }
107 
108     public synchronized void incSentEventSync()
109     {
110         sentEventSync++;
111     }
112 
113     public synchronized void incSentEventASync()
114     {
115         sentEventASync++;
116     }
117 
118     public synchronized void incSentReplyToEvent()
119     {
120         sentReplyToEvent++;
121     }
122 
123     public synchronized void incQueuedEvent()
124     {
125         queuedEvent++;
126         totalQueuedEvent++;
127         if (queuedEvent > maxQueuedEvent)
128         {
129             maxQueuedEvent = queuedEvent;
130         }
131         // if(queuedEvent > 1) {
132         averageQueueSize = Math.round(getAsyncEventsReceived() / totalQueuedEvent);
133         // }
134     }
135 
136     public synchronized void decQueuedEvent()
137     {
138         queuedEvent--;
139     }
140 
141     public synchronized void addExecutionTime(long time)
142     {
143         executedEvent++;
144 
145         totalExecTime += (time == 0 ? 1 : time);
146 
147         if (minExecutionTime == 0 || time < minExecutionTime)
148         {
149             minExecutionTime = time;
150         }
151         if (maxExecutionTime == 0 || time > maxExecutionTime)
152         {
153             maxExecutionTime = time;
154         }
155         averageExecutionTime = Math.round(totalExecTime / executedEvent);
156     }
157 
158     public long getAverageExecutionTime()
159     {
160         return averageExecutionTime;
161     }
162 
163     public long getAverageQueueSize()
164     {
165         return averageQueueSize;
166     }
167 
168     public long getMaxQueueSize()
169     {
170         return maxQueuedEvent;
171     }
172 
173     public long getMaxExecutionTime()
174     {
175         return maxExecutionTime;
176     }
177 
178     public long getFatalErrors()
179     {
180         return fatalError;
181     }
182 
183     public long getMinExecutionTime()
184     {
185         return minExecutionTime;
186     }
187 
188     public long getTotalExecutionTime()
189     {
190         return totalExecTime;
191     }
192 
193     public long getQueuedEvents()
194     {
195         return queuedEvent;
196     }
197 
198     public long getAsyncEventsReceived()
199     {
200         return receivedEventASync;
201     }
202 
203     public long getSyncEventsReceived()
204     {
205         return receivedEventSync;
206     }
207 
208     public long getReplyToEventsSent()
209     {
210         return sentReplyToEvent;
211     }
212 
213     public long getSyncEventsSent()
214     {
215         return sentEventSync;
216     }
217 
218     public long getAsyncEventsSent()
219     {
220         return sentEventASync;
221     }
222 
223     public long getTotalEventsSent()
224     {
225         return getSyncEventsSent() + getAsyncEventsSent();
226     }
227 
228     public long getTotalEventsReceived()
229     {
230         return getSyncEventsReceived() + getAsyncEventsReceived();
231     }
232 
233     public long getExecutedEvents()
234     {
235         return executedEvent;
236     }
237 
238     public long getExecutionErrors()
239     {
240         return executionError;
241     }
242 
243     public synchronized String getName()
244     {
245         return name;
246     }
247 
248     public synchronized void setName(String name)
249     {
250         this.name = name;
251     }
252 
253     /**
254      * log in info level the main statistics
255      */
256     public void logSummary()
257     {
258         logSummary(new SimplePrinter(System.out));
259     }
260 
261     public void logSummary(PrintWriter printer)
262     {
263         printer.print(this);
264     }
265 
266     public synchronized void clear()
267     {
268 
269         totalExecTime = 0;
270         receivedEventSync = 0;
271         receivedEventASync = 0;
272         queuedEvent = 0;
273         maxQueuedEvent = 0;
274         totalQueuedEvent = 0;
275         averageQueueSize = 0;
276 
277         sentEventSync = 0;
278         sentEventASync = 0;
279         sentReplyToEvent = 0;
280 
281         executedEvent = 0;
282         executionError = 0;
283         fatalError = 0;
284 
285         minExecutionTime = 0;
286         maxExecutionTime = 0;
287 
288         if (getInboundRouterStat() != null)
289         {
290             getInboundRouterStat().clear();
291         }
292         if (getOutboundRouterStat() != null)
293         {
294             getOutboundRouterStat().clear();
295         }
296 
297         samplePeriod = System.currentTimeMillis();
298 
299     }
300 
301     /**
302      * @return Returns the inboundRouterStat.
303      */
304     public RouterStatistics getInboundRouterStat()
305     {
306         return inboundRouterStat;
307     }
308 
309     /**
310      * @param inboundRouterStat The inboundRouterStat to set.
311      */
312     public void setInboundRouterStat(RouterStatistics inboundRouterStat)
313     {
314         this.inboundRouterStat = inboundRouterStat;
315         this.inboundRouterStat.setEnabled(enabled);
316     }
317 
318     /**
319      * @return Returns the outboundRouterStat.
320      */
321     public RouterStatistics getOutboundRouterStat()
322     {
323         return outboundRouterStat;
324     }
325 
326     /**
327      * @param outboundRouterStat The outboundRouterStat to set.
328      */
329     public void setOutboundRouterStat(RouterStatistics outboundRouterStat)
330     {
331         this.outboundRouterStat = outboundRouterStat;
332         this.outboundRouterStat.setEnabled(enabled);
333     }
334 
335     public int getThreadPoolSize()
336     {
337         return threadPoolSize;
338     }
339 
340     public long getSamplePeriod()
341     {
342         return System.currentTimeMillis() - samplePeriod;
343     }
344 }