Class StopWatch

java.lang.Object
ca.uhn.fhir.util.StopWatch

public class StopWatch extends Object
A multipurpose stopwatch which can be used to time tasks and produce human readable output about task duration, throughput, estimated task completion, etc.

Thread Safety Note: StopWatch is not intended to be thread safe.

Since:
HAPI FHIR 3.3.0
  • Constructor Details

    • StopWatch

      public StopWatch()
      Constructor
    • StopWatch

      public StopWatch(Date theStart)
      Constructor
      Parameters:
      theStart - The time to record as the start for this timer
    • StopWatch

      public StopWatch(long theStart)
      Constructor
      Parameters:
      theStart - The time that the stopwatch was started
  • Method Details

    • endCurrentTask

      public void endCurrentTask()
      Finish the counter on the current task (which was started by calling startTask(String). This method has no effect if no task is currently started so it's ok to call it more than once.
    • formatMillisPerOperation

      public String formatMillisPerOperation(long theNumOperations)
      Returns a nice human-readable display of the time taken per operation. Note that this may not actually output the number of milliseconds if the time taken per operation was very long (over 10 seconds)
      See Also:
    • formatTaskDurations

      Returns a string providing the durations of all tasks collected by startTask(String)
    • formatThroughput

      public String formatThroughput(long theNumOperations, TimeUnit theUnit)
      Determine the current throughput per unit of time (specified in theUnit) assuming that theNumOperations operations have happened.

      For example, if this stopwatch has 2 seconds elapsed, and this method is called for theNumOperations=30 and TimeUnit=SECONDS, this method will return 15

      See Also:
    • getEstimatedTimeRemaining

      public String getEstimatedTimeRemaining(double theCompleteToDate, double theTotal)
      Given an amount of something completed so far, and a total amount, calculates how long it will take for something to complete
      Parameters:
      theCompleteToDate - The amount so far
      theTotal - The total (must be higher than theCompleteToDate
      Returns:
      A formatted amount of time
    • formatEstimatedTimeRemaining

      public static String formatEstimatedTimeRemaining(double theCompleteToDate, double theTotal, double millis)
      Given an amount of something completed so far, and a total amount, calculates how long it will take for something to complete
      Parameters:
      theCompleteToDate - The amount so far
      theTotal - The total (must be higher than theCompleteToDate
      Returns:
      A formatted amount of time
    • getMillis

      public long getMillis(Date theNow)
    • getMillis

      public long getMillis()
    • getMillisAndRestart

      public long getMillisAndRestart()
    • getMillisPerOperation

      public long getMillisPerOperation(long theNumOperations)
      Parameters:
      theNumOperations - Ok for this to be 0, it will be treated as 1
    • getStartedDate

      public Date getStartedDate()
    • getThroughput

      public double getThroughput(long theNumOperations, TimeUnit theUnit)
      Determine the current throughput per unit of time (specified in theUnit) assuming that theNumOperations operations have happened.

      For example, if this stopwatch has 2 seconds elapsed, and this method is called for theNumOperations=30 and TimeUnit=SECONDS, this method will return 15

      See Also:
    • restart

      public void restart()
    • startTask

      public void startTask(String theTaskName)
      Starts a counter for a sub-task

      Thread Safety Note: This method is not threadsafe! Do not use subtasks in a multithreaded environment.

      Parameters:
      theTaskName - Note that if theTaskName is blank or empty, no task is started
    • toString

      public String toString()
      Formats value in an appropriate format. See formatMillis(long)} for a description of the format
      Overrides:
      toString in class Object
      See Also:
    • formatThroughput

      public static String formatThroughput(double throughput)
      Format a throughput number (output does not include units)
    • getThroughput

      public static double getThroughput(long theNumOperations, long theMillisElapsed, TimeUnit theUnit)
      Calculate throughput
      Parameters:
      theNumOperations - The number of operations completed
      theMillisElapsed - The time elapsed
      theUnit - The unit for the throughput
    • formatMillis

      public static String formatMillis(long theMillis)
      Formats a number of milliseconds for display (e.g. in a log file), tailoring the output to how big the value actually is.

      Example outputs:

      • 133ms
      • 00:00:10.223
      • 1.7 days
      • 64 days
    • formatMillis

      public static String formatMillis(double theMillis)
      Formats a number of milliseconds for display (e.g. in a log file), tailoring the output to how big the value actually is.

      Example outputs:

      • 133ms
      • 00:00:10.223
      • 1.7 days
      • 64 days
    • setNowForUnitTest

      public static void setNowForUnitTest(Long theNowForUnitTest)