Skip to content

Commit d24be7c

Browse files
committed
perf: enhance Merkle tree benchmarks
Enhance their meaning by benchmarking the proof creation/verification: - without measuring the time needed to create the Merkle tree itself - by benchmarking appropriate number of leaves to prove (instead of all the leaves).
1 parent 38ae131 commit d24be7c

File tree

1 file changed

+60
-27
lines changed

1 file changed

+60
-27
lines changed

mithril-common/benches/merkle_tree.rs

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use mithril_common::crypto_helper::{MKTree, MKTreeNode, MKTreeStoreInMemory};
55
const K: usize = 1_000;
66
const M: usize = 1_000 * K;
77
const TOTAL_LEAVES_BENCHES: &[usize] = &[K, 10 * K, 100 * K, M, 10 * M];
8+
const TOTAL_LEAVES_TO_PROVE_BENCHES: &[usize] = &[1, 10, 100];
9+
const TOTAL_LEAVES_TO_APPEND_BENCHES: &[usize] = &[1, 10, 100];
810

911
fn generate_merkle_tree(total_leaves: usize) -> MKTree<MKTreeStoreInMemory> {
1012
MKTree::new(
@@ -33,46 +35,77 @@ fn create_merkle_tree_root_benches(c: &mut Criterion) {
3335
group.finish();
3436
}
3537

38+
fn append_merkle_tree_benches(c: &mut Criterion) {
39+
for total_leaves_to_append in TOTAL_LEAVES_TO_APPEND_BENCHES.iter() {
40+
let mut group = c.benchmark_group(format!(
41+
"append_merkle_tree[appended_leaves={total_leaves_to_append}]"
42+
));
43+
for total_leaves in TOTAL_LEAVES_BENCHES.iter() {
44+
let mut mk_tree = generate_merkle_tree(*total_leaves);
45+
let leaves_to_append = &mk_tree.leaves()[..*total_leaves_to_append];
46+
group.bench_with_input(
47+
BenchmarkId::from_parameter(total_leaves),
48+
total_leaves,
49+
|b, &_total_leaves| {
50+
b.iter(|| {
51+
mk_tree.append(leaves_to_append).unwrap();
52+
});
53+
},
54+
);
55+
}
56+
group.finish();
57+
}
58+
}
59+
3660
fn create_merkle_tree_proof_benches(c: &mut Criterion) {
37-
let mut group = c.benchmark_group("create_merkle_tree_proof");
38-
for total_leaves in TOTAL_LEAVES_BENCHES.iter() {
39-
group.bench_with_input(
40-
BenchmarkId::from_parameter(total_leaves),
41-
total_leaves,
42-
|b, &_total_leaves| {
43-
b.iter(|| {
44-
let mk_tree = generate_merkle_tree(*total_leaves);
45-
let leaves_to_prove = mk_tree.leaves();
46-
mk_tree.compute_proof(&leaves_to_prove).unwrap();
47-
});
48-
},
49-
);
61+
for total_leaves_to_prove in TOTAL_LEAVES_TO_PROVE_BENCHES.iter() {
62+
let mut group = c.benchmark_group(format!(
63+
"create_merkle_tree_proof[proved_leaves={total_leaves_to_prove}]"
64+
));
65+
for total_leaves in TOTAL_LEAVES_BENCHES.iter() {
66+
let mk_tree = generate_merkle_tree(*total_leaves);
67+
let leaves_to_prove = &mk_tree.leaves()[..*total_leaves_to_prove];
68+
group.bench_with_input(
69+
BenchmarkId::from_parameter(total_leaves),
70+
total_leaves,
71+
|b, &_total_leaves| {
72+
b.iter(|| {
73+
mk_tree.compute_proof(leaves_to_prove).unwrap();
74+
});
75+
},
76+
);
77+
}
78+
group.finish();
5079
}
51-
group.finish();
5280
}
5381

5482
fn verify_merkle_tree_proof_benches(c: &mut Criterion) {
55-
let mut group = c.benchmark_group("verify_merkle_tree_proof");
56-
for total_leaves in TOTAL_LEAVES_BENCHES.iter() {
57-
let mk_tree = generate_merkle_tree(*total_leaves);
58-
let leaves_to_prove = mk_tree.leaves();
59-
let mk_proof = mk_tree.compute_proof(&leaves_to_prove).unwrap();
60-
group.bench_with_input(
61-
BenchmarkId::from_parameter(total_leaves),
62-
total_leaves,
63-
|b, &_total_leaves| {
64-
b.iter(|| mk_proof.verify().unwrap());
65-
},
66-
);
83+
for total_leaves_to_prove in TOTAL_LEAVES_TO_PROVE_BENCHES.iter() {
84+
let mut group = c.benchmark_group(format!(
85+
"verify_merkle_tree_proof[proved_leaves={total_leaves_to_prove}]"
86+
));
87+
for total_leaves in TOTAL_LEAVES_BENCHES.iter() {
88+
let mk_tree = generate_merkle_tree(*total_leaves);
89+
let leaves_to_prove = &mk_tree.leaves()[..*total_leaves_to_prove];
90+
let mk_proof = mk_tree.compute_proof(leaves_to_prove).unwrap();
91+
group.bench_with_input(
92+
BenchmarkId::from_parameter(total_leaves),
93+
total_leaves,
94+
|b, &_total_leaves| {
95+
b.iter(|| mk_proof.verify().unwrap());
96+
},
97+
);
98+
}
99+
group.finish();
67100
}
68-
group.finish();
69101
}
70102

71103
criterion_group!(
72104
name=benches;
73105
config = Criterion::default().sample_size(10);
74106
targets=
75107
create_merkle_tree_root_benches,
108+
append_merkle_tree_benches,
76109
create_merkle_tree_proof_benches,
77110
verify_merkle_tree_proof_benches
78111
);

0 commit comments

Comments
 (0)