Skip to content

Commit

Permalink
Add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Siro256 committed May 3, 2024
1 parent ce2ec48 commit fff3db4
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
15 changes: 15 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id("java")
`maven-publish`
signing
id("me.champeau.jmh") version "0.7.2"
}

group = "net.eewbot.base65536j"
Expand Down Expand Up @@ -55,6 +56,20 @@ tasks {
test {
useJUnitPlatform()
}

jmh {
jmhVersion = "1.37"

warmupIterations = 3
warmup = "5s"
iterations = 3
timeOnIteration = "5s"
fork = 5

forceGC = true
resultFormat = "JSON"
failOnError = true
}
}

publishing {
Expand Down
74 changes: 74 additions & 0 deletions src/jmh/java/net/eewbot/base65536j/Benchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package net.eewbot.base65536j;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;

import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
public class Benchmark {
private static final Base65536Encoder encoder = Base65536.getEncoder();
private static final Base65536Decoder decoder = Base65536.getDecoder();
private static byte[] oneByteArray;
private static byte[] tenKilobytesArray;
private static byte[] oneMegabytesArray;
private static String oneByteString;
private static String tenKilobytesString;
private static String oneMegabytesString;

@Setup
public void setup() {
oneByteArray = new byte[]{127};
tenKilobytesArray = new byte[10_000];
oneMegabytesArray = new byte[1_000_000];
for (int i = 0; i < 1_000_000; i++) {
tenKilobytesArray[i % 100] = (byte) (i & 0xff);
oneMegabytesArray[i] = (byte) (i & 0xff);
}
oneByteString = encoder.encodeToString(oneByteArray);
tenKilobytesString = encoder.encodeToString(tenKilobytesArray);
oneMegabytesString = encoder.encodeToString(oneMegabytesArray);
}

@org.openjdk.jmh.annotations.Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void encoderOneByte(Blackhole blackhole) {
blackhole.consume(encoder.encodeToString(oneByteArray));
}

@org.openjdk.jmh.annotations.Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void encoderTenKilobytes(Blackhole blackhole) {
blackhole.consume(encoder.encodeToString(tenKilobytesArray));
}

@org.openjdk.jmh.annotations.Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void encoderOneMegabytes(Blackhole blackhole) {
blackhole.consume(encoder.encodeToString(oneMegabytesArray));
}

@org.openjdk.jmh.annotations.Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void decoderOneByte(Blackhole blackhole) {
blackhole.consume(decoder.decode(oneByteString));
}

@org.openjdk.jmh.annotations.Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void decoderTenKilobytes(Blackhole blackhole) {
blackhole.consume(decoder.decode(tenKilobytesString));
}

@org.openjdk.jmh.annotations.Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void decoderOneMegabytes(Blackhole blackhole) {
blackhole.consume(decoder.decode(oneMegabytesString));
}
}

0 comments on commit fff3db4

Please sign in to comment.