org.javasimon
Class StopwatchImpl

java.lang.Object
  extended by org.javasimon.AbstractSimon
      extended by org.javasimon.StopwatchImpl
All Implemented Interfaces:
HasAttributes, Simon, Stopwatch

final class StopwatchImpl
extends AbstractSimon
implements Stopwatch

Class implements Stopwatch interface - see there for how to use Stopwatch.

Author:
Richard "Virgo" Richter
See Also:
Stopwatch

Field Summary
private  long active
           
private  long counter
           
private  long last
           
private  long max
           
private  long maxActive
           
private  long maxActiveTimestamp
           
private  long maxTimestamp
           
private  double mean
           
private  double mean2
           
private  long min
           
private  long minTimestamp
           
private  long total
           
 
Fields inherited from class org.javasimon.AbstractSimon
enabled, firstUsage, lastUsage, manager
 
Constructor Summary
StopwatchImpl(String name, Manager manager)
          Constructs Stopwatch Simon with a specified name and for the specified manager.
 
Method Summary
private  void activeStart()
           
private  long addSplit(long split)
           
 Stopwatch addSplit(Split split)
          Adds Split to the stopwatch which is useful for aggregation of splits created for other stopwatch.
 Stopwatch addTime(long ns)
          Adds split time in nanoseconds to total time of the stopwatch.
 long getActive()
          Returns current number of measured splits (concurrently running).
 long getCounter()
          Returns usage count of the stopwatch.
 long getLast()
          Returns value of the last added split - wheter it was added directly or with stop method.
 long getMax()
          Returns maximal time split value in nanoseconds.
 long getMaxActive()
          Returns peek value of active concurrent splits.
 long getMaxActiveTimestamp()
          Retruns ms timestamp when the last peek of the active split count occured.
 long getMaxTimestamp()
          Returns ms timestamp when the max value was measured.
 double getMean()
          Returns mean value (average) of all measured values.
 long getMin()
          Returns minimal time split value in nanoseconds.
 long getMinTimestamp()
          Returns ms timestamp when the min value was measured.
 double getStandardDeviation()
          Returns standard deviation for all measured values.
 long getTotal()
          Returns total sum of all split times in nanoseconds.
 double getVariance()
          Returns unbiased estimate of the population variance.
 double getVarianceN()
          Returns variance value of all measured values (entire population).
 Stopwatch reset()
          Resets the Simon - clears total time, min, max, usage stats, etc.
 StopwatchSample sample()
          Samples Simon values and returns them in a Java Bean derived from Sample interface.
 StopwatchSample sampleAndReset()
          Samples Simon values and returns them in a Java Bean derived from Sample interface and resets the Simon.
 Split start()
          Starts the new split for this stopwatch.
(package private)  void stop(Split split, long start, long nowNanos, String subSimon)
          Protected method doing the stop work based on provided start nano-time.
 String toString()
          Returns Simon basic information, total time, counter, max value and min value as a human readable string.
private  void updateUsages()
          Updates usage statistics.
private  void updateUsages(long nowNanos)
          Updates usage statistics without using System.currentTimeMillis() if client code already has current nano timer value.
 
Methods inherited from class org.javasimon.AbstractSimon
addChild, getAttribute, getAttribute, getAttributeNames, getCopyAsSortedMap, getFirstUsage, getChildren, getLastReset, getLastUsage, getName, getNote, getParent, getState, isEnabled, removeAttribute, replaceChild, resetCommon, sampleCommon, setAttribute, setNote, setParent, setState
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.javasimon.Simon
getFirstUsage, getChildren, getLastReset, getLastUsage, getName, getNote, getParent, getState, isEnabled, setNote, setState
 
Methods inherited from interface org.javasimon.HasAttributes
getAttribute, getAttribute, getAttributeNames, getCopyAsSortedMap, removeAttribute, setAttribute
 

Field Detail

total

private long total

counter

private long counter

active

private long active

max

private long max

maxTimestamp

private long maxTimestamp

maxActive

private long maxActive

maxActiveTimestamp

private long maxActiveTimestamp

min

private long min

minTimestamp

private long minTimestamp

last

private long last

mean

private double mean

mean2

private double mean2
Constructor Detail

StopwatchImpl

StopwatchImpl(String name,
              Manager manager)
Constructs Stopwatch Simon with a specified name and for the specified manager.

Parameters:
name - Simon's name
manager - owning manager
Method Detail

addTime

public Stopwatch addTime(long ns)
Description copied from interface: Stopwatch
Adds split time in nanoseconds to total time of the stopwatch.

Specified by:
addTime in interface Stopwatch
Parameters:
ns - split time
Returns:
this stopwatch

addSplit

public Stopwatch addSplit(Split split)
Description copied from interface: Stopwatch
Adds Split to the stopwatch which is useful for aggregation of splits created for other stopwatch. Split object should be stopped. Main difference is the callback method called as Callback.onStopwatchAdd(Stopwatch, Split, StopwatchSample) provides split object to the callback.

Usage examples:

Split split = Split.start(); // no stopwatch needed
...
someStopwatch.addSplit(split.stop()); // you may omit stop(), if you does not use the split after this point

Specified by:
addSplit in interface Stopwatch
Parameters:
split - split object (should be stopped)
Returns:
this stopwatch

start

public Split start()
Description copied from interface: Stopwatch
Starts the new split for this stopwatch. This action does not hold any resources and if Split object is collected, no leak occurs. However, active count is increased and without stopping the split active count stays increased which may render that information useless.

Specified by:
start in interface Stopwatch
Returns:
split object
See Also:
Split.stop()

stop

void stop(Split split,
          long start,
          long nowNanos,
          String subSimon)
Protected method doing the stop work based on provided start nano-time.

Parameters:
split - Split object that has been stopped
start - start nano-time of the split @return split time in ns
nowNanos - current nano time
subSimon - name of the sub-stopwatch (hierarchy delimiter is added automatically), may be null

activeStart

private void activeStart()

reset

public Stopwatch reset()
Description copied from interface: Stopwatch
Resets the Simon - clears total time, min, max, usage stats, etc. Split times that started before reset will be counted when appropriate stop is called, so no split time is ignored by the stopwatch. Active count is not reset.

Specified by:
reset in interface Simon
Specified by:
reset in interface Stopwatch
Returns:
returns this

addSplit

private long addSplit(long split)

getMean

public double getMean()
Description copied from interface: Stopwatch
Returns mean value (average) of all measured values.

Specified by:
getMean in interface Stopwatch
Returns:
mean value

getVarianceN

public double getVarianceN()
Description copied from interface: Stopwatch
Returns variance value of all measured values (entire population).

Specified by:
getVarianceN in interface Stopwatch
Returns:
entire population variance

getVariance

public double getVariance()
Description copied from interface: Stopwatch
Returns unbiased estimate of the population variance.

Specified by:
getVariance in interface Stopwatch
Returns:
unbiased estimated variance

getStandardDeviation

public double getStandardDeviation()
Description copied from interface: Stopwatch
Returns standard deviation for all measured values.

Specified by:
getStandardDeviation in interface Stopwatch
Returns:
standard deviation

getTotal

public long getTotal()
Description copied from interface: Stopwatch
Returns total sum of all split times in nanoseconds.

Specified by:
getTotal in interface Stopwatch
Returns:
total time of the stopwatch in nanoseconds

getLast

public long getLast()
Description copied from interface: Stopwatch
Returns value of the last added split - wheter it was added directly or with stop method.

Specified by:
getLast in interface Stopwatch
Returns:
value of the last added split

getCounter

public long getCounter()
Description copied from interface: Stopwatch
Returns usage count of the stopwatch. Counter is increased by addTime and stop - that means that it's updated every time the next time split is added.

Specified by:
getCounter in interface Stopwatch
Returns:
count of time splits

getMax

public long getMax()
Description copied from interface: Stopwatch
Returns maximal time split value in nanoseconds.

Specified by:
getMax in interface Stopwatch
Returns:
maximal time split in nanoseconds

getMin

public long getMin()
Description copied from interface: Stopwatch
Returns minimal time split value in nanoseconds.

Specified by:
getMin in interface Stopwatch
Returns:
minimal time split in nanoseconds

getMaxTimestamp

public long getMaxTimestamp()
Description copied from interface: Stopwatch
Returns ms timestamp when the max value was measured.

Specified by:
getMaxTimestamp in interface Stopwatch
Returns:
ms timestamp of the max value measurement

getMinTimestamp

public long getMinTimestamp()
Description copied from interface: Stopwatch
Returns ms timestamp when the min value was measured.

Specified by:
getMinTimestamp in interface Stopwatch
Returns:
ms timestamp of the min value measurement

getActive

public long getActive()
Description copied from interface: Stopwatch
Returns current number of measured splits (concurrently running). This counter can show more splits than is measured at any moment if some splits were "forgotten" (not stopped and garbage collected). This does not imply any resource leak, just bad practice of not stopping Splits somewhere in the client code.

Specified by:
getActive in interface Stopwatch
Returns:
current number of active splits

getMaxActive

public long getMaxActive()
Description copied from interface: Stopwatch
Returns peek value of active concurrent splits.

Specified by:
getMaxActive in interface Stopwatch
Returns:
maximum reached value of active splits

getMaxActiveTimestamp

public long getMaxActiveTimestamp()
Description copied from interface: Stopwatch
Retruns ms timestamp when the last peek of the active split count occured.

Specified by:
getMaxActiveTimestamp in interface Stopwatch
Returns:
ms timestamp of the last peek of the active split count

sampleAndReset

public StopwatchSample sampleAndReset()
Description copied from interface: Simon
Samples Simon values and returns them in a Java Bean derived from Sample interface and resets the Simon. Operation is synchronized to assure atomicity.

Specified by:
sampleAndReset in interface Simon
Specified by:
sampleAndReset in interface Stopwatch
Returns:
sample containing all Simon values

sample

public StopwatchSample sample()
Description copied from interface: Simon
Samples Simon values and returns them in a Java Bean derived from Sample interface.

Specified by:
sample in interface Simon
Specified by:
sample in interface Stopwatch
Returns:
sample containing all Simon values

updateUsages

private void updateUsages(long nowNanos)
Updates usage statistics without using System.currentTimeMillis() if client code already has current nano timer value.

Parameters:
nowNanos - current value of nano timer

updateUsages

private void updateUsages()
Updates usage statistics. If current nano timer value is available use updateUsages(long) instead.


toString

public String toString()
Returns Simon basic information, total time, counter, max value and min value as a human readable string.

Overrides:
toString in class AbstractSimon
Returns:
basic information, total time, counter, max and min values
See Also:
AbstractSimon.toString()


Copyright © 2013. All Rights Reserved.