Coverage Report - org.mule.management.stats.ComponentStatistics
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentStatistics
0%
0/74
0%
0/32
1.929
 
 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  0
     protected final Log logger = LogFactory.getLog(getClass());
 29  
 
 30  
     /**
 31  
      * Serial version
 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  
      * 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  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  
                 // just disable it
 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
         averageExecutionTime = 0;
 84  0
     }
 85  
 
 86  
     public boolean isEnabled()
 87  
     {
 88  0
         return enabled;
 89  
     }
 90  
 
 91  
     public void logSummary()
 92  
     {
 93  0
         logSummary(new SimplePrinter(System.out));
 94  0
     }
 95  
 
 96  
     public void logSummary(PrintWriter printer)
 97  
     {
 98  0
         printer.print(this);
 99  0
     }
 100  
 
 101  
     public void setEnabled(boolean b)
 102  
     {
 103  0
         this.enabled = b;
 104  0
     }
 105  
 
 106  
     public long getMaxExecutionTime()
 107  
     {
 108  0
         return maxExecutionTime;
 109  
     }
 110  
 
 111  
     public long getMinExecutionTime()
 112  
     {
 113  0
         return minExecutionTime;
 114  
     }
 115  
 
 116  
     public long getTotalExecutionTime()
 117  
     {
 118  0
         return totalExecTime;
 119  
     }
 120  
 
 121  
     /*
 122  
      * executedEvents is since interval started
 123  
      */
 124  
     public long getExecutedEvents()
 125  
     {
 126  0
         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  0
         if (statIntervalTimeEnabled)
 138  
         {
 139  0
             long currentTime = System.currentTimeMillis();
 140  0
             if (currentIntervalStartTime == 0)
 141  
             {
 142  0
                 currentIntervalStartTime = currentTime;
 143  
             }
 144  
 
 145  0
             if ((currentTime - currentIntervalStartTime) > intervalTime)
 146  
             {
 147  0
                 clear();
 148  0
                 currentIntervalStartTime = currentTime;
 149  
             }
 150  
         }
 151  
 
 152  0
         if (first)
 153  
         {
 154  0
             executedEvent++;
 155  
         }
 156  
 
 157  0
         totalExecTime += ProcessingTime.getEffectiveTime(branch);
 158  0
         long effectiveTotal = ProcessingTime.getEffectiveTime(total);
 159  0
         if (maxExecutionTime == 0 || effectiveTotal > maxExecutionTime)
 160  
         {
 161  0
             maxExecutionTime = effectiveTotal;
 162  
         }
 163  0
         averageExecutionTime = Math.round(totalExecTime / executedEvent);
 164  0
     }
 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  0
         long effectiveTime = ProcessingTime.getEffectiveTime(time);
 172  0
         if (minExecutionTime == 0 || effectiveTime < minExecutionTime)
 173  
         {
 174  0
             minExecutionTime = effectiveTime;
 175  
         }
 176  0
     }
 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  0
         if (statIntervalTimeEnabled)
 186  
         {
 187  0
             long currentTime = System.currentTimeMillis();
 188  0
             if (currentIntervalStartTime == 0)
 189  
             {
 190  0
                 currentIntervalStartTime = currentTime;
 191  
             }
 192  
 
 193  0
             if ((currentTime - currentIntervalStartTime) > intervalTime)
 194  
             {
 195  0
                 clear();
 196  0
                 currentIntervalStartTime = currentTime;
 197  
             }
 198  
         }
 199  
 
 200  0
         executedEvent++;
 201  
 
 202  0
         long effectiveTime = ProcessingTime.getEffectiveTime(time);
 203  0
         totalExecTime += effectiveTime;
 204  
 
 205  0
         if (minExecutionTime == 0 || effectiveTime < minExecutionTime)
 206  
         {
 207  0
             minExecutionTime = time;
 208  
         }
 209  0
         if (maxExecutionTime == 0 || effectiveTime > maxExecutionTime)
 210  
         {
 211  0
             maxExecutionTime = time;
 212  
         }
 213  0
         averageExecutionTime = Math.round(totalExecTime / executedEvent);
 214  0
     }
 215  
 
 216  
     public long getAverageExecutionTime()
 217  
     {
 218  0
         return averageExecutionTime;
 219  
     }
 220  
 
 221  
 }