Package io.debezium.util
Class Stopwatch
java.lang.Object
io.debezium.util.Stopwatch
A stopwatch for measuring durations. All NewStopwatch implementations are threadsafe, although using a single stopwatch
object across threads requires caution and care.
- Author:
- Randall Hauch
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classAbstract base class forStopwatch.Durationsimplementations.static interfaceThe average and total durations as measured by one or more stopwatches.private static final classAStopwatch.Durationsimplementation that accumulates alladdeddurations.private static final classAStopwatch.Durationsimplementation that only remembers the most recentlyaddedduration.static interfaceThe timing statistics for a recorded set of samples.static interfaceA set of stopwatches whose durations are combined. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StopwatchCreate a newStopwatchthat records all of the measured durations of the stopwatch.protected static StringCompute the readable string representation of the supplied duration.private static Stopwatch.Statisticsprotected static StopwatchcreateWith(Stopwatch.BaseDurations duration, Runnable uponStart, Runnable uponStop) Create a new stopwatch that updates the givenduration, and optionally has functions to be called after the stopwatch is started and stopped.abstract Stopwatch.DurationsGet the total and average durations measured by this stopwatch.static Stopwatch.StopwatchSetmultiple()Create a new set of stopwatches.static Stopwatchreusable()Create a newStopwatchthat can be reused.abstract Stopwatchstart()Start the stopwatch.abstract Stopwatchstop()Stop the stopwatch.
-
Constructor Details
-
Stopwatch
public Stopwatch()
-
-
Method Details
-
start
Start the stopwatch. Calling this method on an already-started stopwatch has no effect.- Returns:
- this object to enable chaining methods
- See Also:
-
stop
Stop the stopwatch. Calling this method on an already-stopped stopwatch has no effect.- Returns:
- this object to enable chaining methods
- See Also:
-
durations
Get the total and average durations measured by this stopwatch.- Returns:
- the durations; never null
-
createStatistics
-
reusable
Create a newStopwatchthat can be reused. The resultingdurations(), however, only reflect the most recently completed stopwatch interval.For example, the following code shows this behavior:
Stopwatch sw = Stopwatch.reusable(); sw.start(); sleep(3000); // sleep 3 seconds sw.stop(); print(sw.durations()); // total and average duration are each 3 seconds sw.start(); sleep(2000); // sleep 2 seconds sw.stop(); print(sw.durations()); // total and average duration are each 2 seconds
- Returns:
- the new stopwatch; never null
-
accumulating
Create a newStopwatchthat records all of the measured durations of the stopwatch.For example, the following code shows this behavior:
Stopwatch sw = Stopwatch.accumulating(); sw.start(); sleep(3000); // sleep 3 seconds sw.stop(); print(sw.durations()); // total and average duration are each 3 seconds sw.start(); sleep(2000); // sleep 2 seconds sw.stop(); print(sw.durations()); // total duration is now 5 seconds, average is 2.5 seconds
- Returns:
- the new stopwatch; never null
-
multiple
Create a new set of stopwatches. The resulting object is threadsafe, and eachStopwatchcreated byStopwatch.StopwatchSet.create()is also threadsafe.- Returns:
- the stopwatches set; never null
-
createWith
protected static Stopwatch createWith(Stopwatch.BaseDurations duration, Runnable uponStart, Runnable uponStop) Create a new stopwatch that updates the givenduration, and optionally has functions to be called after the stopwatch is started and stopped.The resulting stopwatch is threadsafe.
- Parameters:
duration- the duration that should be updated; may not be nulluponStart- the function that should be called when the stopwatch is successfully started (after not running); may be nulluponStop- the function that should be called when the stopwatch is successfully stopped (after it was running); may be null- Returns:
- the new stopwatch
-
asString
Compute the readable string representation of the supplied duration.- Parameters:
duration- the duration; may not be null- Returns:
- the string representation; never null
-