Class BenchmarkUtils
- java.lang.Object
-
- org.eclipse.epsilon.common.util.profiling.BenchmarkUtils
-
public final class BenchmarkUtils extends java.lang.ObjectUtility 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.MemoryUnitDEFAULT_MEMORY_UNITSstatic java.time.temporal.TemporalUnitDEFAULT_TIME_UNITSstatic java.lang.StringGC_PROFILE_STAGE
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddProfileInfo(java.util.Collection<ProfileDiagnostic> profileStages, java.lang.String stage, long nanos, long memory)static java.lang.StringformatDuration(java.time.Duration duration)NOTE: Only durations less than 24 hours are supported.static java.lang.StringformatExecutionStages(java.lang.Iterable<ProfileDiagnostic> profileInfo)static java.lang.StringformatExecutionTime(long nanos)static java.lang.StringformatExecutionTime(java.lang.String title, long nanos)static java.lang.StringformatExecutionTime(java.lang.String title, java.time.Duration time)static java.lang.StringformatExecutionTime(java.time.Duration time)static java.lang.StringformatExecutionTimes(java.lang.Iterable<ProfileDiagnostic> profileInfo)static java.lang.StringformatMemory(long amountInBytes, ProfileDiagnostic.MemoryUnit units)static java.lang.StringformatMemory(ProfileDiagnostic pd, java.util.Optional<ProfileDiagnostic.MemoryUnit> conversionFactor)static java.lang.StringformatMemoryConsumption(java.lang.Iterable<ProfileDiagnostic> profileInfo, ProfileDiagnostic.MemoryUnit units)static java.lang.StringformatMillis(long millis)static java.lang.StringformatNanos(long nanos)static java.lang.StringgetAvailableMemory(ProfileDiagnostic.MemoryUnit units)static java.lang.StringgetCpuName()Convenience method for getting the CPU model.static longgetCurrentMemoryUsage()static java.lang.StringgetMaxMemory(ProfileDiagnostic.MemoryUnit units)static intgetNumberOfHardwareThreads()Delegates toRuntime.availableProcessors().static ProfileDiagnosticgetProfileStageByName(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String stageName)Finds the stage for the given name.static java.lang.StringgetTime()static java.time.DurationgetTotalExecutionTimeFrom(java.util.Collection<ProfileDiagnostic> profiledStages)Sums the execution times of the execution stages.static longgetTotalMemoryUsage()static longmeasureAndAddGCTime(java.util.Collection<ProfileDiagnostic> profileInfo)CallsRuntime.gc(), measuring the time and accumulating it in the provided profiled stages using theGC_PROFILE_STAGEkey.static longmeasureTimeMillis(java.lang.Runnable code)static <T> longmeasureTimeMillis(java.util.function.Consumer<T> code, T argument)static <T,E extends java.lang.Exception>
longmeasureTimeMillis(CheckedConsumer<T,E> code, T argument)static <E extends java.lang.Exception>
longmeasureTimeMillis(CheckedRunnable<E> code)static longmeasureTimeNanos(java.lang.Runnable code)static <T> longmeasureTimeNanos(java.util.function.Consumer<T> code, T argument)static <T,E extends java.lang.Exception>
longmeasureTimeNanos(CheckedConsumer<T,E> code, T argument)static <E extends java.lang.Exception>
longmeasureTimeNanos(CheckedRunnable<E> code)static longnanosToMillis(long nanos)static voidprintExecutionTime(java.lang.String stageTag, java.lang.Runnable code)static <E extends java.lang.Exception>
voidprintExecutionTime(java.lang.String stageTag, CheckedRunnable<E> code)static voidprofileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, java.lang.Runnable code)static <T> voidprofileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, java.util.function.Consumer<T> code, T argument)static <T,R>
RprofileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, java.util.function.Function<T,R> code, T argument)static <R> RprofileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, java.util.function.Supplier<R> code)static <T,E extends java.lang.Exception>
voidprofileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, CheckedConsumer<T,E> code, T argument)static <T,R,E extends java.lang.Exception>
RprofileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.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 java.lang.Exception>
voidprofileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, CheckedRunnable<E> code)static <R,E extends java.lang.Exception>
RprofileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, CheckedSupplier<R,E> code)static ProfileDiagnosticremoveGCTimeFromStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.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 java.lang.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 java.time.temporal.TemporalUnit DEFAULT_TIME_UNITS
-
-
Method Detail
-
formatDuration
public static java.lang.String formatDuration(java.time.Duration duration)
NOTE: Only durations less than 24 hours are supported.
-
getTotalExecutionTimeFrom
public static java.time.Duration getTotalExecutionTimeFrom(java.util.Collection<ProfileDiagnostic> profiledStages)
Sums the execution times of the execution stages.- Parameters:
profiledStages- The profiled stages.- Returns:
- The total duration of
ProfileDiagnostic.executionTimefor 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 java.lang.String getTime()
- Returns:
- The current time as a String.
-
formatMemory
public static java.lang.String formatMemory(ProfileDiagnostic pd, java.util.Optional<ProfileDiagnostic.MemoryUnit> conversionFactor)
-
getAvailableMemory
public static java.lang.String getAvailableMemory(ProfileDiagnostic.MemoryUnit units)
-
getMaxMemory
public static java.lang.String getMaxMemory(ProfileDiagnostic.MemoryUnit units)
-
formatMemory
public static java.lang.String formatMemory(long amountInBytes, ProfileDiagnostic.MemoryUnit units)
-
getCpuName
public static java.lang.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(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String stageName)
Finds the stage for the given name.- Parameters:
profileInfo- The profiled stages.stageName- The stage name.- Returns:
- The prifle stage, or
nullif there wasn't one.
-
removeGCTimeFromStage
public static ProfileDiagnostic removeGCTimeFromStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.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(java.util.Collection<ProfileDiagnostic> profileInfo)
CallsRuntime.gc(), measuring the time and accumulating it in the provided profiled stages using theGC_PROFILE_STAGEkey.- Parameters:
profileInfo- The profiled stages to update.- Returns:
- The time taken to perform the GC invocation.
-
profileExecutionStage
public static <T,R,E extends java.lang.Exception> R profileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, CheckedFunction<T,R,E> code, T argument) throws E extends java.lang.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 java.lang.Exception
-
profileExecutionStage
public static <T,E extends java.lang.Exception> void profileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, CheckedConsumer<T,E> code, T argument) throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
-
profileExecutionStage
public static <R,E extends java.lang.Exception> R profileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, CheckedSupplier<R,E> code) throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
-
profileExecutionStage
public static <E extends java.lang.Exception> void profileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, CheckedRunnable<E> code) throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
-
profileExecutionStage
public static <T,R> R profileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, java.util.function.Function<T,R> code, T argument)
-
profileExecutionStage
public static <T> void profileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, java.util.function.Consumer<T> code, T argument)
-
profileExecutionStage
public static <R> R profileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, java.util.function.Supplier<R> code)
-
profileExecutionStage
public static void profileExecutionStage(java.util.Collection<ProfileDiagnostic> profileInfo, java.lang.String description, java.lang.Runnable code)
-
measureTimeNanos
public static <T,E extends java.lang.Exception> long measureTimeNanos(CheckedConsumer<T,E> code, T argument) throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
-
measureTimeNanos
public static <E extends java.lang.Exception> long measureTimeNanos(CheckedRunnable<E> code) throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
-
measureTimeNanos
public static long measureTimeNanos(java.lang.Runnable code)
-
measureTimeNanos
public static <T> long measureTimeNanos(java.util.function.Consumer<T> code, T argument)
-
measureTimeMillis
public static <T,E extends java.lang.Exception> long measureTimeMillis(CheckedConsumer<T,E> code, T argument) throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
-
measureTimeMillis
public static <E extends java.lang.Exception> long measureTimeMillis(CheckedRunnable<E> code) throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
-
measureTimeMillis
public static long measureTimeMillis(java.lang.Runnable code)
-
measureTimeMillis
public static <T> long measureTimeMillis(java.util.function.Consumer<T> code, T argument)
-
printExecutionTime
public static void printExecutionTime(java.lang.String stageTag, java.lang.Runnable code)
-
printExecutionTime
public static <E extends java.lang.Exception> void printExecutionTime(java.lang.String stageTag, CheckedRunnable<E> code) throws E extends java.lang.Exception- Throws:
E extends java.lang.Exception
-
addProfileInfo
public static void addProfileInfo(java.util.Collection<ProfileDiagnostic> profileStages, java.lang.String stage, long nanos, long memory)
-
formatExecutionStages
public static java.lang.String formatExecutionStages(java.lang.Iterable<ProfileDiagnostic> profileInfo)
-
formatMemoryConsumption
public static java.lang.String formatMemoryConsumption(java.lang.Iterable<ProfileDiagnostic> profileInfo, ProfileDiagnostic.MemoryUnit units)
-
formatExecutionTimes
public static java.lang.String formatExecutionTimes(java.lang.Iterable<ProfileDiagnostic> profileInfo)
-
formatExecutionTime
public static java.lang.String formatExecutionTime(java.time.Duration time)
-
formatExecutionTime
public static java.lang.String formatExecutionTime(java.lang.String title, java.time.Duration time)
-
formatExecutionTime
public static java.lang.String formatExecutionTime(long nanos)
-
formatExecutionTime
public static java.lang.String formatExecutionTime(java.lang.String title, long nanos)
-
formatMillis
public static java.lang.String formatMillis(long millis)
-
formatNanos
public static java.lang.String formatNanos(long nanos)
-
nanosToMillis
public static long nanosToMillis(long nanos)
-
-