-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #432 from Chia-Network/jn.hash-benchmark
CHIA-622 Add benchmarks for sha256 operations
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,3 +58,7 @@ harness = false | |
[[bench]] | ||
name = "deserialize" | ||
harness = false | ||
|
||
[[bench]] | ||
name = "sha256_hash" | ||
harness = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |