001package org.javasimon.jmx;
002
003import org.javasimon.utils.SimonUtils;
004
005import java.beans.ConstructorProperties;
006import java.util.Date;
007
008/**
009 * Value object for retrieving data from Stopwatch Simon. Basically, it's
010 * {@link org.javasimon.StopwatchSample} with added JMX capabilities to be return as object via
011 * MXBean method.
012 * <p/>
013 * Example:
014 * <pre>
015 * SimonManagerMXBean simon = JMX.newMXBeanProxy(..., new ObjectName("domain:type=Simon"), SimonManagerMXBean.class);
016 * StopwatchSample = simon.getStopwatchSample("simon.stopwatch");
017 * </pre>
018 *
019 * @author Radovan Sninsky
020 * @since 2.0
021 */
022public final class StopwatchSample extends org.javasimon.StopwatchSample {
023
024        /**
025         * JMX constructor. Constructor used by JMX client code to initialize all properties of object
026         * from composite data object.
027         *
028         * @param name Simon's name
029         * @param mean mean value (provided optionally)
030         * @param stdDev standard deviation (provided optionally)
031         * @param var variance (provided optionally)
032         * @param varN variance N (provided optionally)
033         * @param firstUsage first usage ms timestamp
034         * @param lastUsage last usage ms timestamp
035         * @param total sum of all measured times
036         * @param note note (provided optionally)
037         * @param counter count of measures
038         * @param min minimal measured time
039         * @param max maximal measured time
040         * @param minTimestamp ms timestamp when minimal time was measured
041         * @param maxTimestamp ms timestamp when maximal time was measured
042         * @param active count of actual running measures
043         * @param maxActive maximum parallel measures
044         * @param maxActiveTimestamp ms timestamp time when maximum parallel measures happened
045         * @param last last split value in ns
046         */
047        @ConstructorProperties({"name", "mean", "standardDeviation", "variance", "varianceN", "note", "firstUsage", "lastUsage",
048                "total", "counter", "min", "max", "minTimestamp", "maxTimestamp", "active", "maxActive",
049                "maxActiveTimestamp", "last"})
050        public StopwatchSample(String name, double mean, double stdDev, double var, double varN, String note, long firstUsage,
051                long lastUsage, long total, long counter, long min, long max, long minTimestamp,
052                long maxTimestamp, long active, long maxActive, long maxActiveTimestamp, long last)
053        {
054                setName(name);
055                setMean(mean);
056                setStandardDeviation(stdDev);
057                setVariance(var);
058                setVarianceN(varN);
059                setNote(note);
060                setFirstUsage(firstUsage);
061                setLastUsage(lastUsage);
062
063                setTotal(total);
064                setCounter(counter);
065                setMin(min);
066                setMax(max);
067                setMinTimestamp(minTimestamp);
068                setMaxTimestamp(maxTimestamp);
069                setActive(active);
070                setMaxActive(maxActive);
071                setMaxActiveTimestamp(maxActiveTimestamp);
072                setLast(last);
073        }
074
075        /**
076         * Framework constructor for Simon MBean implementation to initialize all properties
077         * by sample obtained from Simon.
078         *
079         * @param sample sample object obtained from Stopwatch Simon
080         */
081        public StopwatchSample(org.javasimon.StopwatchSample sample) {
082                setName(sample.getName());
083                setMean(sample.getMean());
084                setStandardDeviation(sample.getStandardDeviation());
085                setVariance(sample.getVariance());
086                setVarianceN(sample.getVarianceN());
087                setNote(sample.getNote());
088                setFirstUsage(sample.getFirstUsage());
089                setLastUsage(sample.getLastUsage());
090
091                setCounter(sample.getCounter());
092                setTotal(sample.getTotal());
093                setMin(sample.getMin());
094                setMax(sample.getMax());
095                setMinTimestamp(sample.getMinTimestamp());
096                setMaxTimestamp(sample.getMaxTimestamp());
097                setActive(sample.getActive());
098                setMaxActive(sample.getMaxActive());
099                setMaxActiveTimestamp(sample.getMaxActiveTimestamp());
100                setLast(sample.getLast());
101        }
102
103        /**
104         * Returns the value of the last measured split in ns as a formatted string.
105         *
106         * @return last measured split in ns as string
107         */
108        public final String getLastAsString() {
109                return SimonUtils.presentNanoTime(getLast());
110        }
111
112        /**
113         * Returns the total sum of all split times in nanoseconds as a formatted string.
114         *
115         * @return total time of the stopwatch in nanoseconds as string
116         */
117        public final String getTotalAsString() {
118                return SimonUtils.presentNanoTime(getTotal());
119        }
120
121        /**
122         * Returns minimal time split value in nanoseconds as a formatted string.
123         *
124         * @return minimal time split in nanoseconds as string
125         */
126        public final String getMinAsString() {
127                return SimonUtils.presentNanoTime(getMin());
128        }
129
130        /**
131         * Returns maximal time split value in nanoseconds as a formatted string.
132         *
133         * @return maximal time split in nanoseconds as string
134         */
135        public final String getMaxAsString() {
136                return SimonUtils.presentNanoTime(getMax());
137        }
138
139        /**
140         * Returns ms timestamp when the min value was measured as a formatted string.
141         *
142         * @return ms timestamp of the min value measurement as string
143         */
144        public final String getMinTimestampAsString() {
145                return SimonUtils.presentTimestamp(getMinTimestamp());
146        }
147
148        /**
149         * Returns ms timestamp when the min value was measured as a formatted date.
150         *
151         * @return ms timestamp of the min value measurement as date
152         */
153        public final Date getMinTimestampAsDate() {
154                return new Date(getMinTimestamp());
155        }
156
157        /**
158         * Returns ms timestamp when the max value was measured as a formatted string.
159         *
160         * @return ms timestamp of the max value measurement as string
161         */
162        public final String getMaxTimestampAsString() {
163                return SimonUtils.presentTimestamp(getMaxTimestamp());
164        }
165
166        /**
167         * Returns ms timestamp when the max value was measured as a formatted date.
168         *
169         * @return ms timestamp of the max value measurement as date
170         */
171        public final Date getMaxTimestampAsDate() {
172                return new Date(getMaxTimestamp());
173        }
174
175        /**
176         * Returns ms timestamp when the last peek of the active split count occurred as a formatted string.
177         *
178         * @return ms timestamp of the last peek of the active split count as string
179         */
180        public final String getMaxActiveTimestampAsString() {
181                return SimonUtils.presentTimestamp(getMaxActiveTimestamp());
182        }
183
184        /**
185         * Returns ms timestamp when the last peek of the active split count occurred as a formatted date.
186         *
187         * @return ms timestamp of the last peek of the active split count as date
188         */
189        public final Date getMaxActiveTimestampAsDate() {
190                return new Date(getMaxActiveTimestamp());
191        }
192
193        /**
194         * Returns mean value (average) of all measured values as a formatted string (ns).
195         *
196         * @return mean value as string
197         */
198        public final String getMeanAsString() {
199                return SimonUtils.presentNanoTime((long) getMean());
200        }
201
202        /**
203         * Timestamp of the first usage from the sampled Simon as a formatted string.
204         *
205         * @return Simon's first usage timestamp as string
206         */
207        public String getFirstUsageAsString() {
208                return SimonUtils.presentTimestamp(getFirstUsage());
209        }
210
211        /**
212         * Timestamp of the first usage from the sampled Simon as a formatted date.
213         *
214         * @return Simon's first usage timestamp as date
215         */
216        public Date getFirstUsageAsDate() {
217                return new Date(getFirstUsage());
218        }
219
220        /**
221         * Timestamp of the last usage from the sampled Simon as a formatted string.
222         *
223         * @return Simon's last usage timestamp as string
224         */
225        public String getLastUsageAsString() {
226                return SimonUtils.presentTimestamp(getLastUsage());
227        }
228
229        /**
230         * Timestamp of the last usage from the sampled Simon as a formatted date.
231         *
232         * @return Simon's last usage timestamp as string
233         */
234        public Date getLastUsageAsDate() {
235                return new Date(getLastUsage());
236        }
237}