org.springframework.util
Class StopWatch


public class StopWatch

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.currentTimeMillis(), improving the readability of application code and reducing the likelihood of calculation errors.

Note that this object is not designed to be threadsafe, and does not use synchronization or threading. Therefore it is safe to invoke it from EJBs.

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

Author:
Rod Johnson
Version: $Id: StopWatch.java,v 1.4 2003/12/30 01:18:51 jhoeller Exp $
Since: May 2, 2001

Inner Class Summary
 public static classStopWatch.TaskInfo
          Inner class to hold data about one task executed within the stopwatch

Constructor Summary
StopWatch()
          Construct a new stop watch.
StopWatch(String id)
          Construct a new stop watch with the given id.

Method Summary
 booleangetKeepTaskList()
           
 longgetLastInterval()
          Returns the time taken by the last operation.
 intgetTaskCount()
          Returns the number of tasks timed.
 TaskInfo[]getTaskInfo()
          Returns an array of the data for tasks performed.
 longgetTotalTime()
          Returns the total time in milliseconds for all tasks.
 doublegetTotalTimeSecs()
          Returns the total time in seconds for all tasks.
 booleanisRunning()
          Returns whether the stopwatch is currently running.
 StringprettyPrint()
          Returns a string with a table describing all tasks performed.
 voidsetKeepTaskList(boolean keepTaskList)
          Determines whether TaskInfo array is built over time.
 StringshortSummary()
          Returns a short description of the total running time.
 voidstart(String task)
          Start a named task.
 voidstop()
          Stop the current task.
 StringtoString()
          Returns an informative string describing all tasks performed For custom reporting, call getTaskInfo() and use the task info directly.

Constructor Detail

StopWatch

public StopWatch()
Construct a new stop watch. Does not start any task.

StopWatch

public StopWatch(String id)
Construct a new stop watch with the given id. Does not start any task.
Parameters:
id - identifier for this stop watch. Handy when we have output from multiple stop watches and need to distinguish between them.
Method Detail

getKeepTaskList

public boolean getKeepTaskList()

getLastInterval

public long getLastInterval()
throws java.lang.IllegalStateException
Returns the time taken by the last operation.

getTaskCount

public int getTaskCount()
Returns the number of tasks timed.

getTaskInfo

public TaskInfo[] getTaskInfo()
Returns an array of the data for tasks performed.

getTotalTime

public long getTotalTime()
Returns the total time in milliseconds for all tasks.

getTotalTimeSecs

public double getTotalTimeSecs()
Returns the total time in seconds for all tasks.

isRunning

public boolean isRunning()
Returns whether the stopwatch is currently running.

prettyPrint

public String prettyPrint()
Returns a string with a table describing all tasks performed. For custom reporting, call getTaskInfo() and use the task info directly.

setKeepTaskList

public void setKeepTaskList(boolean keepTaskList)
Determines whether TaskInfo array is built over time. Set this to false when using a stopwatch for millions of intervals, or the task info structure will consume excessive memory. Default is true.
Parameters:
keepTaskList

shortSummary

public String shortSummary()
Returns a short description of the total running time.

start

public void start(String task)
throws java.lang.IllegalStateException
Start a named task. The results are undefined if stop() or timing methods are called without invoking this method.
Parameters:
task - name of the task to start

stop

public void stop()
throws java.lang.IllegalStateException
Stop the current task. The results are undefined if timing methods are called without invoking at least one pair start()/stop() methods.

toString

public String toString()
Returns an informative string describing all tasks performed For custom reporting, call getTaskInfo() and use the task info directly.

Association Links

to Class java.util.List

List of TaskInfo objects

to Class java.lang.String

Name of the current task

to Class java.lang.String

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

to Class org.springframework.util.StopWatch.TaskInfo