bench.rs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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, Criterion};
  19. use equix_pow::{Challenge, EquiXBuilder, EquiXPow, Solution, SolverMemory, NONCE_LEN};
  20. use rand::{seq::SliceRandom, Rng};
  21. use std::hint::black_box;
  22. fn new_challenge() -> Challenge {
  23. let mut rng = rand::thread_rng();
  24. let random: Vec<u8> = (0..32 + NONCE_LEN).map(|_| rng.gen()).collect();
  25. Challenge(random)
  26. }
  27. fn new_equix() -> EquiXPow {
  28. EquiXPow {
  29. effort: 1000,
  30. challenge: new_challenge(),
  31. equix: EquiXBuilder::default(),
  32. mem: SolverMemory::default(),
  33. }
  34. }
  35. fn benchmark_equix_pow(c: &mut Criterion) {
  36. let mut solutions: Vec<(Challenge, Solution)> = Vec::new();
  37. let mut equix_pow = new_equix();
  38. c.bench_function(&format!("EquiXPow::run effort={}", equix_pow.effort), |b| {
  39. b.iter(|| {
  40. equix_pow.challenge = new_challenge();
  41. let solution = black_box(equix_pow.run().unwrap());
  42. solutions.push((equix_pow.challenge.clone(), solution));
  43. });
  44. });
  45. let equix_pow = new_equix();
  46. c.bench_function(&format!("EquiXPow::verify effort={}", equix_pow.effort), |b| {
  47. b.iter(|| {
  48. let (challenge, solution) =
  49. black_box(solutions.choose(&mut rand::thread_rng()).unwrap());
  50. if let Err(e) = equix_pow.verify(challenge, solution) {
  51. eprintln!("Verification failed: {:?}", e);
  52. }
  53. });
  54. });
  55. }
  56. criterion_group!(benches, benchmark_equix_pow);
  57. criterion_main!(benches);