001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.base;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020import static com.google.common.base.Preconditions.checkState;
021import static java.util.concurrent.TimeUnit.DAYS;
022import static java.util.concurrent.TimeUnit.HOURS;
023import static java.util.concurrent.TimeUnit.MICROSECONDS;
024import static java.util.concurrent.TimeUnit.MILLISECONDS;
025import static java.util.concurrent.TimeUnit.MINUTES;
026import static java.util.concurrent.TimeUnit.NANOSECONDS;
027import static java.util.concurrent.TimeUnit.SECONDS;
028
029import com.google.common.annotations.Beta;
030import com.google.common.annotations.GwtCompatible;
031import com.google.common.annotations.GwtIncompatible;
032
033import java.util.concurrent.TimeUnit;
034
035/**
036 * An object that measures elapsed time in nanoseconds. It is useful to measure
037 * elapsed time using this class instead of direct calls to {@link
038 * System#nanoTime} for a few reasons:
039 *
040 * <ul>
041 * <li>An alternate time source can be substituted, for testing or performance
042 *     reasons.
043 * <li>As documented by {@code nanoTime}, the value returned has no absolute
044 *     meaning, and can only be interpreted as relative to another timestamp
045 *     returned by {@code nanoTime} at a different time. {@code Stopwatch} is a
046 *     more effective abstraction because it exposes only these relative values,
047 *     not the absolute ones.
048 * </ul>
049 *
050 * <p>Basic usage:
051 * <pre>
052 *   Stopwatch stopwatch = Stopwatch.{@link #createStarted createStarted}();
053 *   doSomething();
054 *   stopwatch.{@link #stop stop}(); // optional
055 *
056 *   long millis = stopwatch.elapsed(MILLISECONDS);
057 *
058 *   log.info("time: " + stopwatch); // formatted string like "12.3 ms"</pre>
059 *
060 * <p>Stopwatch methods are not idempotent; it is an error to start or stop a
061 * stopwatch that is already in the desired state.
062 *
063 * <p>When testing code that uses this class, use
064 * {@link #createUnstarted(Ticker)} or {@link #createStarted(Ticker)} to
065 * supply a fake or mock ticker.
066 * <!-- TODO(kevinb): restore the "such as" --> This allows you to
067 * simulate any valid behavior of the stopwatch.
068 *
069 * <p><b>Note:</b> This class is not thread-safe.
070 *
071 * @author Kevin Bourrillion
072 * @since 10.0
073 */
074@Beta
075@GwtCompatible(emulated = true)
076public final class Stopwatch {
077  private final Ticker ticker;
078  private boolean isRunning;
079  private long elapsedNanos;
080  private long startTick;
081
082  /**
083   * Creates (but does not start) a new stopwatch using {@link System#nanoTime}
084   * as its time source.
085   *
086   * @since 15.0
087   */
088  public static Stopwatch createUnstarted() {
089    return new Stopwatch();
090  }
091
092  /**
093   * Creates (but does not start) a new stopwatch, using the specified time
094   * source.
095   *
096   * @since 15.0
097   */
098  public static Stopwatch createUnstarted(Ticker ticker) {
099    return new Stopwatch(ticker);
100  }
101
102  /**
103   * Creates (and starts) a new stopwatch using {@link System#nanoTime}
104   * as its time source.
105   *
106   * @since 15.0
107   */
108  public static Stopwatch createStarted() {
109    return new Stopwatch().start();
110  }
111
112  /**
113   * Creates (and starts) a new stopwatch, using the specified time
114   * source.
115   *
116   * @since 15.0
117   */
118  public static Stopwatch createStarted(Ticker ticker) {
119    return new Stopwatch(ticker).start();
120  }
121
122  /**
123   * Creates (but does not start) a new stopwatch using {@link System#nanoTime}
124   * as its time source.
125   *
126   * @deprecated Use {@link Stopwatch#createUnstarted()} instead.
127   */
128  @Deprecated
129  Stopwatch() {
130    this(Ticker.systemTicker());
131  }
132
133  /**
134   * Creates (but does not start) a new stopwatch, using the specified time
135   * source.
136   *
137   * @deprecated Use {@link Stopwatch#createUnstarted(Ticker)} instead.
138   */
139  @Deprecated
140  Stopwatch(Ticker ticker) {
141    this.ticker = checkNotNull(ticker, "ticker");
142  }
143
144  /**
145   * Returns {@code true} if {@link #start()} has been called on this stopwatch,
146   * and {@link #stop()} has not been called since the last call to {@code
147   * start()}.
148   */
149  public boolean isRunning() {
150    return isRunning;
151  }
152
153  /**
154   * Starts the stopwatch.
155   *
156   * @return this {@code Stopwatch} instance
157   * @throws IllegalStateException if the stopwatch is already running.
158   */
159  public Stopwatch start() {
160    checkState(!isRunning, "This stopwatch is already running.");
161    isRunning = true;
162    startTick = ticker.read();
163    return this;
164  }
165
166  /**
167   * Stops the stopwatch. Future reads will return the fixed duration that had
168   * elapsed up to this point.
169   *
170   * @return this {@code Stopwatch} instance
171   * @throws IllegalStateException if the stopwatch is already stopped.
172   */
173  public Stopwatch stop() {
174    long tick = ticker.read();
175    checkState(isRunning, "This stopwatch is already stopped.");
176    isRunning = false;
177    elapsedNanos += tick - startTick;
178    return this;
179  }
180
181  /**
182   * Sets the elapsed time for this stopwatch to zero,
183   * and places it in a stopped state.
184   *
185   * @return this {@code Stopwatch} instance
186   */
187  public Stopwatch reset() {
188    elapsedNanos = 0;
189    isRunning = false;
190    return this;
191  }
192
193  private long elapsedNanos() {
194    return isRunning ? ticker.read() - startTick + elapsedNanos : elapsedNanos;
195  }
196
197  /**
198   * Returns the current elapsed time shown on this stopwatch, expressed
199   * in the desired time unit, with any fraction rounded down.
200   *
201   * <p>Note that the overhead of measurement can be more than a microsecond, so
202   * it is generally not useful to specify {@link TimeUnit#NANOSECONDS}
203   * precision here.
204   *
205   * @since 14.0 (since 10.0 as {@code elapsedTime()})
206   */
207  public long elapsed(TimeUnit desiredUnit) {
208    return desiredUnit.convert(elapsedNanos(), NANOSECONDS);
209  }
210
211  /**
212   * Returns a string representation of the current elapsed time.
213   */
214  @GwtIncompatible("String.format()")
215  @Override public String toString() {
216    long nanos = elapsedNanos();
217
218    TimeUnit unit = chooseUnit(nanos);
219    double value = (double) nanos / NANOSECONDS.convert(1, unit);
220
221    // Too bad this functionality is not exposed as a regular method call
222    return String.format("%.4g %s", value, abbreviate(unit));
223  }
224
225  private static TimeUnit chooseUnit(long nanos) {
226    if (DAYS.convert(nanos, NANOSECONDS) > 0) {
227      return DAYS;
228    }
229    if (HOURS.convert(nanos, NANOSECONDS) > 0) {
230      return HOURS;
231    }
232    if (MINUTES.convert(nanos, NANOSECONDS) > 0) {
233      return MINUTES;
234    }
235    if (SECONDS.convert(nanos, NANOSECONDS) > 0) {
236      return SECONDS;
237    }
238    if (MILLISECONDS.convert(nanos, NANOSECONDS) > 0) {
239      return MILLISECONDS;
240    }
241    if (MICROSECONDS.convert(nanos, NANOSECONDS) > 0) {
242      return MICROSECONDS;
243    }
244    return NANOSECONDS;
245  }
246
247  private static String abbreviate(TimeUnit unit) {
248    switch (unit) {
249      case NANOSECONDS:
250        return "ns";
251      case MICROSECONDS:
252        return "\u03bcs"; // μs
253      case MILLISECONDS:
254        return "ms";
255      case SECONDS:
256        return "s";
257      case MINUTES:
258        return "min";
259      case HOURS:
260        return "h";
261      case DAYS:
262        return "d";
263      default:
264        throw new AssertionError();
265    }
266  }
267}