Top Description Inners Fields Constructors Methods
org.springframework.util

public Class StopWatch

extends Object
Class Inheritance
Imports
java.text.NumberFormat, java.util.ArrayList, .List, .Locale, java.util.concurrent.TimeUnit, org.springframework.lang.Nullable

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. As of 6.1, the default time unit for String renderings is seconds with decimal points in nanosecond precision. Custom renderings with specific time units can be requested through prettyPrint(TimeUnit).

Authors
Rod Johnson, Juergen Hoeller, Sam Brannen
Since
May 2, 2001
See Also
start(), stop(), shortSummary(), prettyPrint()

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
StopWatch.TaskInfo

Nested class to hold data about one task executed within the StopWatch.

Field Summary

Modifier and TypeField and Description
private String
currentTaskName

Name of the current task.

private final String
id

Identifier of this StopWatch.

private StopWatch.TaskInfo
private long
startTimeNanos

Start time of the current task.

private int
private List<StopWatch.TaskInfo>
private long
totalTimeNanos

Total running time.

Constructor Summary

AccessConstructor and Description
public
StopWatch()

Construct a new StopWatch.

public
StopWatch(String
identifier for this stop watch
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.

Method Summary

Modifier and TypeMethod and Description
public String
currentTaskName()

Get the name of the currently running task, if any.

public String

Returns:

the id (empty String by default)
getId
()

Get the id of this StopWatch, as specified on construction.

public StopWatch.TaskInfo
getLastTaskInfo()

Deprecated since 6.1. as of 6.1, in favor of lastTaskInfo()
Get the last task as a TaskInfo object.
public String
getLastTaskName()

Deprecated since 6.1. as of 6.1, in favor of lastTaskInfo()
Get the name of the last task.
public long
getLastTaskTimeMillis()

Deprecated since 6.1. as of 6.1, in favor of lastTaskInfo()
Get the time taken by the last task in milliseconds.
public long
getLastTaskTimeNanos()

Deprecated since 6.1. as of 6.1, in favor of lastTaskInfo()
Get the time taken by the last task in nanoseconds.
public int
getTaskCount()

Get the number of tasks timed.

public StopWatch.TaskInfo[]
getTaskInfo()

Get an array of the data for tasks performed.

public double
getTotalTime(TimeUnit
the unit to use
timeUnit
)

Get the total time for all tasks in the requested time unit (with decimal points in nanosecond precision).

public long
getTotalTimeMillis()

Get the total time for all tasks in milliseconds.

public long
getTotalTimeNanos()

Get the total time for all tasks in nanoseconds.

public double
getTotalTimeSeconds()

Get the total time for all tasks in seconds.

public boolean
isRunning()

Determine whether this StopWatch is currently running.

public StopWatch.TaskInfo
lastTaskInfo()

Get the last task as a TaskInfo object.

public String
prettyPrint()

Generate a table describing all tasks performed in seconds (with decimal points in nanosecond precision).

public String
prettyPrint(TimeUnit
the unit to use for rendering total time and task time
timeUnit
)

Generate a table describing all tasks performed in the requested time unit (with decimal points in nanosecond precision).

public void
setKeepTaskList(boolean keepTaskList)

Configure whether the TaskInfo array is built over time.

public String
shortSummary()

Get a short description of the total running time in seconds.

public void
start()

Start an unnamed task.

public void
start(String
the name of the task to start
taskName
)

Start a named task.

public void
stop()

Stop the current task.

public String
toString()

Overrides java.lang.Object.toString.

Generate an informative string describing all tasks performed in seconds.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

currentTaskNameback to summary
private String currentTaskName

Name of the current task.

Annotations
@Nullable
idback to summary
private final String id

Identifier of this StopWatch.

Handy when we have output from multiple stop watches and need to distinguish between them in log or console output.

lastTaskInfoback to summary
private StopWatch.TaskInfo lastTaskInfo
Annotations
@Nullable
startTimeNanosback to summary
private long startTimeNanos

Start time of the current task.

taskCountback to summary
private int taskCount
taskListback to summary
private List<StopWatch.TaskInfo> taskList
Annotations
@Nullable
totalTimeNanosback to summary
private long totalTimeNanos

Total running time.

Constructor Detail

StopWatchback to summary
public StopWatch()

Construct a new StopWatch.

Does not start any task.

StopWatchback to summary
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:String

identifier for this stop watch

Method Detail

currentTaskNameback to summary
public String currentTaskName()

Get the name of the currently running task, if any.

Annotations
@Nullable
Since
4.2.2
See Also
isRunning()
getIdback to summary
public String getId()

Get the id of this StopWatch, as specified on construction.

Returns:String

the id (empty String by default)

Since
4.2.2
See Also
StopWatch(String)
getLastTaskInfoback to summary
public StopWatch.TaskInfo getLastTaskInfo() throws IllegalStateException

Deprecated

since 6.1.

as of 6.1, in favor of lastTaskInfo()

Get the last task as a TaskInfo object.

Annotations
@Deprecated
since:6.1
getLastTaskNameback to summary
public String getLastTaskName() throws IllegalStateException

Deprecated

since 6.1.

as of 6.1, in favor of lastTaskInfo()

Get the name of the last task.

Annotations
@Deprecated
since:6.1
See Also
TaskInfo#getTaskName()
getLastTaskTimeMillisback to summary
public long getLastTaskTimeMillis() throws IllegalStateException

Deprecated

since 6.1.

as of 6.1, in favor of lastTaskInfo()

Get the time taken by the last task in milliseconds.

Annotations
@Deprecated
since:6.1
See Also
TaskInfo#getTimeMillis()
getLastTaskTimeNanosback to summary
public long getLastTaskTimeNanos() throws IllegalStateException

Deprecated

since 6.1.

as of 6.1, in favor of lastTaskInfo()

Get the time taken by the last task in nanoseconds.

Annotations
@Deprecated
since:6.1
Since
5.2
See Also
TaskInfo#getTimeNanos()
getTaskCountback to summary
public int getTaskCount()

Get the number of tasks timed.

getTaskInfoback to summary
public StopWatch.TaskInfo[] getTaskInfo()

Get an array of the data for tasks performed.

See Also
setKeepTaskList
getTotalTimeback to summary
public double getTotalTime(TimeUnit timeUnit)

Get the total time for all tasks in the requested time unit (with decimal points in nanosecond precision).

Parameters
timeUnit:TimeUnit

the unit to use

Since
6.1
See Also
getTotalTimeNanos(), getTotalTimeMillis(), getTotalTimeSeconds()
getTotalTimeMillisback to summary
public long getTotalTimeMillis()

Get the total time for all tasks in milliseconds.

See Also
getTotalTime(TimeUnit)
getTotalTimeNanosback to summary
public long getTotalTimeNanos()

Get the total time for all tasks in nanoseconds.

Since
5.2
See Also
getTotalTime(TimeUnit)
getTotalTimeSecondsback to summary
public double getTotalTimeSeconds()

Get the total time for all tasks in seconds.

See Also
getTotalTime(TimeUnit)
isRunningback to summary
public boolean isRunning()

Determine whether this StopWatch is currently running.

See Also
currentTaskName()
lastTaskInfoback to summary
public StopWatch.TaskInfo lastTaskInfo() throws IllegalStateException

Get the last task as a TaskInfo object.

Exceptions
IllegalStateException:
if no tasks have run yet
Since
6.1
prettyPrintback to summary
public String prettyPrint()

Generate a table describing all tasks performed in seconds (with decimal points in nanosecond precision).

For custom reporting, call getTaskInfo() and use the data directly.

See Also
prettyPrint(TimeUnit), getTotalTimeSeconds(), TaskInfo#getTimeSeconds()
prettyPrintback to summary
public String prettyPrint(TimeUnit timeUnit)

Generate a table describing all tasks performed in the requested time unit (with decimal points in nanosecond precision).

For custom reporting, call getTaskInfo() and use the data directly.

Parameters
timeUnit:TimeUnit

the unit to use for rendering total time and task time

Since
6.1
See Also
prettyPrint(), getTotalTime(TimeUnit), TaskInfo#getTime(TimeUnit)
setKeepTaskListback to summary
public void setKeepTaskList(boolean keepTaskList)

Configure whether the TaskInfo array is built over time.

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

Default is true.

shortSummaryback to summary
public String shortSummary()

Get a short description of the total running time in seconds.

See Also
prettyPrint(), prettyPrint(TimeUnit)
startback to summary
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.

See Also
start(String), stop()
startback to summary
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:String

the name of the task to start

See Also
start(), stop()
stopback to summary
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.

See Also
start(), start(String)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Generate an informative string describing all tasks performed in seconds.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.

Annotations
@Override
See Also
prettyPrint(), prettyPrint(TimeUnit)
org.springframework.util back to summary

public final Class StopWatch.TaskInfo

extends Object
Class Inheritance

Nested class to hold data about one task executed within the StopWatch.

Field Summary

Modifier and TypeField and Description
private final String
private final long

Constructor Summary

AccessConstructor and Description
pack-priv
TaskInfo(String taskName, long timeNanos)

Method Summary

Modifier and TypeMethod and Description
public String
getTaskName()

Get the name of this task.

public double
getTime(TimeUnit
the unit to use
timeUnit
)

Get the time this task took in the requested time unit (with decimal points in nanosecond precision).

public long
getTimeMillis()

Get the time this task took in milliseconds.

public long
getTimeNanos()

Get the time this task took in nanoseconds.

public double
getTimeSeconds()

Get the time this task took in seconds.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

taskNameback to summary
private final String taskName
timeNanosback to summary
private final long timeNanos

Constructor Detail

TaskInfoback to summary
pack-priv TaskInfo(String taskName, long timeNanos)

Method Detail

getTaskNameback to summary
public String getTaskName()

Get the name of this task.

getTimeback to summary
public double getTime(TimeUnit timeUnit)

Get the time this task took in the requested time unit (with decimal points in nanosecond precision).

Parameters
timeUnit:TimeUnit

the unit to use

Since
6.1
See Also
getTimeNanos(), getTimeMillis(), getTimeSeconds()
getTimeMillisback to summary
public long getTimeMillis()

Get the time this task took in milliseconds.

See Also
getTime(TimeUnit)
getTimeNanosback to summary
public long getTimeNanos()

Get the time this task took in nanoseconds.

Since
5.2
See Also
getTime(TimeUnit)
getTimeSecondsback to summary
public double getTimeSeconds()

Get the time this task took in seconds.

See Also
getTime(TimeUnit)