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
Modifier and TypeClassDescriptionprotected static class
Abstract base class forStopwatch.Durations
implementations.static interface
The average and total durations as measured by one or more stopwatches.private static final class
AStopwatch.Durations
implementation that accumulates alladded
durations.private static final class
AStopwatch.Durations
implementation that only remembers the most recentlyadded
duration.static interface
The timing statistics for a recorded set of samples.static interface
A set of stopwatches whose durations are combined. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Stopwatch
Create a newStopwatch
that records all of the measured durations of the stopwatch.protected static String
Compute the readable string representation of the supplied duration.private static Stopwatch.Statistics
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.abstract Stopwatch.Durations
Get the total and average durations measured by this stopwatch.static Stopwatch.StopwatchSet
multiple()
Create a new set of stopwatches.static Stopwatch
reusable()
Create a newStopwatch
that can be reused.abstract Stopwatch
start()
Start the stopwatch.abstract Stopwatch
stop()
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 newStopwatch
that 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 newStopwatch
that 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 eachStopwatch
created 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
-