sled.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
  19. use darkfi_sdk::crypto::pasta_prelude::*;
  20. use halo2_proofs::pasta::Fp;
  21. use rand::rngs::OsRng;
  22. fn sled(c: &mut Criterion) {
  23. let db = sled::open("/tmp/db").unwrap();
  24. let tree = db.open_tree(b"hello").unwrap();
  25. let mut group = c.benchmark_group("inserts");
  26. for i in 0..10 {
  27. println!("i={}", i);
  28. // Insert 1 million keys
  29. for j in 0..1_000_000 {
  30. if j % 100000 == 0 {
  31. println!(" inserted {} values...", j);
  32. }
  33. let a = Fp::random(&mut OsRng).to_repr();
  34. tree.insert(&a, &[]).unwrap();
  35. }
  36. let x = Fp::random(&mut OsRng).to_repr();
  37. group.bench_with_input(BenchmarkId::from_parameter(i), &i, |b, &_| {
  38. b.iter_batched(|| tree.remove(&x), |_| tree.insert(&x, &[]), BatchSize::SmallInput)
  39. });
  40. }
  41. tree.clear().unwrap();
  42. let _ = db.drop_tree(b"hello");
  43. }
  44. criterion_group!(bench, sled);
  45. criterion_main!(bench);