001package org.javasimon.utils;
002
003import org.javasimon.Simon;
004import org.javasimon.SimonFilter;
005import org.javasimon.StopwatchSample;
006
007/**
008 * Holds aggregate values for stopwatches in a hierarchy as a result of
009 * {@link SimonUtils#calculateStopwatchAggregate(Simon)} (optionally with {@link SimonFilter}).
010 *
011 * @author <a href="mailto:ivan.mushketyk@gmail.com">Ivan Mushketyk</a>
012 * @since 3.5
013 */
014public class StopwatchAggregate {
015
016        private long total;
017        private long counter;
018        private long min = Long.MAX_VALUE;
019        private long max;
020        private long minTimestamp;
021        private long maxTimestamp;
022        private long active;
023        private long maxActive;
024        private long maxActiveTimestamp;
025
026        StopwatchAggregate() {
027        }
028
029        /**
030         * Returns total sum of all splits of all stopwatches in hierarchy in milliseconds.
031         *
032         * @return total time of all stopwatches in nanoseconds
033         */
034        public long getTotal() {
035                return total;
036        }
037
038        /**
039         * Returns total sum of usage counts of all stopwatches in hierarchy in milliseconds.
040         *
041         * @return total sum of usage counts of all stopwatches
042         */
043        public long getCounter() {
044                return counter;
045        }
046
047        /**
048         * Returns min time split among all stopwatches in hierarchy in milliseconds.
049         *
050         * @return min time split among all stopwatches
051         */
052        public long getMin() {
053                return min;
054        }
055
056        /**
057         * Returns max time split among all stopwatches in hierarchy in milliseconds
058         *
059         * @return max time split among all stopwatches
060         */
061        public long getMax() {
062                return max;
063        }
064
065        /**
066         * Returns timestamp in milliseconds when min value was measured among all stopwatches.
067         *
068         * @return timestamp with min value was measured in milliseconds
069         */
070        public long getMinTimestamp() {
071                return minTimestamp;
072        }
073
074        /**
075         * Returns timestamp in milliseconds when max value was measured among all stopwatches.
076         *
077         * @return timestamp with max value was measured in milliseconds
078         */
079        public long getMaxTimestamp() {
080                return maxTimestamp;
081        }
082
083        /**
084         * Returns total number of active splits in all stopwatches in hierarchy.
085         *
086         * @return total number of active splits
087         */
088        public long getActive() {
089                return active;
090        }
091
092        /**
093         * Returns peek value of active concurrent splits among all stopwatches in hierarchy.
094         *
095         * @return peek value of active concurrent splits.
096         */
097        public long getMaxActive() {
098                return maxActive;
099        }
100
101        /**
102         * Returns timestamp in millisecond when the last peek of active split counter occurred among all stopwatches in hierarchy.
103         *
104         * @return timestamp im milliseconds of the last peek of the active split counter
105         */
106        public long getMaxActiveTimestamp() {
107                return maxActiveTimestamp;
108        }
109
110        /**
111         * Add stopwatch sample to current statistics aggregate.
112         *
113         * @param sample - stopwatch sample that will be added to statistics aggregate
114         */
115        void addSample(StopwatchSample sample) {
116                total += sample.getTotal();
117                counter += sample.getCounter();
118
119                if (min > sample.getMin()) {
120                        min = sample.getMin();
121                        minTimestamp = sample.getMinTimestamp();
122                }
123
124                if (max < sample.getMax()) {
125                        max = sample.getMax();
126                        maxTimestamp = sample.getMaxTimestamp();
127                }
128
129                active += sample.getActive();
130
131                if (maxActive < sample.getMaxActive()) {
132                        maxActive = sample.getMaxActive();
133                        maxActiveTimestamp = sample.getMaxActiveTimestamp();
134                }
135        }
136
137        @Override
138        public boolean equals(Object o) {
139                if (this == o) return true;
140                if (o == null || getClass() != o.getClass()) return false;
141
142                StopwatchAggregate aggregate = (StopwatchAggregate) o;
143
144                if (active != aggregate.active) return false;
145                if (counter != aggregate.counter) return false;
146                if (max != aggregate.max) return false;
147                if (maxActive != aggregate.maxActive) return false;
148                if (maxActiveTimestamp != aggregate.maxActiveTimestamp) return false;
149                if (maxTimestamp != aggregate.maxTimestamp) return false;
150                if (min != aggregate.min) return false;
151                if (minTimestamp != aggregate.minTimestamp) return false;
152                if (total != aggregate.total) return false;
153
154                return true;
155        }
156
157        @Override
158        public int hashCode() {
159                int result = (int) (total ^ (total >>> 32));
160                result = 31 * result + (int) (counter ^ (counter >>> 32));
161                result = 31 * result + (int) (min ^ (min >>> 32));
162                result = 31 * result + (int) (max ^ (max >>> 32));
163                result = 31 * result + (int) (minTimestamp ^ (minTimestamp >>> 32));
164                result = 31 * result + (int) (maxTimestamp ^ (maxTimestamp >>> 32));
165                result = 31 * result + (int) (active ^ (active >>> 32));
166                result = 31 * result + (int) (maxActive ^ (maxActive >>> 32));
167                result = 31 * result + (int) (maxActiveTimestamp ^ (maxActiveTimestamp >>> 32));
168                return result;
169        }
170
171        @Override
172        public String toString() {
173                return "StopwatchAggregate{total= " + SimonUtils.presentNanoTime(total) +
174                        ", counter=" + counter +
175                        ", min=" + SimonUtils.presentNanoTime(min) +
176                        ", max=" + SimonUtils.presentNanoTime(max) +
177                        ", minTimestamp=" + SimonUtils.presentTimestamp(minTimestamp) +
178                        ", maxTimestamp=" + SimonUtils.presentTimestamp(maxTimestamp) +
179                        ", active=" + active +
180                        ", maxActive=" + maxActive +
181                        ", maxActiveTimestamp=" + SimonUtils.presentTimestamp(maxActiveTimestamp) + '}';
182        }
183}