Class Benchmarker

java.lang.Object
com.github.rlacher.sortbench.benchmark.Benchmarker

public class Benchmarker extends Object
Benchmarker class for profiling sorting algorithms.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Enum representing profiling modes for benchmarking.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for the Benchmarker class parameterised with a profiling mode.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the benchmark metric based on the profiling mode.
    void
    Measures the memory usage during the sorting process.
    void
    Reports an insert operation during the sorting process.
    void
    Reports a shift operation during the sorting process.
    void
    Reports a swap operation during the sorting process.
    void
    Reports a write operation during the sorting process.
    void
    reportWrites(int count)
    Reports write operations during the sorting process.
    void
    Resets the benchmarker to its initial state.
    void
    Initiates profiling, resetting any prior metric data, based on the selected profiling mode.
    void
    Stops the profiling process based on the selected profiling mode.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Benchmarker

      public Benchmarker(Benchmarker.ProfilingMode profilingMode)
      Constructor for the Benchmarker class parameterised with a profiling mode.
      Parameters:
      profilingMode - The profiling mode to be used.
      Throws:
      IllegalArgumentException - If the profiling mode is null.
  • Method Details

    • startProfiling

      public void startProfiling()
      Initiates profiling, resetting any prior metric data, based on the selected profiling mode.

      Must be followed by stopProfiling() to complete metric collection.

      Throws:
      IllegalStateException - If profiling is already active.
    • stopProfiling

      public void stopProfiling()
      Stops the profiling process based on the selected profiling mode.

      Must follow a startProfiling() call to collect metrics.

      Throws:
      IllegalStateException - If profiling is not in progress.
    • measureMemory

      public void measureMemory()
      Measures the memory usage during the sorting process.

      This method should only be called when memory profiling is active. The scope of this benchmark is limited to heap memory analysis. Stack memory usage is not measured for simplicity.

      Throws:
      IllegalStateException - If profiling is not currently active.
    • reportSwap

      public void reportSwap()
      Reports a swap operation during the sorting process.

      A swap refers to the exchange of two elements in the array being sorted (also known as invertion). This method increases the data write counter by 2.

    • reportShift

      public void reportShift()
      Reports a shift operation during the sorting process.

      A shift refers to movement of an element to the right within the array. This method increments the data write counter by 1.

    • reportInsert

      public void reportInsert()
      Reports an insert operation during the sorting process.

      An insert writes a specified value at a specified location within the array. This method increments the data write counter by 1.

    • reportWrite

      public void reportWrite()
      Reports a write operation during the sorting process.

      This method increments the data write counter by 1.

    • reportWrites

      public void reportWrites(int count)
      Reports write operations during the sorting process.

      This method increments the data write counter by the specified amount only when profiling is active and the profiling mode is set to DATA_WRITE_COUNT.

      Parameters:
      count - The number of write operations performed.
      Throws:
      IllegalArgumentException - If count is negative.
      IllegalStateException - If profiling is not currently active.
    • reset

      public void reset()
      Resets the benchmarker to its initial state.
      Throws:
      IllegalStateException - if profiling is currently active.
    • getMetric

      public BenchmarkMetric getMetric()
      Returns the benchmark metric based on the profiling mode.
      Returns:
      The benchmark iteration metric containing the profiling mode and the corresponding metric value.
      Throws:
      IllegalStateException - If the profiling is still in progress or the profiling mode is not set (should not happen).