2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.util;
10 import static java.util.concurrent.TimeUnit.MICROSECONDS;
11 import static java.util.concurrent.TimeUnit.MILLISECONDS;
12 import static java.util.concurrent.TimeUnit.NANOSECONDS;
13 import static java.util.concurrent.TimeUnit.SECONDS;
15 import com.google.common.annotations.Beta;
16 import java.text.DecimalFormat;
17 import java.text.DecimalFormatSymbols;
18 import java.util.Date;
19 import java.util.concurrent.TimeUnit;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Abstract class that calculates and tracks time duration statistics.
26 * @author Thomas Pantelis
27 * @author Robert Varga
30 public abstract class DurationStatisticsTracker {
31 private static final Logger LOG = LoggerFactory.getLogger(DurationStatisticsTracker.class);
32 private static final DecimalFormat DECIMAL_FORMAT;
35 final DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
36 symbols.setDecimalSeparator('.');
37 DECIMAL_FORMAT = new DecimalFormat("0.00", symbols);
41 * Create a concurrent {@link DurationStatisticsTracker}, which performs well
42 * in very contended environments.
44 * @return A new instance.
46 public static DurationStatisticsTracker createConcurrent() {
47 return new ConcurrentDurationStatisticsTracker();
51 * Create a synchronized {@link DurationStatisticsTracker}, which performs well
52 * in non-contended environments.
54 * @return A new instance.
56 public static DurationStatisticsTracker createSynchronized() {
57 return new SynchronizedDurationStatsTracker();
61 * Add a duration to track.
64 * non-negative duration in nanoseconds.
66 public abstract void addDuration(long duration);
69 * Returns the average duration in nanoseconds.
71 public abstract double getAverageDuration();
74 * Returns the total number of tracked durations.
76 * @return Total number of measurements accumulated since last
79 public abstract long getTotalDurations();
82 * Resets all statistics back to their defaults.
84 public abstract void reset();
87 * Get the shortest recorded duration and the time when it was recorded.
89 * @return Duration and timestamp.
91 protected abstract DurationWithTime getShortest();
94 * Get the longest recorded duration and the time when it was recorded.
96 * @return Duration and timestamp.
98 protected abstract DurationWithTime getLongest();
101 * Returns the longest duration in nanoseconds.
103 public final long getLongestDuration() {
104 return getDuration(getLongest());
108 * Returns the shortest duration in nanoseconds.
110 public final long getShortestDuration() {
111 return getDuration(getShortest());
115 * Returns the average duration as a displayable String with units, e.g.
118 public final String getDisplayableAverageDuration() {
119 return formatDuration(getAverageDuration(), null);
123 * Returns the longest duration as a displayable String with units and the
124 * date/time at which it occurred, e.g. "12.34 ms at 08/02/2014 12:30:24".
126 public final String getDisplayableLongestDuration() {
127 return formatDuration(getLongest());
131 * Returns the shortest duration as a displayable String with units and the
132 * date/time at which it occurred, e.g. "12.34 ms at 08/02/2014 12:30:24".
134 public final String getDisplayableShortestDuration() {
135 return formatDuration(getShortest());
139 * Returns the time stamp of the longest duration.
141 public final long getTimeOfLongestDuration() {
142 return getTimeMillis(getLongest());
146 * Returns the time stamp of the shortest duration.
148 public final long getTimeOfShortestDuration() {
149 return getTimeMillis(getShortest());
153 * Returns formatted value of number, e.g. "12.34". Always is used dot as
156 private static synchronized String formatDecimalValue(final double value) {
157 return DECIMAL_FORMAT.format(value);
160 private static long getDuration(final DurationWithTime current) {
161 return current == null ? 0L : current.getDuration();
164 private static long getTimeMillis(final DurationWithTime current) {
165 return current == null ? 0L : current.getTimeMillis();
168 private static String formatDuration(final double duration, final Long timeStamp) {
169 final TimeUnit unit = chooseUnit((long) duration);
170 final double value = duration / NANOSECONDS.convert(1, unit);
172 final StringBuilder sb = new StringBuilder();
173 sb.append(formatDecimalValue(value));
175 sb.append(abbreviate(unit));
177 if (timeStamp != null) {
178 sb.append(String.format(" at %1$tD %1$tT", new Date(timeStamp)));
181 return sb.toString();
184 private static String formatDuration(final DurationWithTime current) {
185 if (current != null) {
186 return formatDuration(current.getDuration(), current.getTimeMillis());
188 return formatDuration(0, null);
192 private static TimeUnit chooseUnit(final long nanos) {
193 // TODO: this could be inlined, as we are doing needless divisions
194 if (NANOSECONDS.toSeconds(nanos) > 0) {
197 if (NANOSECONDS.toMillis(nanos) > 0) {
200 if (NANOSECONDS.toMicros(nanos) > 0) {
206 private static String abbreviate(final TimeUnit unit) {
211 return "\u03bcs"; // μs
223 LOG.warn("Unhandled time unit {}", unit);