Class BenchmarkRunner
java.lang.Object
com.github.rlacher.sortbench.benchmark.BenchmarkRunner
Executes and benchmarks sorting algorithms based on a provided configuration and returns raw benchmark results.
Each benchmark run focuses on a single profiling mode, though it may involve multiple sorting strategies, data sizes, and data types.
-
Constructor Summary
ConstructorsConstructorDescriptionBenchmarkRunner(Sorter sorter) Constructs aBenchmarkRunnerthat delegates sorting to the providedSorter. -
Method Summary
Modifier and TypeMethodDescriptionprotected Map<BenchmarkContext, List<BenchmarkData>> generateBenchmarkData(Set<String> strategyNames, Set<BenchmarkData.DataType> dataTypes, List<Integer> sizes, int iterations) Generates benchmark data grouped by context to facilitate batch processing.protected SortStrategygetStrategyInstance(Class<? extends SortStrategy> strategyClass, Benchmarker.ProfilingMode mode) Creates an instance of aSortStrategybased on the provided strategy class and profiling mode.run(Map<String, Class<? extends SortStrategy>> selectedStrategies, Set<BenchmarkData.DataType> dataTypes, List<Integer> inputSizes, int iterations, Benchmarker.ProfilingMode profilingMode) Runs benchmarks based on the provided parameters.protected List<BenchmarkResult> runIterations(Map<String, SortStrategy> strategies, Map<BenchmarkContext, List<BenchmarkData>> benchmarkDataMap) Runs the provided sorting strategies on the given benchmark data using the sorter.
-
Constructor Details
-
BenchmarkRunner
Constructs aBenchmarkRunnerthat delegates sorting to the providedSorter.- Parameters:
sorter- TheSorterinstance to use.- Throws:
IllegalArgumentException- Ifsorterisnull.
-
-
Method Details
-
run
public List<BenchmarkResult> run(Map<String, Class<? extends SortStrategy>> selectedStrategies, Set<BenchmarkData.DataType> dataTypes, List<Integer> inputSizes, int iterations, Benchmarker.ProfilingMode profilingMode) Runs benchmarks based on the provided parameters.- Parameters:
selectedStrategies- A mapping from strategy names (e.g., "InsertionSort") to their respectiveSortStrategyclass types for dynamic instantiation.dataTypes- A set of data types for which to generate benchmark data.inputSizes- A list of input sizes for the benchmark data.iterations- The number of iterations for each data arrangement.profilingMode- The profiling mode to use for the benchmark.- Returns:
- A list of benchmark results with size equal or greater than 1.
- Throws:
IllegalArgumentException- If any of the input parameters arenullor invalid:selectedStrategiesisnull, empty, containsnullkeys or values, or duplicate keys (ignoring case) or values.dataTypesisnullor empty.inputSizesisnullor empty or contains non-positive integers.iterationsis not positive.profilingModeisnull.
-
runIterations
protected List<BenchmarkResult> runIterations(Map<String, SortStrategy> strategies, Map<BenchmarkContext, List<BenchmarkData>> benchmarkDataMap) Runs the provided sorting strategies on the given benchmark data using the sorter.- Parameters:
strategies- A map of sorting strategy names to instances.benchmarkDataMap- The map ofBenchmarkContextto lists of benchmark data.- Returns:
- A list of benchmark results.
- Throws:
IllegalArgumentException- If thestrategiesorbenchmarkDataMapisnullor empty.IllegalStateException- If a required sorting strategy is not found for a benchmark context.
-
generateBenchmarkData
protected Map<BenchmarkContext,List<BenchmarkData>> generateBenchmarkData(Set<String> strategyNames, Set<BenchmarkData.DataType> dataTypes, List<Integer> sizes, int iterations) Generates benchmark data grouped by context to facilitate batch processing.This improves JVM optimisation by enhancing data locality. Deep copies data for each strategy to ensure result comparability.
- Parameters:
strategyNames- A set of sorting strategy names.dataTypes- A set of data types for which to generate data.sizes- A list of integer data sizes for which to generate data.iterations- The number of data arrangements per size and data type.- Returns:
- A map of
BenchmarkContextto a list of benchmark data. The map may be empty if any of the input parameters are empty. - Throws:
NullPointerException- If any ofdataTypes,sizesorstrategyNamesarenull. This is unexpected as the callerrun(java.util.Map<java.lang.String, java.lang.Class<? extends com.github.rlacher.sortbench.strategies.SortStrategy>>, java.util.Set<com.github.rlacher.sortbench.benchmark.data.BenchmarkData.DataType>, java.util.List<java.lang.Integer>, int, com.github.rlacher.sortbench.benchmark.Benchmarker.ProfilingMode)ensures these arguments are notnull.
-
getStrategyInstance
protected SortStrategy getStrategyInstance(Class<? extends SortStrategy> strategyClass, Benchmarker.ProfilingMode mode) Creates an instance of aSortStrategybased on the provided strategy class and profiling mode.- Parameters:
strategyClass- The class type of the sorting strategy.mode- The profiling mode to use.- Returns:
- An
SortStrategyinstance of typestrategyClassconfigured with the provided profiling mode. - Throws:
IllegalArgumentException- IfstrategyClassisnull.IllegalStateException- If instantiation fails for theSortStrategy.
-