View Javadoc

1   /*
2    * $Id: ComponentStatistics.java 20343 2010-11-24 21:02:10Z mike.schilling $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.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       * Serial version
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       * The constructor added to initialize the interval time in ms that stats   
47       * are measured for from the property statIntervalTime. If the property is 
48       * not set or cannot be parsed, disable interval time and just compute 
49       * stats from start of mule.
50       * TODO: The code to create and use an interval time for measuring average execution 
51       * time could be removed once a complete solution is available in MuleHQ to
52       * monitor this
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                  // just disable it
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      * executedEvents is since interval started
123      */
124     public long getExecutedEvents()
125     {
126         return executedEvent;
127     }
128 
129     /**
130      * Add a new execution-time measurement for one branch of processing an event
131      * @param first true if this is the first branch for this event
132      * @param branch the time to execute this branch
133      * @param total the total time (so far) for  processing this event
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      * Add the complete execution time for a flow that also reports branhc execution times
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      * Add a new execution-time measurement for processing an event.
180      *
181      * @param time
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 }