Class HistogramBasedStopwatch<T extends Enum<?>>

  • All Implemented Interfaces:
    MultistageStopwatch<T>

    public final class HistogramBasedStopwatch<T extends Enum<?>>
    extends Object
    This type of MultistageStopwatch populates a timing Histogram for each type of operation with counts representing how long those operations took.

    Example: extending the example in MultistageStopwatch:

     // Get a histogram describing how long the athletes took to complete the event.
     Histogram<Long> bicyclingTimes = stopwatch.timingHistogram(BICYCLING);
    
     Function<Long, String> longFormatter = (nanos) -> String.format("%d s", nanos / 1_000_000_000L);
     ConsoleHistogramFormatter<Long> histoFormatter =
         new ConsoleHistogramFormatter(longFormatter, 20);
     System.out.println("Distribution of bicycling race finishing times, in seconds:");
     System.out.println(histoFormatter.format(bicyclingTimes));
     

    (If you just want a single stopwatch that can be stopped and started multiple times and you care about the total elapsed time, but not the number of cycles or the histogram, consider using Stopwatch instead. This class is useful for timing several related operations that are stages in a larger activity, since it allows examination of duration values of individual stages and of the overall activity.)

    • Constructor Detail

      • HistogramBasedStopwatch

        public HistogramBasedStopwatch​(String name,
                                       CurrentNanosSource nanoSource,
                                       T[] allEnumValues,
                                       Supplier<MutableHistogram<Long>> histogramSupplier)
        Create a new instance.
        Parameters:
        name - The name to use to label this stopwatch in formatted output.
        nanoSource - A time source with nanoseconds precision.
        allEnumValues - All of the values of the enum that represents the stages of this MultistageStopwatch.
        histogramSupplier - A Supplier of empty MutableHistogram instances which will record duration values.
    • Method Detail

      • startTimer

        public StoppableTimer startTimer​(T timerType)
        Description copied from interface: MultistageStopwatch
        Start a timer tracking one instance of the specified activity.
        Parameters:
        timerType - The type of activity that this timer tracks. Example: BICYCLING.
        Returns:
        An instance of ActiveTimer that can be used to stop the timer when the activity is complete.
      • getName

        public String getName()
        Get the name of this stopwatch.
        Returns:
        The name of this stopwatch.
      • timingHistogramFor

        public Histogram<Long> timingHistogramFor​(T timerType)
        Get a copy of the histogram of duration values, for the stage identified by the timerType.
        Parameters:
        timerType - The histogram to return. (There is one histogram per stage, so if the enum used as the parameterized type T has four enum values, there will be four histograms.)
        Returns:
        The histogram.
      • equals

        public boolean equals​(Object other)
      • hashCode

        public int hashCode()
      • getTimerTypes

        public Iterable<T> getTimerTypes()
        Description copied from interface: MultistageStopwatch
        Get an Iterable that will provide all of the enum values that this timer handles.

        Typically this will be an Iterable containing all of the values of the enum, e.g. SomeEnum.values().

        Specified by:
        getTimerTypes in interface MultistageStopwatch<T extends Enum<?>>
        Returns:
        All of the timer types that this timer can track.
      • summarize

        public MultistageStopwatch.TimingSummary summarize​(T timerType)
        Description copied from interface: MultistageStopwatch
        Obtain a summary of the timer activity for a given stage of the activity described by the parameterized type T.
        Specified by:
        summarize in interface MultistageStopwatch<T extends Enum<?>>
        Parameters:
        timerType - Which stage's summary should be returned. (There is one summary per stage, so if the enum used as the parameterized type T has four enum values, there will be four summaries.) Example: BICYCLING.
        Returns:
        The summary of the specified stage's timer activity.