benchmark: warmup & iterations

This commit is contained in:
Daniel Langbein 2025-01-14 12:02:58 +00:00
parent 82c58e403c
commit 4198e28c79
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
5 changed files with 89 additions and 24 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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,