benchmark: @Param & DataEnum

This commit is contained in:
Daniel Langbein 2024-12-11 17:08:01 +00:00
parent 605d0c2781
commit 904d462fc6
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
8 changed files with 100 additions and 34 deletions

View File

@ -1,7 +1,19 @@
package de.uni_marburg.powersort.benchmark;
import de.uni_marburg.powersort.data.RandomIntegers;
import org.openjdk.jmh.annotations.*;
import de.uni_marburg.powersort.data.DataEnum;
import de.uni_marburg.powersort.data.ObjectSupplier;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import java.util.concurrent.TimeUnit;
@ -22,13 +34,23 @@ import java.util.concurrent.TimeUnit;
*/
@State(Scope.Benchmark)
public abstract class AbstractBenchmark {
private final RandomIntegers readonly = new RandomIntegers();
/* package-protected */ Integer[] array;
@Param()
private DataEnum dataEnum;
private DataEnum prevDataEnum = null;
private ObjectSupplier data;
/* package-protected */ Object[] workingCopy;
// TODO: This is inaccurate. How to create and use separate arrays for each warmup x iteration x sortAlgorithm ?
@Setup(Level.Invocation)
public void setup() {
array = readonly.getCopy();
if (dataEnum != prevDataEnum){
System.out.println(prevDataEnum + " => " + dataEnum);
prevDataEnum = dataEnum;
data = dataEnum.get();
}
workingCopy = data.getCopy();
}
@Benchmark

View File

@ -5,6 +5,6 @@ import de.uni_marburg.powersort.sort.DummySort;
public class DummySortBenchmark extends AbstractBenchmark {
@Override
public void sort() {
DummySort.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0);
DummySort.sort(workingCopy, 0, workingCopy.length, NaturalOrder.INSTANCE, null, 0, 0);
}
}

View File

@ -5,6 +5,6 @@ import de.uni_marburg.powersort.sort.MergeSort;
public class MergeSortBenchmark extends AbstractBenchmark {
@Override
public void sort() {
MergeSort.legacyMergeSort(array, NaturalOrder.INSTANCE);
MergeSort.legacyMergeSort(workingCopy, NaturalOrder.INSTANCE);
}
}

View File

@ -17,19 +17,35 @@ public class IntegerArray {
}
/**
* @return [start, start-1, ..., end+1, end]
* @return [high, high-1, ..., low+1, low]
*/
public static Integer[] descending(final int start, final int end) {
assert start > end;
public static Integer[] descending(final int high, final int low) {
assert high > low;
Integer[] list = new Integer[start - end + 1];
Integer[] list = new Integer[high - low + 1];
for (int i = 0; i < list.length; i++) {
int value = start - i;
int value = high - i;
list[i] = value;
}
assert list[0] == start;
assert list[list.length - 1] == end;
assert list[0] == high;
assert list[list.length - 1] == low;
return list;
}
/**
* @return [low, low-1, ..., high+1, high]
*/
public static Integer[] ascending(final int low, final int high) {
assert low < high;
Integer[] list = new Integer[low - high + 1];
for (int i = 0; i < list.length; i++) {
int value = low + i;
list[i] = value;
}
assert list[0] == low;
assert list[list.length - 1] == high;
return list;
}
}

View File

@ -1,16 +1,13 @@
package de.uni_marburg.powersort.benchmark;
import de.uni_marburg.powersort.data.DataEnum;
import de.uni_marburg.powersort.sort.DummySort;
import de.uni_marburg.powersort.sort.MergeSort;
import de.uni_marburg.powersort.sort.TimSort;
import de.uni_marburg.powersort.data.DescendingIntegers;
import de.uni_marburg.powersort.data.RandomIntegers;
import de.uni_marburg.powersort.data.ObjectSupplier;
import java.util.Arrays;
import java.util.List;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
/**
* Custom benchmark.
@ -18,10 +15,10 @@ import java.util.function.Supplier;
public class Main {
public static void main(final String[] args) {
final SortImpl[] sortImplementations = getSortImplementations();
final List<Supplier<ObjectSupplier>> sortInputSuppliers = getSortInputSuppliers();
final EnumSet<DataEnum> dataEnums = getSortInputSuppliers();
for (Supplier<ObjectSupplier> sortInputSupplier : sortInputSuppliers) {
ObjectSupplier objectSupplier = sortInputSupplier.get();
for (DataEnum dataEnum : dataEnums) {
ObjectSupplier objectSupplier = dataEnum.get();
System.out.println("\n" + objectSupplier.title());
for (SortImpl sortImplementation : sortImplementations) {
@ -53,6 +50,12 @@ public class Main {
TimSort.sort(a, 0, a.length, NaturalOrder.INSTANCE, null, 0, 0);
}
},
// new SortImpl("FinnSort") {
// @Override
// public void sort(Object[] a) {
// FinnSort.sort(a);
// }
// },
new SortImpl("MergeSort") {
@Override
public void sort(Object[] a) {
@ -63,18 +66,11 @@ public class Main {
}
/**
* The returned ObjectSupplier objects are wrapped by Supplier objects.
* This way they are lazily created on their first access.
* 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 List<Supplier<ObjectSupplier>> getSortInputSuppliers() {
return Arrays.asList(
// Three different random lists.
RandomIntegers::new,
RandomIntegers::new,
RandomIntegers::new,
// One descending list.
DescendingIntegers::new
);
static EnumSet<DataEnum> getSortInputSuppliers() {
return EnumSet.allOf(DataEnum.class);
}
}

View File

@ -0,0 +1,17 @@
package de.uni_marburg.powersort.data;
import de.uni_marburg.powersort.benchmark.IntegerArray;
import de.uni_marburg.powersort.benchmark.LongFormatter;
public class AscendingIntegers extends IntegerSupplier {
private static final int SIZE = 50_000_000;
public AscendingIntegers() {
super(IntegerArray.ascending(SIZE, 0));
}
@Override
public String title() {
return "Array of " + LongFormatter.formatUnderscore(SIZE) + " ascending Integer objects.";
}
}

View File

@ -0,0 +1,15 @@
package de.uni_marburg.powersort.data;
public enum DataEnum {
RANDOM_INTEGERS,
ASCENDING_INTEGERS,
DESCENDING_INTEGERS;
public ObjectSupplier get() {
return switch (this) {
case RANDOM_INTEGERS -> new RandomIntegers();
case ASCENDING_INTEGERS -> new AscendingIntegers();
case DESCENDING_INTEGERS -> new DescendingIntegers();
};
}
}

View File

@ -3,7 +3,7 @@ package de.uni_marburg.powersort.data;
import java.util.Arrays;
public abstract class ObjectSupplier {
final Object[] readOnly;
private final Object[] readOnly;
ObjectSupplier(Object[] readOnly) {
this.readOnly = readOnly;