TimeStatisticImpl xref

View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.management;
    19
    20  import javax.management.j2ee.statistics.TimeStatistic;
    21
    22  /***
    23   * A time statistic implementation
    24   *
    25   * @version $Revision: 1.7 $
    26   */
    27  public class TimeStatisticImpl extends StatisticImpl implements TimeStatistic {
    28      private long count;
    29      private long maxTime;
    30      private long minTime;
    31      private long totalTime;
    32      private TimeStatisticImpl parent;
    33
    34      public TimeStatisticImpl(String name, String description) {
    35          this(name, "millis", description);
    36      }
    37
    38      public TimeStatisticImpl(TimeStatisticImpl parent, String name, String description) {
    39          this(name, description);
    40          this.parent = parent;
    41      }
    42
    43      public TimeStatisticImpl(String name, String unit, String description) {
    44          super(name, unit, description);
    45      }
    46
    47      public synchronized void reset() {
    48          super.reset();
    49          count = 0;
    50          maxTime = 0;
    51          minTime = 0;
    52          totalTime = 0;
    53      }
    54
    55      public synchronized long getCount() {
    56          return count;
    57      }
    58
    59      public synchronized void addTime(long time) {
    60          count++;
    61          totalTime += time;
    62          if (time > maxTime) {
    63              maxTime = time;
    64          }
    65          if (time < minTime || minTime == 0) {
    66              minTime = time;
    67          }
    68          updateSampleTime();
    69          if (parent != null) {
    70              parent.addTime(time);
    71          }
    72      }
    73
    74      /***
    75       * @return the maximum time of any step
    76       */
    77      public long getMaxTime() {
    78          return maxTime;
    79      }
    80
    81      /***
    82       * @return the minimum time of any step
    83       */
    84      public synchronized long getMinTime() {
    85          return minTime;
    86      }
    87
    88      /***
    89       * @return the total time of all the steps added together
    90       */
    91      public synchronized long getTotalTime() {
    92          return totalTime;
    93      }
    94
    95      /***
    96       * @return the average time calculated by dividing the
    97       *         total time by the number of counts
    98       */
    99      public synchronized double getAverageTime() {
    100         if (count == 0) {
    101             return 0;
    102         }
    103         double d = totalTime;
    104         return d / count;
    105     }
    106
    107
    108     /***
    109      * @return the average time calculated by dividing the
    110      *         total time by the number of counts but excluding the
    111      *         minimum and maximum times.
    112      */
    113     public synchronized double getAverageTimeExcludingMinMax() {
    114         if (count <= 2) {
    115             return 0;
    116         }
    117         double d = totalTime - minTime - maxTime;
    118         return d / (count - 2);
    119     }
    120
    121
    122     /***
    123      * @return the average number of steps per second
    124      */
    125     public double getAveragePerSecond() {
    126         double d = 1000;
    127         double averageTime = getAverageTime();
    128         if (averageTime == 0) {
    129             return 0;
    130         }
    131         return d / averageTime;
    132     }
    133
    134     /***
    135      * @return the average number of steps per second excluding the min & max values
    136      */
    137     public double getAveragePerSecondExcludingMinMax() {
    138         double d = 1000;
    139         double average = getAverageTimeExcludingMinMax();
    140         if (average == 0) {
    141             return 0;
    142         }
    143         return d / average;
    144     }
    145
    146     public TimeStatisticImpl getParent() {
    147         return parent;
    148     }
    149
    150     public void setParent(TimeStatisticImpl parent) {
    151         this.parent = parent;
    152     }
    153
    154     protected synchronized void appendFieldDescription(StringBuffer buffer) {
    155         buffer.append(" count: ");
    156         buffer.append(Long.toString(count));
    157         buffer.append(" maxTime: ");
    158         buffer.append(Long.toString(maxTime));
    159         buffer.append(" minTime: ");
    160         buffer.append(Long.toString(minTime));
    161         buffer.append(" totalTime: ");
    162         buffer.append(Long.toString(totalTime));
    163         buffer.append(" averageTime: ");
    164         buffer.append(Double.toString(getAverageTime()));
    165         buffer.append(" averageTimeExMinMax: ");
    166         buffer.append(Double.toString(getAverageTimeExcludingMinMax()));
    167         buffer.append(" averagePerSecond: ");
    168         buffer.append(Double.toString(getAveragePerSecond()));
    169         buffer.append(" averagePerSecondExMinMax: ");
    170         buffer.append(Double.toString(getAveragePerSecondExcludingMinMax()));
    171         super.appendFieldDescription(buffer);
    172     }
    173
    174 }