| 1 |
use criterion::{Criterion, criterion_group, criterion_main}; |
| 2 |
use std::io::BufReader; |
| 3 |
|
| 4 |
fn open_appfs() -> Option<BufReader<std::fs::File>> { |
| 5 |
let path = std::path::Path::new("../tests/appfs.raw"); |
| 6 |
if !path.exists() { |
| 7 |
return None; |
| 8 |
} |
| 9 |
let file = std::fs::File::open(path).ok()?; |
| 10 |
Some(BufReader::new(file)) |
| 11 |
} |
| 12 |
|
| 13 |
fn bench_open(c: &mut Criterion) { |
| 14 |
if open_appfs().is_none() { |
| 15 |
eprintln!("Skipping benchmarks - appfs.raw not found"); |
| 16 |
return; |
| 17 |
} |
| 18 |
|
| 19 |
c.bench_function("apfs_open", |b| { |
| 20 |
b.iter(|| { |
| 21 |
let reader = open_appfs().unwrap(); |
| 22 |
let _vol = apfs::ApfsVolume::open(reader).unwrap(); |
| 23 |
}) |
| 24 |
}); |
| 25 |
} |
| 26 |
|
| 27 |
fn bench_list_root(c: &mut Criterion) { |
| 28 |
let reader = match open_appfs() { |
| 29 |
Some(r) => r, |
| 30 |
None => return, |
| 31 |
}; |
| 32 |
|
| 33 |
let mut vol = apfs::ApfsVolume::open(reader).unwrap(); |
| 34 |
|
| 35 |
c.bench_function("apfs_list_root", |b| { |
| 36 |
b.iter(|| { |
| 37 |
let _entries = vol.list_directory("/").unwrap(); |
| 38 |
}) |
| 39 |
}); |
| 40 |
} |
| 41 |
|
| 42 |
fn bench_walk_all(c: &mut Criterion) { |
| 43 |
if open_appfs().is_none() { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
c.bench_function("apfs_walk_all", |b| { |
| 48 |
b.iter(|| { |
| 49 |
let reader = open_appfs().unwrap(); |
| 50 |
let mut vol = apfs::ApfsVolume::open(reader).unwrap(); |
| 51 |
let _entries = vol.walk().unwrap(); |
| 52 |
}) |
| 53 |
}); |
| 54 |
} |
| 55 |
|
| 56 |
fn bench_stat(c: &mut Criterion) { |
| 57 |
let reader = match open_appfs() { |
| 58 |
Some(r) => r, |
| 59 |
None => return, |
| 60 |
}; |
| 61 |
|
| 62 |
let mut vol = apfs::ApfsVolume::open(reader).unwrap(); |
| 63 |
|
| 64 |
|
| 65 |
let walk = vol.walk().unwrap(); |
| 66 |
let file_path = walk |
| 67 |
.iter() |
| 68 |
.find(|e| e.entry.kind == apfs::EntryKind::File && e.entry.size > 0) |
| 69 |
.map(|e| e.path.clone()); |
| 70 |
|
| 71 |
if let Some(path) = file_path { |
| 72 |
c.bench_function("apfs_stat", |b| { |
| 73 |
b.iter(|| { |
| 74 |
let _stat = vol.stat(&path).unwrap(); |
| 75 |
}) |
| 76 |
}); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
fn bench_read_small_file(c: &mut Criterion) { |
| 81 |
let reader = match open_appfs() { |
| 82 |
Some(r) => r, |
| 83 |
None => return, |
| 84 |
}; |
| 85 |
|
| 86 |
let mut vol = apfs::ApfsVolume::open(reader).unwrap(); |
| 87 |
|
| 88 |
|
| 89 |
let walk = vol.walk().unwrap(); |
| 90 |
let file_path = walk |
| 91 |
.iter() |
| 92 |
.find(|e| { |
| 93 |
e.entry.kind == apfs::EntryKind::File && e.entry.size > 0 && e.entry.size < 100_000 |
| 94 |
}) |
| 95 |
.map(|e| e.path.clone()); |
| 96 |
|
| 97 |
if let Some(path) = file_path { |
| 98 |
c.bench_function("apfs_read_small_file", |b| { |
| 99 |
b.iter(|| { |
| 100 |
let _data = vol.read_file(&path).unwrap(); |
| 101 |
}) |
| 102 |
}); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
criterion_group!( |
| 107 |
benches, |
| 108 |
bench_open, |
| 109 |
bench_list_root, |
| 110 |
bench_walk_all, |
| 111 |
bench_stat, |
| 112 |
bench_read_small_file |
| 113 |
); |
| 114 |
criterion_main!(benches); |