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.activeio.stats;
19  
20  import javax.management.j2ee.statistics.TimeStatistic;
21  
22  /***
23   * Shamelessly taken from the ActiveMQ project ( http://activemq.com )
24   * A time statistic implementation
25   *
26   * @version $Revision: 1.1 $
27   */
28  public class TimeStatisticImpl extends StatisticImpl implements TimeStatistic {
29      private long count;
30      private long maxTime;
31      private long minTime;
32      private long totalTime;
33      private TimeStatisticImpl parent;
34  
35      public TimeStatisticImpl(String name, String description) {
36          this(name, "millis", description);
37      }
38  
39      public TimeStatisticImpl(TimeStatisticImpl parent, String name, String description) {
40          this(name, description);
41          this.parent = parent;
42      }
43  
44      public TimeStatisticImpl(String name, String unit, String description) {
45          super(name, unit, description);
46      }
47  
48      public synchronized void reset() {
49          super.reset();
50          count = 0;
51          maxTime = 0;
52          minTime = 0;
53          totalTime = 0;
54      }
55  
56      public synchronized long getCount() {
57          return count;
58      }
59  
60      public synchronized void addTime(long time) {
61          count++;
62          totalTime += time;
63          if (time > maxTime) {
64              maxTime = time;
65          }
66          if (time < minTime || minTime == 0) {
67              minTime = time;
68          }
69          updateSampleTime();
70          if (parent != null) {
71              parent.addTime(time);
72          }
73      }
74  
75      /***
76       * @return the maximum time of any step
77       */
78      public long getMaxTime() {
79          return maxTime;
80      }
81  
82      /***
83       * @return the minimum time of any step
84       */
85      public synchronized long getMinTime() {
86          return minTime;
87      }
88  
89      /***
90       * @return the total time of all the steps added together
91       */
92      public synchronized long getTotalTime() {
93          return totalTime;
94      }
95  
96      /***
97       * @return the average time calculated by dividing the
98       *         total time by the number of counts
99       */
100     public synchronized double getAverageTime() {
101         if (count == 0) {
102             return 0;
103         }
104         double d = totalTime;
105         return d / count;
106     }
107 
108 
109     /***
110      * @return the average time calculated by dividing the
111      *         total time by the number of counts but excluding the
112      *         minimum and maximum times.
113      */
114     public synchronized double getAverageTimeExcludingMinMax() {
115         if (count <= 2) {
116             return 0;
117         }
118         double d = totalTime - minTime - maxTime;
119         return d / (count - 2);
120     }
121 
122 
123     /***
124      * @return the average number of steps per second
125      */
126     public double getAveragePerSecond() {
127         double d = 1000;
128         double averageTime = getAverageTime();
129         if (averageTime == 0) {
130             return 0;
131         }
132         return d / averageTime;
133     }
134 
135     /***
136      * @return the average number of steps per second excluding the min & max values
137      */
138     public double getAveragePerSecondExcludingMinMax() {
139         double d = 1000;
140         double average = getAverageTimeExcludingMinMax();
141         if (average == 0) {
142             return 0;
143         }
144         return d / average;
145     }
146 
147     public TimeStatisticImpl getParent() {
148         return parent;
149     }
150 
151     public void setParent(TimeStatisticImpl parent) {
152         this.parent = parent;
153     }
154 
155     protected synchronized void appendFieldDescription(StringBuffer buffer) {
156         buffer.append(" count: ");
157         buffer.append(Long.toString(count));
158         buffer.append(" maxTime: ");
159         buffer.append(Long.toString(maxTime));
160         buffer.append(" minTime: ");
161         buffer.append(Long.toString(minTime));
162         buffer.append(" totalTime: ");
163         buffer.append(Long.toString(totalTime));
164         buffer.append(" averageTime: ");
165         buffer.append(Double.toString(getAverageTime()));
166         buffer.append(" averageTimeExMinMax: ");
167         buffer.append(Double.toString(getAverageTimeExcludingMinMax()));
168         buffer.append(" averagePerSecond: ");
169         buffer.append(Double.toString(getAveragePerSecond()));
170         buffer.append(" averagePerSecondExMinMax: ");
171         buffer.append(Double.toString(getAveragePerSecondExcludingMinMax()));
172         super.appendFieldDescription(buffer);
173     }
174 
175 }