From bcf521a3a34ed36519179cef6352565368846479 Mon Sep 17 00:00:00 2001 From: freya02 <41875020+freya022@users.noreply.github.com> Date: Thu, 9 Nov 2023 16:29:02 +0100 Subject: [PATCH] Arrange benchmarks to avoid using unnecessary parameters --- lib/build.gradle.kts | 2 +- .../benchmark/EmojiManagerAliasBenchmark.java | 22 +++++++++++++++ .../java/benchmark/EmojiManagerBenchmark.java | 17 +++--------- .../excluded/CodePointBenchmark.java | 27 +++++++++++++++++++ 4 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 lib/src/jmh/java/benchmark/EmojiManagerAliasBenchmark.java create mode 100644 lib/src/jmh/java/benchmark/excluded/CodePointBenchmark.java diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts index 0ad8d65..2e3d343 100644 --- a/lib/build.gradle.kts +++ b/lib/build.gradle.kts @@ -92,7 +92,7 @@ testing { jmh { // includes.addAll("some regular expression") // include pattern (regular expression) for benchmarks to be executed -// excludes.addAll("some regular expression") // exclude pattern (regular expression) for benchmarks to be executed + excludes.addAll("excluded") // exclude pattern (regular expression) for benchmarks to be executed // iterations.set(10) // Number of measurement iterations to do. benchmarkMode.addAll("avgt") // Benchmark mode. Available modes are: [Throughput/thrpt, AverageTime/avgt, SampleTime/sample, SingleShotTime/ss, All/all] // batchSize.set(1) // Batch size: number of benchmark method calls per operation. (some benchmark modes can ignore this setting) diff --git a/lib/src/jmh/java/benchmark/EmojiManagerAliasBenchmark.java b/lib/src/jmh/java/benchmark/EmojiManagerAliasBenchmark.java new file mode 100644 index 0000000..1229287 --- /dev/null +++ b/lib/src/jmh/java/benchmark/EmojiManagerAliasBenchmark.java @@ -0,0 +1,22 @@ +package benchmark; + +import net.fellbaum.jemoji.Emoji; +import net.fellbaum.jemoji.EmojiManager; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; + +import java.util.Optional; + +@State(Scope.Benchmark) +public class EmojiManagerAliasBenchmark { + + @Param({":+1:", "nope"}) + private String alias; + + @Benchmark + public Optional getByAlias() { + return EmojiManager.getByAlias(alias); + } +} diff --git a/lib/src/jmh/java/benchmark/EmojiManagerBenchmark.java b/lib/src/jmh/java/benchmark/EmojiManagerBenchmark.java index f044e12..c754502 100644 --- a/lib/src/jmh/java/benchmark/EmojiManagerBenchmark.java +++ b/lib/src/jmh/java/benchmark/EmojiManagerBenchmark.java @@ -4,17 +4,16 @@ import net.fellbaum.jemoji.EmojiManager; import net.fellbaum.jemoji.EmojiManagerTest; import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.Param; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.State; import java.io.BufferedReader; import java.io.InputStreamReader; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; import java.util.stream.Collector; import java.util.stream.Collectors; -@State(Scope.Benchmark) public class EmojiManagerBenchmark { private static final String TEXT = new BufferedReader(new InputStreamReader(Objects.requireNonNull(EmojiManagerBenchmark.class.getClassLoader().getResourceAsStream("ExampleTextFileWithEmojis.txt")))) @@ -42,14 +41,6 @@ public static void main(String[] args) throws Exception { private static final String EMOJIS_RANDOM_ORDER = String.join("", EmojiManager.getAllEmojisLengthDescending().stream().map(Emoji::getEmoji).collect(toShuffledList())); - @Param({":+1:", "nope"}) - private String alias; - - @Benchmark - public Optional getByAlias() { - return EmojiManager.getByAlias(alias); - } - @Benchmark //@BenchmarkMode(Mode.AverageTime) //@Warmup(iterations = 1) diff --git a/lib/src/jmh/java/benchmark/excluded/CodePointBenchmark.java b/lib/src/jmh/java/benchmark/excluded/CodePointBenchmark.java new file mode 100644 index 0000000..93eb868 --- /dev/null +++ b/lib/src/jmh/java/benchmark/excluded/CodePointBenchmark.java @@ -0,0 +1,27 @@ +package benchmark.excluded; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; + +@State(Scope.Benchmark) +public class CodePointBenchmark { + @Param("\uD83D\uDC4D\uD83C\uDFFC") + private String alias; + + @Benchmark + public int codePointArrayLength() { + return alias.codePoints().toArray().length; + } + + @Benchmark + public long codePointStreamCount() { + return alias.codePoints().count(); + } + + @Benchmark + public int codePointCount() { + return alias.codePointCount(0, alias.length()); + } +}