public class TimingSession extends Object
A convenient timing object that supports multiple counter types.
Use the default constructor, with no arguments, to create a timing session with all of the available metrics. If required, an additional constructor may be used to provide a timing session with specific counters. E.g.:
new TimingSession(); // provides all available counter types new TimingSession(Counter.Type.WALL_CLOCK_TIME); // wall-clock time only new TimingSession(Counter.Type.CPU_TIME, Counter.Type.USER_TIME); // two counters
Note: For a list of available counters, refer to Counter.Type.
Call start() to start the timing session. When you are done, call
stop() to complete the timing session.
Use elapsedTime(Counter.Type) to retrieve the elapsed time for a particular
counter. E.g.:
double elapsedTimeNanos = cpuTime.elapsedTime(Counter.Type.CPU_TIME, TimeUnit.NANOSECONDS);
Although it is intended that the output methods elapsedTime() should be called
after the stop, some suitable, temporary data may be returned if the timing session is
on going. In this scenario, the initial values will be compared to the most up-to-date
ones, retrieved at the moment of the call.
Note: This class is not thread-safe. In a multi-thread context, different instances must be created for each thread.
Counter,
Counter.Type,
Stopwatch| Constructor and Description |
|---|
TimingSession()
Creates a new timing session with default counter types.
|
TimingSession(Counter.Type... types)
Creates a new timing session with custom counter types.
|
| Modifier and Type | Method and Description |
|---|---|
Duration |
elapsedTime(Counter.Type type)
A convenient method that returns the elapsed time of a specific counter.
|
double |
elapsedTime(Counter.Type type,
TimeUnit timeUnit)
A convenient method that returns the elapsed time of a specific counter, in the
specified time unit.
|
double |
elapsedTime(Counter.Type type,
TimeUnit timeUnit,
ConversionMode conversionMode)
A convenient method that returns the elapsed time of a specific counter, in the
specified time unit, by applying a custom
ConversionMode. |
Counter |
getCounter(Counter.Type type)
Returns the counter instance associated with a given type in this timing session.
|
Collection<Counter> |
getCounters()
Returns the counters associated with this timing session.
|
boolean |
isStarted()
Returns
true if this timing session is started. |
void |
reset()
Resets all counters associated with this timing session.
|
void |
start()
Starts the timing session.
|
void |
stop()
Stops the timing session.
|
public TimingSession()
public TimingSession(Counter.Type... types)
types - the counter types to setpublic void reset()
public void start()
IllegalStateException - if the session state is not suitable for this actionpublic void stop()
IllegalStateException - if the session state is not suitable for this actionpublic boolean isStarted()
true if this timing session is started.public Duration elapsedTime(Counter.Type type)
This has the same effect as calling:
timingSession.getCounter(type).elapsedTime()
type - the counter type to be fetchedIllegalArgumentException - if the type was not specified in this timing sessionpublic double elapsedTime(Counter.Type type, TimeUnit timeUnit)
This has the same effect as calling:
timingSession.getCounter(type).elapsedTime(timeUnit)
type - the counter type to be fetchedtimeUnit - the time unit to which the elapsed time will be convertedIllegalArgumentException - if the type was not specified in this timing sessionpublic double elapsedTime(Counter.Type type, TimeUnit timeUnit, ConversionMode conversionMode)
ConversionMode.
This has the same effect as calling:
timingSession.getCounter(type).elapsedTime(timeUnit, conversionMode)
type - the counter type to be fetchedtimeUnit - the time unit to which the elapsed time will be convertedconversionMode - the ConversionMode to be appliedIllegalArgumentException - if the type was not specified in this timing sessionpublic Collection<Counter> getCounters()
public Counter getCounter(Counter.Type type)
type - the counter type to be fetchedIllegalArgumentException - if the type was not specified in this timing sessionCopyright © 2022. All rights reserved.