org.elasticsearch.common
Class StopWatch

java.lang.Object
  extended by org.elasticsearch.common.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.currentTimeMillis(), 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-concepts and in development, rather than as part of production applications.


Nested Class Summary
static class StopWatch.TaskInfo
          Inner class to hold data about one task executed within the stop watch.
 
Constructor Summary
StopWatch()
          Construct a new stop watch.
StopWatch(String id)
          Construct a new stop watch with the given id.
 
Method Summary
 boolean isRunning()
          Return whether the stop watch is currently running.
 StopWatch keepTaskList(boolean keepTaskList)
          Determine whether the TaskInfo array is built over time.
 String lastTaskName()
          Return the name of the last task.
 TimeValue lastTaskTime()
          Return the time taken by the last task.
 String prettyPrint()
          Return a string with a table describing all tasks performed.
 String shortSummary()
          Return a short description of the total running time.
 StopWatch start()
          Start an unnamed task.
 StopWatch start(String taskName)
          Start a named task.
 StopWatch stop()
          Stop the current task.
 int taskCount()
          Return the number of tasks timed.
 StopWatch.TaskInfo[] taskInfo()
          Return an array of the data for tasks performed.
 String toString()
          Return an informative string describing all tasks performed For custom reporting, call getTaskInfo() and use the task info directly.
 TimeValue totalTime()
          Return the total time for all tasks.
 
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(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

keepTaskList

public StopWatch keepTaskList(boolean keepTaskList)
Determine whether the 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".


start

public StopWatch start()
                throws IllegalStateException
Start an unnamed task. The results are undefined if stop() or timing methods are called without invoking this method.

Throws:
IllegalStateException
See Also:
stop()

start

public StopWatch start(String taskName)
                throws IllegalStateException
Start a named task. The results are undefined if stop() or timing methods are called without invoking this method.

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

stop

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

Throws:
IllegalStateException
See Also:
start()

isRunning

public boolean isRunning()
Return whether the stop watch is currently running.


lastTaskTime

public TimeValue lastTaskTime()
                       throws IllegalStateException
Return the time taken by the last task.

Throws:
IllegalStateException

lastTaskName

public String lastTaskName()
                    throws IllegalStateException
Return the name of the last task.

Throws:
IllegalStateException

totalTime

public TimeValue totalTime()
Return the total time for all tasks.


taskCount

public int taskCount()
Return the number of tasks timed.


taskInfo

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


shortSummary

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


prettyPrint

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


toString

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

Overrides:
toString in class Object


Copyright © 2009-2012. All Rights Reserved.