/ bench / sled.rs
sled.rs
 1  /* This file is part of DarkFi (https://dark.fi)
 2   *
 3   * Copyright (C) 2020-2025 Dyne.org foundation
 4   *
 5   * This program is free software: you can redistribute it and/or modify
 6   * it under the terms of the GNU Affero General Public License as
 7   * published by the Free Software Foundation, either version 3 of the
 8   * License, or (at your option) any later version.
 9   *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Affero General Public License for more details.
14   *
15   * You should have received a copy of the GNU Affero General Public License
16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17   */
18  
19  use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
20  use darkfi_sdk::crypto::pasta_prelude::*;
21  use halo2_proofs::pasta::Fp;
22  use rand::rngs::OsRng;
23  
24  fn sled(c: &mut Criterion) {
25      let db = sled::open("/tmp/db").unwrap();
26      let tree = db.open_tree(b"hello").unwrap();
27  
28      let mut group = c.benchmark_group("inserts");
29      for i in 0..10 {
30          println!("i={}", i);
31          // Insert 1 million keys
32          for j in 0..1_000_000 {
33              if j % 100000 == 0 {
34                  println!("  inserted {} values...", j);
35              }
36              let a = Fp::random(&mut OsRng).to_repr();
37              tree.insert(&a, &[]).unwrap();
38          }
39          let x = Fp::random(&mut OsRng).to_repr();
40          group.bench_with_input(BenchmarkId::from_parameter(i), &i, |b, &_| {
41              b.iter_batched(|| tree.remove(&x), |_| tree.insert(&x, &[]), BatchSize::SmallInput)
42          });
43      }
44      tree.clear().unwrap();
45      let _ = db.drop_tree(b"hello");
46  }
47  
48  criterion_group!(bench, sled);
49  criterion_main!(bench);