use super::budget::Budget;
use super::report::DiskMetrics;
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant, SystemTime};
const CHUNK: usize = 4 * 1024 * 1024;
const TOTAL: usize = 256 * 1024 * 1024;
struct Scratch(PathBuf);
impl Drop for Scratch {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.0);
}
}
fn scratch_name(pid: u32) -> String {
format!("bench-{pid}.tmp")
}
pub const STALE_SCRATCH_AFTER: Duration = Duration::from_secs(3600);
pub fn sweep_stale_scratch(dir: &Path, now: SystemTime, max_age: Duration) -> usize {
let Ok(rd) = std::fs::read_dir(dir) else {
return 0;
};
let mut n = 0;
for e in rd.flatten() {
let name = e.file_name();
let Some(name) = name.to_str() else { continue };
if !(name.starts_with("bench-") && name.ends_with(".tmp")) {
continue;
}
let old = e
.metadata()
.and_then(|m| m.modified())
.ok()
.and_then(|t| now.duration_since(t).ok())
.is_some_and(|age| age > max_age);
if old && std::fs::remove_file(e.path()).is_ok() {
n += 1;
}
}
n
}
pub fn probe(dir: &Path, b: &Budget) -> Result<DiskMetrics, String> {
probe_sized(dir, b, TOTAL)
}
fn probe_sized(dir: &Path, b: &Budget, total: usize) -> Result<DiskMetrics, String> {
if b.cancelled() {
return Err("cancelled".into());
}
let scratch = Scratch(dir.join(scratch_name(std::process::id())));
let mut x: u64 = 0x9E37_79B9_7F4A_7C15;
let mut buf = vec![0u8; CHUNK];
for w in buf.chunks_mut(8) {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
w.copy_from_slice(&x.to_le_bytes()[..w.len()]);
}
let chunks = total / CHUNK;
let t = Instant::now();
{
let mut f = File::create(&scratch.0).map_err(|e| e.to_string())?;
for _ in 0..chunks {
if b.expired() {
return Err("budget exhausted during disk write".into());
}
f.write_all(&buf).map_err(|e| e.to_string())?;
}
f.sync_all().map_err(|e| e.to_string())?;
}
let write_secs = t.elapsed().as_secs_f64();
let t = Instant::now();
let mut f = File::open(&scratch.0).map_err(|e| e.to_string())?;
let mut rbuf = vec![0u8; CHUNK];
let mut read = 0usize;
while read < total {
if b.expired() {
return Err("budget exhausted during disk read".into());
}
let n = f.read(&mut rbuf).map_err(|e| e.to_string())?;
if n == 0 {
break;
}
read += n;
}
let read_secs = t.elapsed().as_secs_f64();
if read < total || write_secs <= 0.0 || read_secs <= 0.0 {
return Err("disk probe short read".into());
}
let mb = total as f64 / 1e6;
Ok(DiskMetrics {
write_mbps: mb / write_secs,
read_mbps: mb / read_secs,
})
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
#[test]
fn the_sweep_removes_only_old_bench_scratch_files() {
let dir = std::env::temp_dir().join(format!("zc-bench-sweep-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
for n in [
"bench-123.tmp",
"bench-9.tmp",
"last.json",
"last.json.tmp",
"notbench-1.tmp",
"bench-1.log",
] {
std::fs::write(dir.join(n), b"x").unwrap();
}
let hour = Duration::from_secs(3600);
assert_eq!(sweep_stale_scratch(&dir, SystemTime::now(), hour), 0);
assert!(dir.join("bench-123.tmp").exists());
let later = SystemTime::now() + 2 * hour;
assert_eq!(sweep_stale_scratch(&dir, later, hour), 2);
assert!(!dir.join("bench-123.tmp").exists() && !dir.join("bench-9.tmp").exists());
for keep in [
"last.json",
"last.json.tmp",
"notbench-1.tmp",
"bench-1.log",
] {
assert!(dir.join(keep).exists(), "{keep} must survive");
}
assert_eq!(sweep_stale_scratch(&dir.join("nope"), later, hour), 0);
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn disk_probe_measures_and_removes_its_file() {
let dir = std::env::temp_dir().join(format!("zc-bench-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let m = probe_sized(
&dir,
&Budget::new(Duration::from_secs(10), Arc::new(AtomicBool::new(false))),
2 * CHUNK,
)
.unwrap();
assert!(m.write_mbps > 0.0 && m.read_mbps > 0.0);
assert!(!dir.join(scratch_name(std::process::id())).exists());
assert_ne!(scratch_name(1), scratch_name(2));
std::fs::remove_dir_all(&dir).ok();
}
}