org.springframework.util

## Class StopWatch

- java.lang.Object
- - org.springframework.util.StopWatch

-     public class StopWatch
      extends Object

  Simple stop watch, allowing for timing of a number of tasks, exposing total running time and running time for each named task.

  Conceals use of `System.nanoTime()`, improving the readability of application code and reducing the likelihood of calculation errors.

  Note that this object is not designed to be thread-safe and does not use synchronization.

  This class is normally used to verify performance during proof-of-concept work and in development, rather than as part of production applications.

  As of Spring Framework 5.2, running time is tracked and reported in nanoseconds.

  Since:
    May 2, 2001
  Author:
    Rod Johnson, Juergen Hoeller, Sam Brannen

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `StopWatch.TaskInfo` Nested class to hold data about one task executed within the `StopWatch`.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `StopWatch()` Construct a new `StopWatch`.
    `StopWatch(String id)` Construct a new `StopWatch` with the given ID.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `String` | `currentTaskName()` Get the name of the currently running task, if any.
    `String` | `getId()` Get the ID of this `StopWatch`, as specified on construction.
    `StopWatch.TaskInfo` | `getLastTaskInfo()` Get the last task as a `StopWatch.TaskInfo` object.
    `String` | `getLastTaskName()` Get the name of the last task.
    `long` | `getLastTaskTimeMillis()` Get the time taken by the last task in milliseconds.
    `long` | `getLastTaskTimeNanos()` Get the time taken by the last task in nanoseconds.
    `int` | `getTaskCount()` Get the number of tasks timed.
    `StopWatch.TaskInfo[]` | `getTaskInfo()` Get an array of the data for tasks performed.
    `long` | `getTotalTimeMillis()` Get the total time in milliseconds for all tasks.
    `long` | `getTotalTimeNanos()` Get the total time in nanoseconds for all tasks.
    `double` | `getTotalTimeSeconds()` Get the total time in seconds for all tasks.
    `boolean` | `isRunning()` Determine whether this `StopWatch` is currently running.
    `String` | `prettyPrint()` Generate a string with a table describing all tasks performed.
    `void` | `setKeepTaskList(boolean keepTaskList)` Configure whether the `StopWatch.TaskInfo` array is built over time.
    `String` | `shortSummary()` Get a short description of the total running time.
    `void` | `start()` Start an unnamed task.
    `void` | `start(String taskName)` Start a named task.
    `void` | `stop()` Stop the current task.
    `String` | `toString()` Generate an informative string describing all tasks performed

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### StopWatch

          public StopWatch()

      Construct a new `StopWatch`.

      Does not start any task.

    - #### StopWatch

          public StopWatch(String id)

      Construct a new `StopWatch` with the given ID.

      The ID is handy when we have output from multiple stop watches and need to distinguish between them.

      Does not start any task.

      Parameters:
        `id` - identifier for this stop watch

  - ### Method Detail

    - #### getId

          public String getId()

      Get the ID of this `StopWatch`, as specified on construction.
      Returns:
        the ID (empty String by default)
      Since:
        4.2.2
      See Also:
        `StopWatch(String)`

    - #### setKeepTaskList

          public void setKeepTaskList(boolean keepTaskList)

      Configure whether the `StopWatch.TaskInfo` array is built over time.

      Set this to `false` when using a `StopWatch` for millions of intervals; otherwise, the `TaskInfo` structure will consume excessive memory.

      Default is `true`.

    - #### start

          public void start()
                     throws IllegalStateException

      Start an unnamed task.

      The results are undefined if `stop()` or timing methods are called without invoking this method first.

      Throws:
        `IllegalStateException`
      See Also:
        `start(String)`, `stop()`

    - #### start

          public void start(String taskName)
                     throws IllegalStateException

      Start a named task.

      The results are undefined if `stop()` or timing methods are called without invoking this method first.

      Parameters:
        `taskName` - the name of the task to start
      Throws:
        `IllegalStateException`
      See Also:
        `start()`, `stop()`

    - #### stop

          public void stop()
                    throws IllegalStateException

      Stop the current task.

      The results are undefined if timing methods are called without invoking at least one pair of `start()` / `stop()` methods.

      Throws:
        `IllegalStateException`
      See Also:
        `start()`, `start(String)`

    - #### isRunning

          public boolean isRunning()

      Determine whether this `StopWatch` is currently running.
      See Also:
        `currentTaskName()`

    - #### currentTaskName

          @Nullable
          public String currentTaskName()

      Get the name of the currently running task, if any.
      Since:
        4.2.2
      See Also:
        `isRunning()`

    - #### getLastTaskTimeNanos

          public long getLastTaskTimeNanos()
                                    throws IllegalStateException

      Get the time taken by the last task in nanoseconds.
      Throws:
        `IllegalStateException`
      Since:
        5.2
      See Also:
        `getLastTaskTimeMillis()`

    - #### getLastTaskTimeMillis

          public long getLastTaskTimeMillis()
                                     throws IllegalStateException

      Get the time taken by the last task in milliseconds.
      Throws:
        `IllegalStateException`
      See Also:
        `getLastTaskTimeNanos()`

    - #### getLastTaskName

          public String getLastTaskName()
                                 throws IllegalStateException

      Get the name of the last task.
      Throws:
        `IllegalStateException`

    - #### getLastTaskInfo

          public StopWatch.TaskInfo getLastTaskInfo()
                                             throws IllegalStateException

      Get the last task as a `StopWatch.TaskInfo` object.
      Throws:
        `IllegalStateException`

    - #### getTotalTimeNanos

          public long getTotalTimeNanos()

      Get the total time in nanoseconds for all tasks.
      Since:
        5.2
      See Also:
        `getTotalTimeMillis()`, `getTotalTimeSeconds()`

    - #### getTotalTimeMillis

          public long getTotalTimeMillis()

      Get the total time in milliseconds for all tasks.
      See Also:
        `getTotalTimeNanos()`, `getTotalTimeSeconds()`

    - #### getTotalTimeSeconds

          public double getTotalTimeSeconds()

      Get the total time in seconds for all tasks.
      See Also:
        `getTotalTimeNanos()`, `getTotalTimeMillis()`

    - #### getTaskCount

          public int getTaskCount()

      Get the number of tasks timed.

    - #### getTaskInfo

          public StopWatch.TaskInfo[] getTaskInfo()

      Get an array of the data for tasks performed.

    - #### shortSummary

          public String shortSummary()

      Get a short description of the total running time.

    - #### prettyPrint

          public String prettyPrint()

      Generate a string with a table describing all tasks performed.

      For custom reporting, call `getTaskInfo()` and use the task info directly.

    - #### toString

          public String toString()

      Generate an informative string describing all tasks performed

      For custom reporting, call `getTaskInfo()` and use the task info directly.

      Overrides:
        `toString` in class `Object`