001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.utils;
020
021/**
022 * A simple utility class to measure the elapsed time between two points in the code.
023 * Typicaly it is used as follows:
024 * <pre>TimeMarker marker = new TimeMarker().start();
025 * // ... execute some code ...
026 * long elapsed = marker.stop().getLastElapsed();
027 * System.out.println("elapsed time: " + marker.getLastElapsedAsString());</pre>
028 * @author Laurent Cohen
029 */
030public class TimeMarker {
031  /**
032   * Holds the start of a new time measurement.
033   */
034  private long start;
035  /**
036   * Holds the elapsed time for the last measurement.
037   */
038  private long lastElapsed;
039  /**
040   * Holds the sum of all performed measurements.
041   */
042  private long totalElapsed;
043  /**
044   * Holds the count of performed measurements.
045   */
046  private int count;
047
048  /**
049   * Start a new measurement.
050   * @return this {@code TimeMarker}.
051   */
052  public TimeMarker start() {
053    start = System.nanoTime();
054    return this;
055  }
056
057  /**
058   * Stop the current measurement.
059   * @return this {@code TimeMarker}.
060   */
061  public TimeMarker stop() {
062    lastElapsed = System.nanoTime() - start;
063    totalElapsed += lastElapsed;
064    count++;
065    return this;
066  }
067
068  /**
069   * Get the elapsed time for the last measurement.
070   * @return the elapsed time as a {@code long} value.
071   */
072  public long getLastElapsed() {
073    return lastElapsed;
074  }
075
076  /**
077   * Get the total elapsed time for all measurements.
078   * @return the elapsed time as a {@code long} value.
079   */
080  public long getTotalElapsed() {
081    return totalElapsed;
082  }
083
084  /**
085   * Get the elapsed time for the last measurement formatted as hh:mm:ss.nnn
086   * @return a string representation of the last elapsed time.
087   */
088  public String getLastElapsedAsString() {
089    return StringUtils.toStringDuration(lastElapsed / 1_000_000L);
090  }
091
092  /**
093   * Get the total elapsed time formatted as hh:mm:ss.nnn
094   * @return a string representation of the total elapsed time.
095   */
096  public String getTotalElapsedAsString() {
097    return StringUtils.toStringDuration(totalElapsed / 1_000_000L);
098  }
099
100  /**
101   * Get the count of measurements.
102   * @return the count as an {@code int} value.
103   */
104  public int getCount() {
105    return count;
106  }
107}