001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.management; 018 019 020/** 021 * A time statistic implementation 022 * 023 * 024 */ 025public class TimeStatisticImpl extends StatisticImpl { 026 private long count; 027 private long maxTime; 028 private long minTime; 029 private long totalTime; 030 private TimeStatisticImpl parent; 031 032 public TimeStatisticImpl(String name, String description) { 033 this(name, "millis", description); 034 } 035 036 public TimeStatisticImpl(TimeStatisticImpl parent, String name, String description) { 037 this(name, description); 038 this.parent = parent; 039 } 040 041 public TimeStatisticImpl(String name, String unit, String description) { 042 super(name, unit, description); 043 } 044 045 public synchronized void reset() { 046 if(isDoReset()) { 047 super.reset(); 048 count = 0; 049 maxTime = 0; 050 minTime = 0; 051 totalTime = 0; 052 } 053 } 054 055 public synchronized long getCount() { 056 return count; 057 } 058 059 public synchronized void addTime(long time) { 060 count++; 061 totalTime += time; 062 if (time > maxTime) { 063 maxTime = time; 064 } 065 if (time < minTime || minTime == 0) { 066 minTime = time; 067 } 068 updateSampleTime(); 069 if (parent != null) { 070 parent.addTime(time); 071 } 072 } 073 074 /** 075 * @return the maximum time of any step 076 */ 077 public long getMaxTime() { 078 return maxTime; 079 } 080 081 /** 082 * @return the minimum time of any step 083 */ 084 public synchronized long getMinTime() { 085 return minTime; 086 } 087 088 /** 089 * @return the total time of all the steps added together 090 */ 091 public synchronized long getTotalTime() { 092 return totalTime; 093 } 094 095 /** 096 * @return the average time calculated by dividing the 097 * total time by the number of counts 098 */ 099 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}