From 4198e28c797ab79bbb6c4a9393ecca39d5e07bab Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Tue, 14 Jan 2025 12:02:58 +0000 Subject: [PATCH] benchmark: warmup & iterations --- .../powersort/benchmark/CbmBase.java | 52 ++++++++++-------- .../powersort/benchmark/CbmBench.java | 54 +++++++++++++++++++ .../powersort/benchmark/CbmCgl.java | 3 +- .../powersort/benchmark/CbmCompetition.java | 3 +- .../powersort/benchmark/Filter.java | 1 + 5 files changed, 89 insertions(+), 24 deletions(-) create mode 100644 app/src/main/java/de/uni_marburg/powersort/benchmark/CbmBench.java diff --git a/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmBase.java b/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmBase.java index 57172b4..08c202d 100644 --- a/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmBase.java +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmBase.java @@ -4,44 +4,52 @@ import de.uni_marburg.powersort.data.DataEnum; import de.uni_marburg.powersort.sort.SortEnum; import de.uni_marburg.powersort.data.ObjectSupplier; -import java.util.concurrent.TimeUnit; +import java.util.Arrays; /** * Custom Benchmark (CBM). */ -public class CbmBase { - static void run(SortEnum[] sortImplementations, DataEnum[] dataEnums) { +public class CbmBase implements Runnable { + private final SortEnum[] sortImplementations; + private final DataEnum[] dataEnums; + private final int maxDataEnumStrLen; + private final int maxSortStrLen; + + public CbmBase(SortEnum[] sortEnums, DataEnum[] dataEnums) { + this.sortImplementations = sortEnums; + this.dataEnums = dataEnums; + + maxDataEnumStrLen = Arrays.stream(dataEnums).map(e -> e.toString().length()).max(Integer::compareTo).orElse(0); + maxSortStrLen = Arrays.stream(sortEnums).map(e -> e.toString().length()).max(Integer::compareTo).orElse(0); + } + + @Override + public void run() { System.out.println(); + for (DataEnum dataEnum : dataEnums) { ObjectSupplier objectSupplier = null; for (SortEnum sortImplementation : sortImplementations) { if (!Filter.isFiltered(dataEnum, sortImplementation)) { if (objectSupplier == null) { - System.out.println("⏳ Creating object supplier " + dataEnum + " ⏳"); + System.out.println("Creating object supplier " + dataEnum); + System.out.println(); objectSupplier = dataEnum.getObjectSupplier(); } - benchmark(objectSupplier, sortImplementation); + + String dataEnumStr = dataEnum.toString(); + dataEnumStr += " ".repeat(maxDataEnumStrLen - dataEnumStr.length()); + String sortImplStr = sortImplementation.toString(); + sortImplStr += " ".repeat(maxSortStrLen - sortImplStr.length()); + + CbmBench cbmBench = new CbmBench(dataEnumStr, sortImplStr, objectSupplier, sortImplementation); + cbmBench.run(); + + System.out.println(); } } System.out.println(); } } - - static void benchmark(ObjectSupplier objectSupplier, SortEnum sortImplementation) { - System.out.print(sortImplementation); - System.out.flush(); - Object[] sortInput = objectSupplier.getCopy(); - - // TODO: JVM warmup! - final long startNanos = System.nanoTime(); - sortImplementation.getSortImpl().sort(sortInput); - final long stopNanos = System.nanoTime(); - - final long durNanos = stopNanos - startNanos; - final long durMillis = TimeUnit.NANOSECONDS.toMillis(durNanos); - final String durFormatted = LongFormatter.formatUnderscore(durMillis); - - System.out.println("," + durFormatted); - } } diff --git a/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmBench.java b/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmBench.java new file mode 100644 index 0000000..7009720 --- /dev/null +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmBench.java @@ -0,0 +1,54 @@ +package de.uni_marburg.powersort.benchmark; + +import de.uni_marburg.powersort.data.ObjectSupplier; +import de.uni_marburg.powersort.sort.SortEnum; + +import java.util.concurrent.TimeUnit; + +public class CbmBench implements Runnable{ + private final String dataEnumStr; + private final String sortImplStr; + private final ObjectSupplier objectSupplier; + private final SortEnum sortImplementation; + + public CbmBench(String dataEnumStr, String sortImplStr, ObjectSupplier objectSupplier, SortEnum sortImplementation) { + this.dataEnumStr = dataEnumStr; + this.sortImplStr = sortImplStr; + this.objectSupplier = objectSupplier; + this.sortImplementation = sortImplementation; + } + + @Override + public void run() { + // Warmup + for (int i = 0; i < 3; i++) { + benchmark(true); + } + // Measure + for (int i = 0; i < 3; i++) { + benchmark(false); + } + } + + private void benchmark(boolean warmup) { + System.out.print(dataEnumStr + "," + sortImplStr + ","); + if (warmup) { + System.out.print("WARMUP ,"); + } else { + System.out.print("MEASURE,"); + } + System.out.flush(); + + Object[] sortInput = objectSupplier.getCopy(); + + final long startNanos = System.nanoTime(); + sortImplementation.getSortImpl().sort(sortInput); + final long stopNanos = System.nanoTime(); + + final long durNanos = stopNanos - startNanos; + final long durMillis = TimeUnit.NANOSECONDS.toMillis(durNanos); + final String durFormatted = LongFormatter.formatUnderscore(durMillis); + + System.out.println(durFormatted); + } +} diff --git a/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmCgl.java b/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmCgl.java index bc8ec7a..7ff471e 100644 --- a/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmCgl.java +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmCgl.java @@ -5,9 +5,10 @@ import de.uni_marburg.powersort.sort.SortEnum; public class CbmCgl { public static void main(String[] args) { - CbmBase.run( + CbmBase cbmBase = new CbmBase( SortEnum.values(), CglEnum.values() ); + cbmBase.run(); } } diff --git a/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmCompetition.java b/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmCompetition.java index 03f4d6a..b31b91a 100644 --- a/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmCompetition.java +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/CbmCompetition.java @@ -5,9 +5,10 @@ import de.uni_marburg.powersort.sort.SortEnum; public class CbmCompetition { public static void main(String[] args) { - CbmBase.run( + CbmBase cbmBase = new CbmBase( SortEnum.values(), CompetitionEnum.values() ); + cbmBase.run(); } } diff --git a/app/src/main/java/de/uni_marburg/powersort/benchmark/Filter.java b/app/src/main/java/de/uni_marburg/powersort/benchmark/Filter.java index 9943b93..34ddc3e 100644 --- a/app/src/main/java/de/uni_marburg/powersort/benchmark/Filter.java +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/Filter.java @@ -128,6 +128,7 @@ public class Filter { return List.of( CglEnum.DESCENDING_INTEGERS, CglEnum.ASCENDING_RUNS, + CglEnum.ASCENDING_RUNS_WITH_OVERLAP, CompetitionEnum.COMPETITION_83, CompetitionEnum.COMPETITION_85,