From 684f28bb2b01340884cc3ddbc6ad0f70d7604f0c Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Wed, 11 Dec 2024 16:08:49 +0000 Subject: [PATCH] benchmark: refactor --- .../powersort/benchmark/BenchmarkJmh.java | 56 +++++++++---------- .../uni_marburg/powersort/benchmark/Main.java | 2 +- .../powersort/data/IntegerSupplier.java | 4 +- .../powersort/data/ObjectSupplier.java | 2 +- 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/app/src/jmh/java/de/uni_marburg/powersort/benchmark/BenchmarkJmh.java b/app/src/jmh/java/de/uni_marburg/powersort/benchmark/BenchmarkJmh.java index d4ebd95..09b1685 100644 --- a/app/src/jmh/java/de/uni_marburg/powersort/benchmark/BenchmarkJmh.java +++ b/app/src/jmh/java/de/uni_marburg/powersort/benchmark/BenchmarkJmh.java @@ -9,45 +9,43 @@ import org.openjdk.jmh.annotations.*; import java.util.concurrent.TimeUnit; // TODO: The parameters are way too low. Use for debugging only! -@Fork(1) -@Warmup(iterations = 3) -@Measurement(iterations = 6) +/* + * Benchmark parameters + */ +@Fork(0) +@Warmup(iterations = 0) +@Measurement(iterations = 1) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +/* + * Benchmark state parameters + * + * Quote from JMH: + * State objects naturally encapsulate the state on which benchmark is working on. + */ +@State(Scope.Benchmark) public class BenchmarkJmh { - /** - * Quote from JMH: - * State objects naturally encapsulate the state on which benchmark is working on. - */ - @State(Scope.Benchmark) - public static class State1 { - RandomIntegers d = new RandomIntegers(); - Integer[] a; + private final RandomIntegers readonly = new RandomIntegers(); + /* package-protected */ Integer[] array; - // TODO: This is inaccurate. - // How to create and use separate arrays for each warmup x iteration x sortAlgorithm ? - @Setup(Level.Invocation) - public void setup() { - a = d.get(); - } + // 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(); } - @BenchmarkMode(Mode.AverageTime) - @OutputTimeUnit(TimeUnit.MILLISECONDS) @Benchmark - public void rand1DummySort(State1 s) { - DummySort.sort(s.a, 0, s.a.length, NaturalOrder.INSTANCE, null, 0, 0); + public void rand1DummySort() { + DummySort.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0); } - @BenchmarkMode(Mode.AverageTime) - @OutputTimeUnit(TimeUnit.MILLISECONDS) @Benchmark - public void rand1TimSort(State1 s) { - TimSort.sort(s.a, 0, s.a.length, NaturalOrder.INSTANCE, null, 0, 0); + public void rand1TimSort() { + TimSort.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0); } - @BenchmarkMode(Mode.AverageTime) - @OutputTimeUnit(TimeUnit.MILLISECONDS) @Benchmark - public void rand1MergeSort(State1 s) { - MergeSort.legacyMergeSort(s.a, NaturalOrder.INSTANCE); + public void rand1MergeSort() { + MergeSort.legacyMergeSort(array, NaturalOrder.INSTANCE); } } diff --git a/app/src/main/java/de/uni_marburg/powersort/benchmark/Main.java b/app/src/main/java/de/uni_marburg/powersort/benchmark/Main.java index b4a5c48..59d8848 100644 --- a/app/src/main/java/de/uni_marburg/powersort/benchmark/Main.java +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/Main.java @@ -25,7 +25,7 @@ public class Main { System.out.println("\n" + objectSupplier.title()); for (SortImpl sortImplementation : sortImplementations) { - Object[] sortInput = objectSupplier.get(); + Object[] sortInput = objectSupplier.getCopy(); final long startNanos = System.nanoTime(); sortImplementation.sort(sortInput); diff --git a/app/src/main/java/de/uni_marburg/powersort/data/IntegerSupplier.java b/app/src/main/java/de/uni_marburg/powersort/data/IntegerSupplier.java index 3e0f392..f1727d7 100644 --- a/app/src/main/java/de/uni_marburg/powersort/data/IntegerSupplier.java +++ b/app/src/main/java/de/uni_marburg/powersort/data/IntegerSupplier.java @@ -6,7 +6,7 @@ public abstract class IntegerSupplier extends ObjectSupplier { } @Override - public Integer[] get() { - return (Integer[]) super.get(); + public Integer[] getCopy() { + return (Integer[]) super.getCopy(); } } diff --git a/app/src/main/java/de/uni_marburg/powersort/data/ObjectSupplier.java b/app/src/main/java/de/uni_marburg/powersort/data/ObjectSupplier.java index 6ddd7b7..f191abc 100644 --- a/app/src/main/java/de/uni_marburg/powersort/data/ObjectSupplier.java +++ b/app/src/main/java/de/uni_marburg/powersort/data/ObjectSupplier.java @@ -17,7 +17,7 @@ public abstract class ObjectSupplier { /** * @return A fresh copy of the array of objects represented by this object. */ - public Object[] get(){ + public Object[] getCopy(){ return Arrays.copyOf(readOnly, readOnly.length); } }