Skip to content

Commit

Permalink
Simplify benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Apr 17, 2024
1 parent c0ead06 commit ba4dec6
Showing 1 changed file with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

@BenchmarkMode(Mode.Throughput)
@State(Scope.Benchmark)
// Realistic usage within Bazel will hash files on multiple threads.
@Threads(4)
public class BazelHashFunctionsBenchmark {

static {
Expand All @@ -36,33 +34,35 @@ public enum HashFunctionType {
}
}

public enum Size {
B,
KB,
MB,
GB;

final int bytes;

Size() {
bytes = 1 << (ordinal() * 10);
}
}

@Param({"BLAKE3", "SHA2_256"})
public HashFunctionType type;

@Param({"1", "16", "128", "512", "1024", "4096", "16384", "1048576"})
public int size;
@Param({"B", "KB", "MB", "GB"})
public Size size;

private byte[] data;

@Setup(Level.Iteration)
public void setup() {
data = new byte[size];
data = new byte[size.bytes];
new SecureRandom().nextBytes(data);
}

@Benchmark
public HashCode hashBytesOneShot() {
return type.hashFunction.hashBytes(data);
}

private static final int CHUNK_SIZE = 4096;

@Benchmark
public HashCode hashBytesChunks() {
Hasher hasher = type.hashFunction.newHasher();
for (int pos = 0; pos < data.length; pos += CHUNK_SIZE) {
hasher.putBytes(data, pos, Math.min(CHUNK_SIZE, data.length - pos));
}
return hasher.hash();
}
}

0 comments on commit ba4dec6

Please sign in to comment.