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