Skip to content

Commit

Permalink
chore(jmh): add bench for digests using reader
Browse files Browse the repository at this point in the history
  • Loading branch information
guicamest committed Nov 26, 2023
1 parent 7a72a37 commit 0c8c303
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
Expand Down Expand Up @@ -48,4 +53,27 @@ public void digestBlake(Blackhole bh) {
blake3.update(bytes);
bh.consume(blake3.doFinalize(outBytes));
}

@Benchmark
public void digestMD5_inc(Blackhole bh) throws IOException {
forEachBufferIn(bytes, (buffer, len) -> digestInstance.update(bytes, 0, len));
bh.consume(digestInstance.digest());
}

@Benchmark
public void digestBlake_inc(Blackhole bh) throws IOException {
forEachBufferIn(bytes, (buffer, len) -> blake3.update(bytes, 0, len));
bh.consume(blake3.doFinalize(outBytes));
}

private void forEachBufferIn(byte[] bytes, BiConsumer<byte[], Integer> consumer) throws IOException {
try(var reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes)))){
final int TRANSFER_BUFFER_SIZE = 8192;
char[] buffer = new char[TRANSFER_BUFFER_SIZE];
int nRead;
while ((nRead = reader.read(buffer, 0, TRANSFER_BUFFER_SIZE)) >= 0) {
consumer.accept(bytes, nRead);
}
}
}
}

0 comments on commit 0c8c303

Please sign in to comment.