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  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.management.stats;
 8  
 
 9  
 import org.mule.api.management.stats.Statistics;
 10  
 import org.mule.management.stats.printers.SimplePrinter;
 11  
 import org.mule.util.StringUtils;
 12  
 
 13  
 import java.io.PrintWriter;
 14  
 
 15  
 import org.apache.commons.logging.Log;
 16  
 import org.apache.commons.logging.LogFactory;
 17  
 
 18  
 /**
 19  
  * 
 20  
  */
 21  
 public class ComponentStatistics implements Statistics
 22  
 {
 23  
 
 24  0
     protected final Log logger = LogFactory.getLog(getClass());
 25  
 
 26  
     /**
 27  
      * Serial version
 28  
      */
 29  
     private static final long serialVersionUID = -2086999226732861674L;
 30  
 
 31  0
     private long minExecutionTime = 0;
 32  0
     private long maxExecutionTime = 0;
 33  0
     private long averageExecutionTime = 0;
 34  0
     private long executedEvent = 0;
 35  0
     private long totalExecTime = 0;
 36  0
     private boolean enabled = false;
 37  0
     private long intervalTime = 0;
 38  0
     private long currentIntervalStartTime = 0;
 39  0
     private boolean statIntervalTimeEnabled = false;
 40  
 
 41  
     /**
 42  
      * The constructor added to initialize the interval time in ms that stats   
 43  
      * are measured for from the property statIntervalTime. If the property is 
 44  
      * not set or cannot be parsed, disable interval time and just compute 
 45  
      * stats from start of mule.
 46  
      * TODO: The code to create and use an interval time for measuring average execution 
 47  
      * time could be removed once a complete solution is available in MuleHQ to
 48  
      * monitor this
 49  
      */
 50  
     public ComponentStatistics() 
 51  0
     {
 52  0
         String intervalTimeString = System.getProperty("statIntervalTime");
 53  0
         if (StringUtils.isBlank(intervalTimeString))
 54  
         {
 55  0
             statIntervalTimeEnabled = false;
 56  
         }
 57  
         else
 58  
         {
 59  
             try
 60  
             {
 61  0
                 intervalTime = Integer.parseInt(intervalTimeString);
 62  0
                 statIntervalTimeEnabled = true;
 63  
             }
 64  0
             catch (NumberFormatException e)
 65  
             {
 66  
                 // just disable it
 67  0
                 statIntervalTimeEnabled = false;
 68  0
                 logger.warn("Couldn't parse statIntervalTime: " + intervalTimeString + ". Disabled.");
 69  0
             }
 70  
         }
 71  0
     }
 72  
 
 73  
     public void clear()
 74  
     {
 75  0
         minExecutionTime = 0;
 76  0
         maxExecutionTime = 0;
 77  0
         executedEvent = 0;
 78  0
         totalExecTime = 0;
 79  0
         averageExecutionTime = 0;
 80  0
     }
 81  
 
 82  
     public boolean isEnabled()
 83  
     {
 84  0
         return enabled;
 85  
     }
 86  
 
 87  
     public void logSummary()
 88  
     {
 89  0
         logSummary(new SimplePrinter(System.out));
 90  0
     }
 91  
 
 92  
     public void logSummary(PrintWriter printer)
 93  
     {
 94  0
         printer.print(this);
 95  0
     }
 96  
 
 97  
     public void setEnabled(boolean b)
 98  
     {
 99  0
         this.enabled = b;
 100  0
     }
 101  
 
 102  
     public long getMaxExecutionTime()
 103  
     {
 104  0
         return maxExecutionTime;
 105  
     }
 106  
 
 107  
     public long getMinExecutionTime()
 108  
     {
 109  0
         return minExecutionTime;
 110  
     }
 111  
 
 112  
     public long getTotalExecutionTime()
 113  
     {
 114  0
         return totalExecTime;
 115  
     }
 116  
 
 117  
     /*
 118  
      * executedEvents is since interval started
 119  
      */
 120  
     public long getExecutedEvents()
 121  
     {
 122  0
         return executedEvent;
 123  
     }
 124  
 
 125  
     /**
 126  
      * Add a new execution-time measurement for one branch of processing an event
 127  
      * @param first true if this is the first branch for this event
 128  
      * @param branch the time to execute this branch
 129  
      * @param total the total time (so far) for  processing this event
 130  
      */
 131  
     public synchronized void addExecutionBranchTime(boolean first, long branch, long total)
 132  
     {
 133  0
         if (statIntervalTimeEnabled)
 134  
         {
 135  0
             long currentTime = System.currentTimeMillis();
 136  0
             if (currentIntervalStartTime == 0)
 137  
             {
 138  0
                 currentIntervalStartTime = currentTime;
 139  
             }
 140  
 
 141  0
             if ((currentTime - currentIntervalStartTime) > intervalTime)
 142  
             {
 143  0
                 clear();
 144  0
                 currentIntervalStartTime = currentTime;
 145  
             }
 146  
         }
 147  
 
 148  0
         if (first)
 149  
         {
 150  0
             executedEvent++;
 151  
         }
 152  
 
 153  0
         totalExecTime += ProcessingTime.getEffectiveTime(branch);
 154  0
         long effectiveTotal = ProcessingTime.getEffectiveTime(total);
 155  0
         if (maxExecutionTime == 0 || effectiveTotal > maxExecutionTime)
 156  
         {
 157  0
             maxExecutionTime = effectiveTotal;
 158  
         }
 159  0
         averageExecutionTime = Math.round(totalExecTime / executedEvent);
 160  0
     }
 161  
 
 162  
     /**
 163  
      * Add the complete execution time for a flow that also reports branhc execution times
 164  
      */
 165  
     public synchronized void addCompleteExecutionTime(long time)
 166  
     {
 167  0
         long effectiveTime = ProcessingTime.getEffectiveTime(time);
 168  0
         if (minExecutionTime == 0 || effectiveTime < minExecutionTime)
 169  
         {
 170  0
             minExecutionTime = effectiveTime;
 171  
         }
 172  0
     }
 173  
 
 174  
     /**
 175  
      * Add a new execution-time measurement for processing an event.
 176  
      *
 177  
      * @param time
 178  
      */
 179  
     public synchronized void addExecutionTime(long time)
 180  
     {
 181  0
         if (statIntervalTimeEnabled)
 182  
         {
 183  0
             long currentTime = System.currentTimeMillis();
 184  0
             if (currentIntervalStartTime == 0)
 185  
             {
 186  0
                 currentIntervalStartTime = currentTime;
 187  
             }
 188  
 
 189  0
             if ((currentTime - currentIntervalStartTime) > intervalTime)
 190  
             {
 191  0
                 clear();
 192  0
                 currentIntervalStartTime = currentTime;
 193  
             }
 194  
         }
 195  
 
 196  0
         executedEvent++;
 197  
 
 198  0
         long effectiveTime = ProcessingTime.getEffectiveTime(time);
 199  0
         totalExecTime += effectiveTime;
 200  
 
 201  0
         if (minExecutionTime == 0 || effectiveTime < minExecutionTime)
 202  
         {
 203  0
             minExecutionTime = time;
 204  
         }
 205  0
         if (maxExecutionTime == 0 || effectiveTime > maxExecutionTime)
 206  
         {
 207  0
             maxExecutionTime = time;
 208  
         }
 209  0
         averageExecutionTime = Math.round(totalExecTime / executedEvent);
 210  0
     }
 211  
 
 212  
     public long getAverageExecutionTime()
 213  
     {
 214  0
         return averageExecutionTime;
 215  
     }
 216  
 
 217  
 }