Skip to content

Commit cebf645

Browse files
author
Eduardo Leegwater Simões
committed
Add more comprehensive benchmarks
The `blake3` insertion benchmark is rewritten to allow for measuring the time of multiple variable insertions. A new benchmark is also introduced that does the same as the insertion benchmark, but also computes the root of the tree at the end of the insertions. Both of these benchmarks allow for accurate estimation of the root calculation from scratch, given a certain number of elements inserted. These are the results on a `AMD Ryzen 9 5950X` (3.4 GHz base clock): ```text blake3_insert_n/10 time: [6.5373 µs 6.5423 µs 6.5481 µs] blake3_insert_n/100 time: [73.387 µs 73.877 µs 74.377 µs] blake3_insert_n/1000 time: [1.1084 ms 1.1090 ms 1.1095 ms] blake3_insert_n/10000 time: [12.633 ms 12.789 ms 12.948 ms] blake3_root_n/10 time: [31.108 µs 31.271 µs 31.422 µs] blake3_root_n/100 time: [298.88 µs 298.93 µs 298.98 µs] blake3_root_n/1000 time: [3.0444 ms 3.0616 ms 3.0790 ms] blake3_root_n/10000 time: [31.971 ms 32.185 ms 32.420 ms] ```
1 parent a8fb57e commit cebf645

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

benches/blake3.rs

+54-17
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,74 @@
44
//
55
// Copyright (c) DUSK NETWORK. All rights reserved.
66

7-
use criterion::{black_box, criterion_group, criterion_main, Criterion};
7+
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
88

9+
use rand::rngs::StdRng;
910
use rand::{RngCore, SeedableRng};
1011

1112
use blake3::Hasher;
13+
1214
use dusk_merkle::blake3::Item;
1315
use dusk_merkle::Tree;
1416

1517
const H: usize = 32;
16-
const A: usize = 4;
18+
const A: usize = 2;
1719

1820
type Blake3Tree = Tree<Item, H, A>;
1921

20-
fn bench_blake3(c: &mut Criterion) {
21-
let tree = &mut Blake3Tree::new();
22-
let rng = &mut rand::rngs::StdRng::seed_from_u64(0xbeef);
22+
const NS: &[u64] = &[10, 100, 1000, 10000];
23+
24+
fn bench_blake3_insert(c: &mut Criterion) {
25+
let rng = &mut StdRng::seed_from_u64(0xbeef);
26+
27+
let mut group = c.benchmark_group("blake3_insert_n");
28+
for n in NS {
29+
group.bench_with_input(
30+
BenchmarkId::from_parameter(*n),
31+
n,
32+
|b, &size| {
33+
b.iter(|| {
34+
let mut tree = Blake3Tree::new();
35+
insert_random_n(rng, &mut tree, size);
36+
});
37+
},
38+
);
39+
}
40+
}
41+
42+
fn bench_blake3_root(c: &mut Criterion) {
43+
let rng = &mut StdRng::seed_from_u64(0xbeef);
44+
45+
let mut group = c.benchmark_group("blake3_root_n");
46+
for n in NS {
47+
group.bench_with_input(
48+
BenchmarkId::from_parameter(*n),
49+
n,
50+
|b, &size| {
51+
b.iter(|| {
52+
let mut tree = Blake3Tree::new();
53+
insert_random_n(rng, &mut tree, size);
54+
let _root = *tree.root();
55+
});
56+
},
57+
);
58+
}
59+
}
2360

24-
c.bench_function("blake3 insertion", |b| {
25-
b.iter(|| {
26-
let pos = rng.next_u64();
61+
fn insert_random_n<Rng: RngCore>(rng: &mut Rng, tree: &mut Blake3Tree, n: u64) {
62+
let cap = tree.capacity();
2763

28-
let mut hash_bytes = [0u8; 32];
29-
rng.fill_bytes(&mut hash_bytes);
30-
let mut hasher = Hasher::new();
31-
hasher.update(&hash_bytes);
32-
let hash: Item = hasher.finalize().into();
64+
let mut hash_bytes = [0u8; 32];
65+
rng.fill_bytes(&mut hash_bytes);
66+
let mut hasher = Hasher::new();
67+
hasher.update(&hash_bytes);
68+
let hash: Item = hasher.finalize().into();
3369

34-
tree.insert(black_box(pos), black_box(hash));
35-
})
36-
});
70+
for _ in 0..n {
71+
let pos = rng.next_u64() % cap;
72+
tree.insert(pos, hash);
73+
}
3774
}
3875

39-
criterion_group!(benches, bench_blake3);
76+
criterion_group!(benches, bench_blake3_insert, bench_blake3_root);
4077
criterion_main!(benches);

0 commit comments

Comments
 (0)