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.5 $ 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 void reset() { 48 super.reset(); 49 count = 0; 50 maxTime = 0; 51 minTime = 0; 52 totalTime = 0; 53 } 54 55 public long getCount() { 56 return count; 57 } 58 59 public 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 long getMinTime() { 85 return minTime; 86 } 87 88 /*** 89 * @return the total time of all the steps added together 90 */ 91 public 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 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 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 return d / getAverageTime(); 128 } 129 130 /*** 131 * @return the average number of steps per second excluding the min & max values 132 */ 133 public double getAveragePerSecondExcludingMinMax() { 134 double d = 1000; 135 return d / getAverageTimeExcludingMinMax(); 136 } 137 138 public TimeStatisticImpl getParent() { 139 return parent; 140 } 141 142 public void setParent(TimeStatisticImpl parent) { 143 this.parent = parent; 144 } 145 146 protected void appendFieldDescription(StringBuffer buffer) { 147 buffer.append(" count: "); 148 buffer.append(Long.toString(count)); 149 buffer.append(" maxTime: "); 150 buffer.append(Long.toString(maxTime)); 151 buffer.append(" minTime: "); 152 buffer.append(Long.toString(minTime)); 153 buffer.append(" totalTime: "); 154 buffer.append(Long.toString(totalTime)); 155 buffer.append(" averageTime: "); 156 buffer.append(Double.toString(getAverageTime())); 157 buffer.append(" averageTimeExMinMax: "); 158 buffer.append(Double.toString(getAverageTimeExcludingMinMax())); 159 buffer.append(" averagePerSecond: "); 160 buffer.append(Double.toString(getAveragePerSecond())); 161 buffer.append(" averagePerSecondExMinMax: "); 162 buffer.append(Double.toString(getAveragePerSecondExcludingMinMax())); 163 super.appendFieldDescription(buffer); 164 } 165 166 }