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