block_benches.rs 2.2 KB

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