block_benches.rs 2.1 KB

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