kvdb.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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 kvdb_overlay::Database;
  22. use rand::rngs::OsRng;
  23. fn kvdb(c: &mut Criterion) {
  24. let (kvdb, _kvdb_folder) = Database::open_temp().unwrap();
  25. let tree = kvdb.open_tree_default("hello").unwrap();
  26. let mut group = c.benchmark_group("inserts");
  27. for i in 0..10 {
  28. println!("i={}", i);
  29. // Insert 1 million keys
  30. for j in 0..1_000_000 {
  31. if j % 100000 == 0 {
  32. println!(" inserted {} values...", j);
  33. }
  34. let a = Fp::random(&mut OsRng).to_repr();
  35. tree.insert(&a, &[]).unwrap();
  36. }
  37. let x = Fp::random(&mut OsRng).to_repr();
  38. group.bench_with_input(BenchmarkId::from_parameter(i), &i, |b, &_| {
  39. b.iter_batched(|| tree.remove(&x), |_| tree.insert(&x, &[]), BatchSize::SmallInput)
  40. });
  41. }
  42. tree.clear().unwrap();
  43. let _ = kvdb.drop_tree("hello");
  44. }
  45. criterion_group!(bench, kvdb);
  46. criterion_main!(bench);