Using curam.util.jmx.NumericalCounterStatisticsAggregator

The following example shows how to use curam.util.jmx. NumericalCounterStatisticsAggregator and curam.util.jmx.NumericalCounterStatistics to calculate and make available various arithmetic values for a numerical counter (average, minimum, maximum and standard deviation).

Figure 1. Using NumericalCounterStatisticsAggregator and NumericCounterStatistics
import curam.util.jmx.NumericCounterStatisticsAggregator;
...
    /** Elapsed time statistics. */
    private NumericCounterStatisticsAggregator
                              elapsedTimeStats;

    /** Error counter. */
    private AtomicLong errors;

    /** Constructor. */
    MyClass() {
      super();
      errors = new AtomicLong(0);
      elapsedTimeStats =
           new NumericCounterStatisticsAggregator();
    }

    /**
     * Get the number of invocations.
     *
     * @return the number of invocations.
     */
    long getInvocations() {
      return this.elapsedTimeStats
                    .getNumberOfSamples();
    }

    /**
     * Get elapsed time statistics.
     *
     * @return elapsed time statistics.
     */
    NumericCounterStatistics getElapsedTimeStats() {
      return elapsedTimeStats.getAll();
    }

    /**
     * Get error counter.
     *
     * @return error counter.
     */
    long getErrors() {
      return errors.get();
    }

 /**
  * Add a statistics sample.
  *
  * @param elapsedTime the elapsed time.
  * @param error true if invocation ended in error.
  */
 void addStats(long elapsedTime, boolean error) {
  boolean reset = this.elapsedTimeStats
                           .add(elapsedTime);
  if(reset) {
   // Long.MAX_VALUE overflow
   errors.set(0);
  } else if(error){
   if(errors.incrementAndGet() < 0) {
    // Long.MAX_VALUE overflow
    this.elapsedTimeStats.reset();
   }
  }
 }
...