001    /**
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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     * 
017     **/
018    package org.activemq.management;
019    
020    
021    /**
022     * A time statistic implementation
023     *
024     * @version $Revision: 1.1.1.1 $
025     */
026    public class TimeStatisticImpl extends StatisticImpl {
027        private long count;
028        private long maxTime;
029        private long minTime;
030        private long totalTime;
031        private TimeStatisticImpl parent;
032    
033        public TimeStatisticImpl(String name, String description) {
034            this(name, "millis", description);
035        }
036    
037        public TimeStatisticImpl(TimeStatisticImpl parent, String name, String description) {
038            this(name, description);
039            this.parent = parent;
040        }
041    
042        public TimeStatisticImpl(String name, String unit, String description) {
043            super(name, unit, description);
044        }
045    
046        public synchronized void reset() {
047            super.reset();
048            count = 0;
049            maxTime = 0;
050            minTime = 0;
051            totalTime = 0;
052        }
053    
054        public synchronized long getCount() {
055            return count;
056        }
057    
058        public synchronized void addTime(long time) {
059            count++;
060            totalTime += time;
061            if (time > maxTime) {
062                maxTime = time;
063            }
064            if (time < minTime || minTime == 0) {
065                minTime = time;
066            }
067            updateSampleTime();
068            if (parent != null) {
069                parent.addTime(time);
070            }
071        }
072    
073        /**
074         * @return the maximum time of any step
075         */
076        public long getMaxTime() {
077            return maxTime;
078        }
079    
080        /**
081         * @return the minimum time of any step
082         */
083        public synchronized long getMinTime() {
084            return minTime;
085        }
086    
087        /**
088         * @return the total time of all the steps added together
089         */
090        public synchronized long getTotalTime() {
091            return totalTime;
092        }
093    
094        /**
095         * @return the average time calculated by dividing the
096         *         total time by the number of counts
097         */
098        public synchronized double getAverageTime() {
099            if (count == 0) {
100                return 0;
101            }
102            double d = totalTime;
103            return d / count;
104        }
105    
106    
107        /**
108         * @return the average time calculated by dividing the
109         *         total time by the number of counts but excluding the
110         *         minimum and maximum times.
111         */
112        public synchronized double getAverageTimeExcludingMinMax() {
113            if (count <= 2) {
114                return 0;
115            }
116            double d = totalTime - minTime - maxTime;
117            return d / (count - 2);
118        }
119    
120    
121        /**
122         * @return the average number of steps per second
123         */
124        public double getAveragePerSecond() {
125            double d = 1000;
126            double averageTime = getAverageTime();
127            if (averageTime == 0) {
128                return 0;
129            }
130            return d / averageTime;
131        }
132    
133        /**
134         * @return the average number of steps per second excluding the min & max values
135         */
136        public double getAveragePerSecondExcludingMinMax() {
137            double d = 1000;
138            double average = getAverageTimeExcludingMinMax();
139            if (average == 0) {
140                return 0;
141            }
142            return d / average;
143        }
144    
145        public TimeStatisticImpl getParent() {
146            return parent;
147        }
148    
149        public void setParent(TimeStatisticImpl parent) {
150            this.parent = parent;
151        }
152    
153        protected synchronized void appendFieldDescription(StringBuffer buffer) {
154            buffer.append(" count: ");
155            buffer.append(Long.toString(count));
156            buffer.append(" maxTime: ");
157            buffer.append(Long.toString(maxTime));
158            buffer.append(" minTime: ");
159            buffer.append(Long.toString(minTime));
160            buffer.append(" totalTime: ");
161            buffer.append(Long.toString(totalTime));
162            buffer.append(" averageTime: ");
163            buffer.append(Double.toString(getAverageTime()));
164            buffer.append(" averageTimeExMinMax: ");
165            buffer.append(Double.toString(getAverageTimeExcludingMinMax()));
166            buffer.append(" averagePerSecond: ");
167            buffer.append(Double.toString(getAveragePerSecond()));
168            buffer.append(" averagePerSecondExMinMax: ");
169            buffer.append(Double.toString(getAveragePerSecondExcludingMinMax()));
170            super.appendFieldDescription(buffer);
171        }
172    
173    }