Skip to content

Commit

Permalink
Merge pull request #432 from Chia-Network/jn.hash-benchmark
Browse files Browse the repository at this point in the history
CHIA-622 Add benchmarks for sha256 operations
  • Loading branch information
jack60612 authored Jul 11, 2024
2 parents 4460b48 + 1a609a6 commit a356a9a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ harness = false
[[bench]]
name = "deserialize"
harness = false

[[bench]]
name = "sha256_hash"
harness = false
42 changes: 42 additions & 0 deletions benches/sha256_hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use criterion::{criterion_group, criterion_main, Criterion};

use clvmr::sha2::Sha256;

const BYTE_LENGTHS: [u8; 6] = [8, 16, 32, 64, 96, 128];
const MAX_VAL: u8 = 250;

fn gen_bytes(value: u8, amount: u8) -> Vec<u8> {
let mut bytes = Vec::new();
for _ in 0..amount {
bytes.push(value);
}
bytes
}

fn hash_bytes(bytes: &[u8]) -> [u8; 32] {
let mut sha256 = Sha256::new();
sha256.update(bytes);
sha256.finalize()
}

fn sha256_hash_benchmark(c: &mut Criterion) {
// setup benchmark
let mut group = c.benchmark_group("sha256_hash");

group.bench_function("hash_benchmark", |b| {
b.iter(|| {
// this figures out how many iterations to run.
for val in 0..MAX_VAL {
for len in BYTE_LENGTHS {
let bytes = gen_bytes(val, len);
hash_bytes(&bytes);
}
}
})
});
// create
group.finish();
}

criterion_group!(sha256_hash, sha256_hash_benchmark);
criterion_main!(sha256_hash);

0 comments on commit a356a9a

Please sign in to comment.