Skip to content

Commit

Permalink
add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
jack60612 committed Jul 10, 2024
1 parent 4460b48 commit 72e9eea
Show file tree
Hide file tree
Showing 2 changed files with 44 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
40 changes: 40 additions & 0 deletions benches/sha256_hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use criterion::{Criterion, criterion_group, criterion_main};

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: Vec<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); // purposely not borrowing bytes, as no longer needed.
}
}
})
});
// create
group.finish();
}

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

0 comments on commit 72e9eea

Please sign in to comment.