block_benches.rs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // SPDX-License-Identifier: AGPL-3.0-or-later
  2. //! Benchmarks for implementations of the [Block] trait.
  3. //! You can run these with `cargo bench`.
  4. use std::{fs::OpenOptions, io::Write, time::Duration};
  5. use btlib::{
  6. crypto::{ConcreteCreds, Creds},
  7. BlockOpenOptions, BlockPath, Epoch, Principaled, SECTOR_SZ_DEFAULT,
  8. };
  9. use criterion::{black_box, criterion_group, criterion_main, Criterion};
  10. use tempdir::TempDir;
  11. fn block_write(c: &mut Criterion) {
  12. let temp_dir = TempDir::new("block_bench").expect("failed to create temp dir");
  13. let root_creds = ConcreteCreds::generate().expect("failed to generate root_creds");
  14. let mut node_creds = ConcreteCreds::generate().expect("failed to generate node_creds");
  15. let components = ["nodes", "phone"];
  16. let writecap = root_creds
  17. .issue_writecap(
  18. node_creds.principal(),
  19. &mut components.into_iter(),
  20. Epoch::now() + Duration::from_secs(3600),
  21. )
  22. .expect("failed to issue writecap");
  23. node_creds.set_writecap(writecap).unwrap();
  24. let mut fs_path = temp_dir.path().to_owned();
  25. fs_path.extend(components.iter());
  26. std::fs::create_dir_all(&fs_path).expect("failed to create fs_path");
  27. let node_path = BlockPath::from_components(root_creds.principal(), components.into_iter());
  28. let file_path = fs_path.join("file.txt");
  29. c.bench_function("block_write", |b| {
  30. b.iter(|| {
  31. let file = OpenOptions::new()
  32. .create(true)
  33. .truncate(true)
  34. .read(true)
  35. .write(true)
  36. .open(&file_path)
  37. .expect("failed to open file");
  38. let mut block = BlockOpenOptions::new()
  39. .with_inner(file)
  40. .with_creds(node_creds.clone())
  41. .with_encrypt(true)
  42. .with_block_path(node_path.clone())
  43. .open()
  44. .expect("failed to open block");
  45. const ZEROS: [u8; SECTOR_SZ_DEFAULT] = [0u8; SECTOR_SZ_DEFAULT];
  46. const ITER: usize = (512 * 1024) / ZEROS.len();
  47. for zeros in std::iter::repeat(ZEROS.as_slice()).take(ITER) {
  48. block.write(black_box(zeros)).expect("write failed");
  49. }
  50. })
  51. });
  52. }
  53. criterion_group!(benches, block_write);
  54. criterion_main!(benches);