Skip to content

Commit

Permalink
Merge pull request #132 from Samyssmile/benchmark
Browse files Browse the repository at this point in the history
test(): new benchmark added
  • Loading branch information
Samyssmile authored Nov 20, 2023
2 parents 2502a0a + b3c73be commit 5d72ba7
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 3 deletions.
1 change: 0 additions & 1 deletion benchmark-data/placeholder.txt

This file was deleted.

1 change: 1 addition & 0 deletions benchmark-data/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Put the augmentation-benchmark-images folder here.
Empty file added lib/benchmark-results.json
Empty file.
6 changes: 6 additions & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ configurations.all {
}
}

jmh {
resultFormat = 'JSON'
resultsFile = file("benchmark-results.json")
}

dependencies {
String osString = getOsString()
String archString = getArchString()
Expand Down Expand Up @@ -74,6 +79,7 @@ task javadocJar(type: Jar) {
from javadoc.destinationDir
}


publishing {
publications {
mavenJava(MavenPublication) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,57 @@
package de.edux.benchmark.augmentation.io;

import de.edux.augmentation.core.AugmentationBuilder;
import de.edux.augmentation.core.AugmentationSequence;
import de.edux.augmentation.effects.*;
import de.edux.augmentation.effects.geomentry.Perspective;
import de.edux.augmentation.io.AugmentationImageReader;
import de.edux.augmentation.io.IAugmentationImageReader;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.profile.GCProfiler;
import org.openjdk.jmh.profile.MemPoolProfiler;
import org.openjdk.jmh.profile.StackProfiler;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.SECONDS)
@Timeout(time = 25, timeUnit = TimeUnit.SECONDS)
@Warmup(iterations = 1)
@Measurement(iterations = 1)
public class AugmentatioIOBenchmark {

IAugmentationImageReader reader;

public AugmentatioIOBenchmark() {}

public static void main(String[] args) throws Exception {
runBenchmark(ResultFormatType.JSON, "benchmark-augmentation-results.json");
}

private static void runBenchmark(ResultFormatType formatType, String fileName) throws Exception {
Options opt =
new OptionsBuilder()
.include(AugmentatioIOBenchmark.class.getSimpleName())
.resultFormat(formatType)
.result("benchmark-data/results/" + fileName)
.addProfiler(GCProfiler.class)
.addProfiler(MemPoolProfiler.class)
.addProfiler(StackProfiler.class)
.build();

org.openjdk.jmh.Main.main(args);
new Runner(opt).run();
}

@Benchmark
@Fork(value = 1, warmups = 1)
@BenchmarkMode({Mode.All})
@Measurement(iterations = 1)
public void readBatchOfImages() throws Exception {
Path benchmarkDataDir =
Paths.get(
Expand All @@ -38,4 +64,42 @@ public void readBatchOfImages() throws Exception {

var imageStream = reader.readImagePathsAsStream(benchmarkDataDir.toString());
}

@Benchmark
@Fork(value = 1, warmups = 1)
@BenchmarkMode({Mode.All})
public void imageAugmentation() throws Exception {
Path benchmarkDataDir =
Paths.get(
".."
+ File.separator
+ "benchmark-data"
+ File.separator
+ "augmentation-benchmark-images");

Path benchmarkOutputDir =
Paths.get(
".."
+ File.separator
+ "benchmark-data"
+ File.separator
+ "augmentation-benchmark-output");

AugmentationSequence augmentationSequence =
new AugmentationBuilder()
.addAugmentation(new ResizeAugmentation(256, 256, ResizeQuality.QUALITY))
.addAugmentation(new ContrastAugmentation(0.6f))
.addAugmentation(new BlurAugmentation(5))
.addAugmentation(new NoiseInjectionAugmentation(40))
.addAugmentation(new MonochromeAugmentation())
.addAugmentation(new BrightnessAugmentation(0.5))
.addAugmentation(new FlippingAugmentation())
.addAugmentation(new ColorEqualizationAugmentation())
.addAugmentation(new CroppingAugmentation(0.8f))
.addAugmentation(new ElasticTransformationAugmentation(5, 0.5))
.addAugmentation(new PerspectiveTransformationsAugmentation(Perspective.RIGHT_TILT))
.addAugmentation(new RandomDeleteAugmentation(5, 10, 10))
.build()
.run(benchmarkDataDir.toString(), 4, benchmarkOutputDir.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package de.edux.benchmark.augmentation.io;

import de.edux.functions.imputation.MedianImputation;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.profile.GCProfiler;
import org.openjdk.jmh.profile.MemPoolProfiler;
import org.openjdk.jmh.profile.StackProfiler;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.SECONDS)
@Timeout(time = 25, timeUnit = TimeUnit.SECONDS)
@Warmup(iterations = 1)
@Measurement(iterations = 2)
public class MedianImputationBenchmark {
public static void main(String[] args) throws Exception {
runBenchmark(ResultFormatType.JSON, "benchmark-median-imputation-results.json");
}

private static void runBenchmark(ResultFormatType formatType, String fileName) throws Exception {
Options opt =
new OptionsBuilder()
.include(AugmentatioIOBenchmark.class.getSimpleName())
.resultFormat(formatType)
.result("benchmark-data/results/" + fileName)
.addProfiler(GCProfiler.class)
.addProfiler(MemPoolProfiler.class)
.addProfiler(StackProfiler.class)
.build();

new Runner(opt).run();
}

@Benchmark
@Fork(value = 1, warmups = 1)
@BenchmarkMode({Mode.All})
public void readBatchOfImages() throws Exception {

String[] largeDataset = new String[1000000];
Random random = new Random();
for (int i = 0; i < largeDataset.length; i++) {
if (random.nextDouble() < 0.05) { // 5% empty values
largeDataset[i] = "";
} else {
largeDataset[i] = String.valueOf(random.nextDouble() * 1000000);
}
}

// Erwarteter Median
double[] numericValues =
Arrays.stream(largeDataset)
.filter(s -> !s.isBlank())
.mapToDouble(Double::parseDouble)
.sorted()
.toArray();
double expectedMedian =
numericValues.length % 2 == 0
? (numericValues[numericValues.length / 2]
+ numericValues[numericValues.length / 2 - 1])
/ 2.0
: numericValues[numericValues.length / 2];

MedianImputation medianImputation = new MedianImputation();

long startTime = System.nanoTime();
double calculatedMedian = medianImputation.calculateMedian(largeDataset);
long endTime = System.nanoTime();

System.out.println("Process time in seconds: " + (endTime - startTime) / 1e9);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,5 @@ private void processImage(BufferedImage image, String fileName) throws IOExcepti
}

ImageIO.write(image, "png", outputFile);
System.out.println("Bild gespeichert: " + outputFile.getAbsolutePath());
}
}

0 comments on commit 5d72ba7

Please sign in to comment.