Class BenchmarkUtils
- java.lang.Object
-
- org.eclipse.epsilon.common.util.profiling.BenchmarkUtils
-
public final class BenchmarkUtils extends Object
Utility class for performance profiling; mainly for measuring execution times. All long temporal values are assumed to be nanoseconds if not otherwise specified. Similarly, all long memory values are assumed to be in bytes if not otherwise specified.- Since:
- 1.6
- Author:
- Sina Madani
-
-
Field Summary
Fields Modifier and Type Field Description static ProfileDiagnostic.MemoryUnit
DEFAULT_MEMORY_UNITS
static TemporalUnit
DEFAULT_TIME_UNITS
static String
GC_PROFILE_STAGE
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addProfileInfo(Collection<ProfileDiagnostic> profileStages, String stage, long nanos, long memory)
static String
formatDuration(Duration duration)
NOTE: Only durations less than 24 hours are supported.static String
formatExecutionStages(Iterable<ProfileDiagnostic> profileInfo)
static String
formatExecutionTime(long nanos)
static String
formatExecutionTime(String title, long nanos)
static String
formatExecutionTime(String title, Duration time)
static String
formatExecutionTime(Duration time)
static String
formatExecutionTimes(Iterable<ProfileDiagnostic> profileInfo)
static String
formatMemory(long amountInBytes, ProfileDiagnostic.MemoryUnit units)
static String
formatMemory(ProfileDiagnostic pd, Optional<ProfileDiagnostic.MemoryUnit> conversionFactor)
static String
formatMemoryConsumption(Iterable<ProfileDiagnostic> profileInfo, ProfileDiagnostic.MemoryUnit units)
static String
formatMillis(long millis)
static String
formatNanos(long nanos)
static String
getAvailableMemory(ProfileDiagnostic.MemoryUnit units)
static String
getCpuName()
Convenience method for getting the CPU model.static long
getCurrentMemoryUsage()
static String
getMaxMemory(ProfileDiagnostic.MemoryUnit units)
static int
getNumberOfHardwareThreads()
Delegates toRuntime.availableProcessors()
.static ProfileDiagnostic
getProfileStageByName(Collection<ProfileDiagnostic> profileInfo, String stageName)
Finds the stage for the given name.static String
getTime()
static Duration
getTotalExecutionTimeFrom(Collection<ProfileDiagnostic> profiledStages)
Sums the execution times of the execution stages.static long
getTotalMemoryUsage()
static long
measureAndAddGCTime(Collection<ProfileDiagnostic> profileInfo)
CallsRuntime.gc()
, measuring the time and accumulating it in the provided profiled stages using theGC_PROFILE_STAGE
key.static long
measureTimeMillis(Runnable code)
static <T> long
measureTimeMillis(Consumer<T> code, T argument)
static <T,E extends Exception>
longmeasureTimeMillis(CheckedConsumer<T,E> code, T argument)
static <E extends Exception>
longmeasureTimeMillis(CheckedRunnable<E> code)
static long
measureTimeNanos(Runnable code)
static <T> long
measureTimeNanos(Consumer<T> code, T argument)
static <T,E extends Exception>
longmeasureTimeNanos(CheckedConsumer<T,E> code, T argument)
static <E extends Exception>
longmeasureTimeNanos(CheckedRunnable<E> code)
static long
nanosToMillis(long nanos)
static void
printExecutionTime(String stageTag, Runnable code)
static <E extends Exception>
voidprintExecutionTime(String stageTag, CheckedRunnable<E> code)
static void
profileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, Runnable code)
static <T> void
profileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, Consumer<T> code, T argument)
static <T,R>
RprofileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, Function<T,R> code, T argument)
static <R> R
profileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, Supplier<R> code)
static <T,E extends Exception>
voidprofileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, CheckedConsumer<T,E> code, T argument)
static <T,R,E extends Exception>
RprofileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, CheckedFunction<T,R,E> code, T argument)
Creates a new ProfileDiagnostic and adds it to the collection of profiled stages, measuring execution time and memory consumption.static <E extends Exception>
voidprofileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, CheckedRunnable<E> code)
static <R,E extends Exception>
RprofileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, CheckedSupplier<R,E> code)
static ProfileDiagnostic
removeGCTimeFromStage(Collection<ProfileDiagnostic> profileInfo, String stageName)
Performs aRuntime.gc()
, takes into account the time taken to do so and subtracts it from the given stage, identified by its name.
-
-
-
Field Detail
-
GC_PROFILE_STAGE
public static final String GC_PROFILE_STAGE
- See Also:
- Constant Field Values
-
DEFAULT_MEMORY_UNITS
public static final ProfileDiagnostic.MemoryUnit DEFAULT_MEMORY_UNITS
-
DEFAULT_TIME_UNITS
public static final TemporalUnit DEFAULT_TIME_UNITS
-
-
Method Detail
-
formatDuration
public static String formatDuration(Duration duration)
NOTE: Only durations less than 24 hours are supported.
-
getTotalExecutionTimeFrom
public static Duration getTotalExecutionTimeFrom(Collection<ProfileDiagnostic> profiledStages)
Sums the execution times of the execution stages.- Parameters:
profiledStages
- The profiled stages.- Returns:
- The total duration of
ProfileDiagnostic.executionTime
for the given profiled stages.
-
getTotalMemoryUsage
public static long getTotalMemoryUsage()
- Returns:
- The sum of peak memory usage for all memory pools.
-
getCurrentMemoryUsage
public static long getCurrentMemoryUsage()
-
getTime
public static String getTime()
- Returns:
- The current time as a String.
-
formatMemory
public static String formatMemory(ProfileDiagnostic pd, Optional<ProfileDiagnostic.MemoryUnit> conversionFactor)
-
getAvailableMemory
public static String getAvailableMemory(ProfileDiagnostic.MemoryUnit units)
-
getMaxMemory
public static String getMaxMemory(ProfileDiagnostic.MemoryUnit units)
-
formatMemory
public static String formatMemory(long amountInBytes, ProfileDiagnostic.MemoryUnit units)
-
getCpuName
public static String getCpuName()
Convenience method for getting the CPU model.- Returns:
- The CPU model as reported by the operating system.
- Since:
- 1.6
-
getNumberOfHardwareThreads
public static int getNumberOfHardwareThreads()
Delegates toRuntime.availableProcessors()
.- Returns:
- The number of logical cores available to the JVM.
-
getProfileStageByName
public static ProfileDiagnostic getProfileStageByName(Collection<ProfileDiagnostic> profileInfo, String stageName)
Finds the stage for the given name.- Parameters:
profileInfo
- The profiled stages.stageName
- The stage name.- Returns:
- The prifle stage, or
null
if there wasn't one.
-
removeGCTimeFromStage
public static ProfileDiagnostic removeGCTimeFromStage(Collection<ProfileDiagnostic> profileInfo, String stageName)
Performs aRuntime.gc()
, takes into account the time taken to do so and subtracts it from the given stage, identified by its name.- Parameters:
profileInfo
- The profiled stages.stageName
- The stage identifier to subtract garbage collection time from.- Returns:
- The updated stage, for convenience.
-
measureAndAddGCTime
public static long measureAndAddGCTime(Collection<ProfileDiagnostic> profileInfo)
CallsRuntime.gc()
, measuring the time and accumulating it in the provided profiled stages using theGC_PROFILE_STAGE
key.- Parameters:
profileInfo
- The profiled stages to update.- Returns:
- The time taken to perform the GC invocation.
-
profileExecutionStage
public static <T,R,E extends Exception> R profileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, CheckedFunction<T,R,E> code, T argument) throws E extends Exception
Creates a new ProfileDiagnostic and adds it to the collection of profiled stages, measuring execution time and memory consumption.- Parameters:
profileInfo
- The execution stages to write to.description
- The name of the execution stage.code
- The transformation to profile.argument
- The argument to the transformation code.- Returns:
- The result of the transformation code.
- Throws:
E
- Any exception thrown from the transformation code.E extends Exception
-
profileExecutionStage
public static <T,E extends Exception> void profileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, CheckedConsumer<T,E> code, T argument) throws E extends Exception
- Throws:
E extends Exception
-
profileExecutionStage
public static <R,E extends Exception> R profileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, CheckedSupplier<R,E> code) throws E extends Exception
- Throws:
E extends Exception
-
profileExecutionStage
public static <E extends Exception> void profileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, CheckedRunnable<E> code) throws E extends Exception
- Throws:
E extends Exception
-
profileExecutionStage
public static <T,R> R profileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, Function<T,R> code, T argument)
-
profileExecutionStage
public static <T> void profileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, Consumer<T> code, T argument)
-
profileExecutionStage
public static <R> R profileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, Supplier<R> code)
-
profileExecutionStage
public static void profileExecutionStage(Collection<ProfileDiagnostic> profileInfo, String description, Runnable code)
-
measureTimeNanos
public static <T,E extends Exception> long measureTimeNanos(CheckedConsumer<T,E> code, T argument) throws E extends Exception
- Throws:
E extends Exception
-
measureTimeNanos
public static <E extends Exception> long measureTimeNanos(CheckedRunnable<E> code) throws E extends Exception
- Throws:
E extends Exception
-
measureTimeNanos
public static long measureTimeNanos(Runnable code)
-
measureTimeNanos
public static <T> long measureTimeNanos(Consumer<T> code, T argument)
-
measureTimeMillis
public static <T,E extends Exception> long measureTimeMillis(CheckedConsumer<T,E> code, T argument) throws E extends Exception
- Throws:
E extends Exception
-
measureTimeMillis
public static <E extends Exception> long measureTimeMillis(CheckedRunnable<E> code) throws E extends Exception
- Throws:
E extends Exception
-
measureTimeMillis
public static long measureTimeMillis(Runnable code)
-
measureTimeMillis
public static <T> long measureTimeMillis(Consumer<T> code, T argument)
-
printExecutionTime
public static <E extends Exception> void printExecutionTime(String stageTag, CheckedRunnable<E> code) throws E extends Exception
- Throws:
E extends Exception
-
addProfileInfo
public static void addProfileInfo(Collection<ProfileDiagnostic> profileStages, String stage, long nanos, long memory)
-
formatExecutionStages
public static String formatExecutionStages(Iterable<ProfileDiagnostic> profileInfo)
-
formatMemoryConsumption
public static String formatMemoryConsumption(Iterable<ProfileDiagnostic> profileInfo, ProfileDiagnostic.MemoryUnit units)
-
formatExecutionTimes
public static String formatExecutionTimes(Iterable<ProfileDiagnostic> profileInfo)
-
formatExecutionTime
public static String formatExecutionTime(long nanos)
-
formatMillis
public static String formatMillis(long millis)
-
formatNanos
public static String formatNanos(long nanos)
-
nanosToMillis
public static long nanosToMillis(long nanos)
-
-