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
021import java.util.concurrent.TimeUnit;
022
023/**
024 * A simple utility class to measure the elapsed time between two points in the code.
025 * Typicaly it is used as follows:
026 * <pre>TimeMarker marker = new TimeMarker().start();
027 * // ... execute some code ...
028 * long elapsed = marker.stop().getLastElapsed();
029 * System.out.println("elapsed time: " + marker.getLastElapsedAsString());</pre>
030 * @author Laurent Cohen
031 */
032public class TimeMarker {
033  /**
034   * Holds the start of a new time measurement.
035   */
036  private long start;
037  /**
038   * Holds the elapsed time for the last measurement.
039   */
040  private long lastElapsed;
041  /**
042   * Holds the sum of all performed measurements.
043   */
044  private long totalElapsed;
045  /**
046   * Holds the count of performed measurements.
047   */
048  private int count;
049
050  /**
051   * Start a new measurement.
052   * @return this {@code TimeMarker}.
053   */
054  public TimeMarker start() {
055    start = System.nanoTime();
056    return this;
057  }
058
059  /**
060   * Stop the current measurement.
061   * @return this {@code TimeMarker}.
062   */
063  public TimeMarker stop() {
064    return markTime();
065  }
066
067  /**
068   * Take a measurement and recompute the last and total elapsed times.
069   * @return this {@code TimeMarker}.
070   */
071  public TimeMarker markTime() {
072    lastElapsed = System.nanoTime() - start;
073    totalElapsed += lastElapsed;
074    count++;
075    return this;
076  }
077
078  /**
079   * Get the elapsed time for the last measurement.
080   * @return the elapsed time as a {@code long} value.
081   */
082  public long getLastElapsed() {
083    return lastElapsed;
084  }
085
086  /**
087   * Get the elapsed time for the last measurement in milliseconds.
088   * @return the elapsed time as a {@code long} value.
089   */
090  public long getLastElapsedMillis() {
091    return TimeUnit.NANOSECONDS.toMillis(lastElapsed);
092  }
093
094  /**
095   * Get the total elapsed time for all measurements in milliseconds.
096   * @return the elapsed time as a {@code long} value.
097   */
098  public long getTotalElapsed() {
099    return TimeUnit.NANOSECONDS.toMillis(totalElapsed);
100  }
101
102  /**
103   * Get the total elapsed time for all measurements.
104   * @return the elapsed time as a {@code long} value.
105   */
106  public long getTotalElapsedMillis() {
107    return totalElapsed;
108  }
109
110  /**
111   * Get the elapsed time for the last measurement formatted as hh:mm:ss.nnn
112   * @return a string representation of the last elapsed time.
113   */
114  public String getLastElapsedAsString() {
115    return StringUtils.toStringDuration(lastElapsed / 1_000_000L);
116  }
117
118  /**
119   * Get the total elapsed time formatted as hh:mm:ss.nnn
120   * @return a string representation of the total elapsed time.
121   */
122  public String getTotalElapsedAsString() {
123    return StringUtils.toStringDuration(totalElapsed / 1_000_000L);
124  }
125
126  /**
127   * Get the count of measurements.
128   * @return the count as an {@code int} value.
129   */
130  public int getCount() {
131    return count;
132  }
133}