From 08d4a8d002ccbec982e24d5e0b698bbd1a19537d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Anda=20Estensen?= Date: Wed, 20 Nov 2024 14:32:15 +0100 Subject: [PATCH] feat: add hash_tree_root() bench --- ssz-rs/Cargo.toml | 5 +++++ ssz-rs/benches/merkleization.rs | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 ssz-rs/benches/merkleization.rs diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index a8fea4d4..b14199f1 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -32,6 +32,11 @@ snap = "1.0" project-root = "0.2.2" serde_json = "1.0.81" hex = "0.4.3" +criterion = { version = "0.5", features = ["html_reports"] } [build-dependencies] sha2 = "0.9.8" + +[[bench]] +name = "merkleization" +harness = false diff --git a/ssz-rs/benches/merkleization.rs b/ssz-rs/benches/merkleization.rs new file mode 100644 index 00000000..b7086265 --- /dev/null +++ b/ssz-rs/benches/merkleization.rs @@ -0,0 +1,25 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use std::hint::black_box; + +fn bench_merkleization(c: &mut Criterion) { + use ssz_rs::{HashTreeRoot, List}; + + let inner: Vec> = vec![ + vec![0u8, 1u8, 2u8].try_into().unwrap(), + vec![3u8, 4u8, 5u8].try_into().unwrap(), + vec![6u8, 7u8, 8u8].try_into().unwrap(), + vec![9u8, 10u8, 11u8].try_into().unwrap(), + ]; + + // Emulate a transactions tree + let outer: List, 1048576> = List::try_from(inner).unwrap(); + + c.bench_function("hash_tree_root", |b| { + b.iter(|| { + let _ = black_box(outer.hash_tree_root().unwrap()); + }) + }); +} + +criterion_group!(benches, bench_merkleization); +criterion_main!(benches);