com.interface21.util
Class StopWatch

java.lang.Object
  |
  +--com.interface21.util.StopWatch

public class StopWatch
extends java.lang.Object

Simple stop watch, allowing for timing of a number of tasks, exposing total running time and running time for each named task.
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.

Since:
May 2, 2001
Author:
Rod Johnson

Inner Class Summary
 class StopWatch.TaskInfo
          Inner class to hold data about one task
 
Constructor Summary
StopWatch()
          Construct a new stop watch.
StopWatch(java.lang.String id)
          Construct a new stop watch with the given id Does not start any task.
 
Method Summary
 long getLastInterval()
          Return the time taken by the last operation
 int getTaskCount()
          Return the number of tasks timed
 StopWatch.TaskInfo[] getTaskInfo()
          Return an array of the data for tasks performed
 long getTotalTime()
          Return the total time in milliseconds for all tasks
 double getTotalTimeSecs()
          Return the total time in seconds for all tasks
 java.lang.String prettyPrint()
          Return an informative string describing all tasks performed
 void start(java.lang.String task)
          Start a named task.
 void stop()
          Stop the current task.
 java.lang.String toString()
          Return 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 stop watch. Does not start any task.

StopWatch

public StopWatch(java.lang.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

start

public void start(java.lang.String task)
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()
Stop the current task. The results are undefined if timing methods are called without invoking at least one pair start()/stop() methods.

getTotalTime

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

getLastInterval

public long getLastInterval()
Return the time taken by the last operation
Returns:
the time taken by the last operation

getTotalTimeSecs

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

getTaskCount

public int getTaskCount()
Return the number of tasks timed
Returns:
the number of tasks timed

getTaskInfo

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

toString

public java.lang.String toString()
Return an informative string describing all tasks performed
Overrides:
toString in class java.lang.Object
Returns:
an informative string describing all tasks performed. For custom reporting, call getTaskInfo() and use the task info directly

prettyPrint

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


Rod Johnson and Spring contributors 2001-2003.