org.springframework.util
Class StopWatch

java.lang.Object
  extended byorg.springframework.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. 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.

Since:
May 2, 2001
Version:
$Id: StopWatch.java,v 1.6 2004/03/18 02:46:10 trisberg Exp $
Author:
Rod Johnson

Nested Class Summary
static class StopWatch.TaskInfo
          Inner class to hold data about one task executed within the stopwatch
 
Constructor Summary
StopWatch()
          Construct a new stop watch.
StopWatch(java.lang.String id)
          Construct a new stop watch with the given id.
 
Method Summary
 boolean getKeepTaskList()
           
 long getLastInterval()
          Returns the time taken by the last operation.
 int getTaskCount()
          Returns the number of tasks timed.
 StopWatch.TaskInfo[] getTaskInfo()
          Returns an array of the data for tasks performed.
 long getTotalTime()
          Returns the total time in milliseconds for all tasks.
 double getTotalTimeSecs()
          Returns the total time in seconds for all tasks.
 boolean isRunning()
          Returns whether the stopwatch is currently running.
 java.lang.String prettyPrint()
          Returns a string with a table describing all tasks performed.
 void setKeepTaskList(boolean keepTaskList)
          Determines whether TaskInfo array is built over time.
 java.lang.String shortSummary()
          Returns a short description of the total running time.
 void start(java.lang.String task)
          Start a named task.
 void stop()
          Stop the current task.
 java.lang.String toString()
          Returns an informative string describing all tasks performed For custom reporting, call getTaskInfo() and use the task info directly.
 
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

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 -

getKeepTaskList

public boolean getKeepTaskList()

start

public void start(java.lang.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
Throws:
java.lang.IllegalStateException

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.

Throws:
java.lang.IllegalStateException

getTotalTime

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


getLastInterval

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

Throws:
java.lang.IllegalStateException

getTotalTimeSecs

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


getTaskCount

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


getTaskInfo

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


isRunning

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


shortSummary

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


prettyPrint

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


toString

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



Copyright (C) 2003-2004 The Spring Framework Project.