benchmark: cleanup

This commit is contained in:
Daniel Langbein 2025-01-07 13:09:21 +00:00
parent 54f3ea6d42
commit 4223f76727
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
4 changed files with 25 additions and 30 deletions

View File

@ -18,13 +18,11 @@ import org.openjdk.jmh.annotations.Warmup;
import java.util.concurrent.TimeUnit;
import static de.uni_marburg.powersort.data.ObjectSupplier.EMPTY_ARRAY;
// TODO: The parameters are way too low. Use for debugging only!
/*
* Benchmark parameters
*/
@Fork(1)
@Fork(value = 1, jvmArgsAppend = "-Xmx8g")
@Warmup(iterations = 1)
@Measurement(iterations = 6)
@BenchmarkMode(Mode.AverageTime)
@ -42,7 +40,7 @@ public class MainJmh {
@Param()
private SortEnum sortEnum;
private ObjectSupplier data = EMPTY_ARRAY;
private ObjectSupplier data = null;
/* package-protected */ Object[] workingCopy;
// TODO: This is inaccurate. How to create and use separate arrays for each warmup x iteration x sortAlgorithm ?
@ -50,15 +48,18 @@ public class MainJmh {
public void setup() {
if(Filter.isFiltered(dataEnum, sortEnum)) {
// This combination of DataEnum and SortEnum should be skipped.
// We can't tell JMH to not run the benchmark at all,
// so we'll let it sort an empty list instead ;)
data = EMPTY_ARRAY;
// We can't tell JMH to not run the benchmark at all.
// Instead, we let it sort an invalid list which results in an exception.
// This way the filtered combinations are skipped quickly
// and not included in the benchmark results.
data = null;
workingCopy = null;
return;
}
// A new MainJmh object is created for each @Param variation.
// Then, `data` is `null` again.
if (data == EMPTY_ARRAY) {
if (data == null) {
data = dataEnum.getObjectSupplier();
}
// For all warmup and measurement iterations of one @Param variation, the MainJmh object is reused.

View File

@ -24,7 +24,12 @@ public class Filter {
// return true;
// }
// Skip some sort algorithms for all competition input data.
// To skip all competition inputs, uncomment the next line.
// if(d.isCompetitionInput()){
// return true;
// }
// Skip some sort algorithms for all competition inputs.
if(d.isCompetitionInput()){
if(List.of(
SortEnum.BUBBLE_SORT,

View File

@ -4,7 +4,6 @@ import de.uni_marburg.powersort.data.DataEnum;
import de.uni_marburg.powersort.sort.SortEnum;
import de.uni_marburg.powersort.data.ObjectSupplier;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;
/**
@ -12,15 +11,15 @@ import java.util.concurrent.TimeUnit;
*/
public class Main {
public static void main(final String[] args) {
final EnumSet<SortEnum> sortImplementations = getSortImplementations();
final EnumSet<DataEnum> dataEnums = getSortInputSuppliers();
final SortEnum[] sortImplementations = SortEnum.values();
final DataEnum[] dataEnums = DataEnum.values();
System.out.println();
for (DataEnum dataEnum : dataEnums) {
ObjectSupplier objectSupplier = null;
for (SortEnum sortImplementation : sortImplementations) {
if(!Filter.isFiltered(dataEnum, sortImplementation)) {
if(objectSupplier == null){
if (!Filter.isFiltered(dataEnum, sortImplementation)) {
if (objectSupplier == null) {
System.out.println("⏳ Creating object supplier " + dataEnum + "");
objectSupplier = dataEnum.getObjectSupplier();
}
@ -32,7 +31,7 @@ public class Main {
}
}
static void benchmark(ObjectSupplier objectSupplier, SortEnum sortImplementation){
static void benchmark(ObjectSupplier objectSupplier, SortEnum sortImplementation) {
System.out.print(sortImplementation);
System.out.flush();
Object[] sortInput = objectSupplier.getCopy();
@ -48,17 +47,4 @@ public class Main {
System.out.println("," + durFormatted);
}
static EnumSet<SortEnum> getSortImplementations() {
return EnumSet.allOf(SortEnum.class);
}
/**
* The returned ObjectSupplier objects are wrapped by DataEnum objects.
* This way they are lazily created on their first access with DataEnum.get().
* This saves memory if we work with large lists.
*/
static EnumSet<DataEnum> getSortInputSuppliers() {
return EnumSet.allOf(DataEnum.class);
}
}

View File

@ -3,8 +3,6 @@ package de.uni_marburg.powersort.data;
import java.util.Arrays;
public abstract class ObjectSupplier {
public static final ObjectSupplier EMPTY_ARRAY = new ObjectSupplier(new Object[0]) {};
/* package-protected */ final Object[] readOnly;
ObjectSupplier(Object[] readOnly) {
@ -17,4 +15,9 @@ public abstract class ObjectSupplier {
public Object[] getCopy(){
return Arrays.copyOf(readOnly, readOnly.length);
}
@Override
public String toString() {
return "ObjectSupplier{" + readOnly.length + "}";
}
}