use std::{
path::{Path, PathBuf},
time::{SystemTime, UNIX_EPOCH},
};
use super::PProfBackend;
#[derive(Debug, Clone)]
#[must_use]
pub struct FileBackendConfig {
out_dir: PathBuf,
}
impl FileBackendConfig {
pub fn new(out_dir: impl AsRef<Path>) -> Self {
Self {
out_dir: out_dir.as_ref().to_path_buf(),
}
}
}
#[must_use]
pub struct FileBackend {
config: FileBackendConfig,
}
impl FileBackend {
pub fn new(config: FileBackendConfig) -> Self {
Self { config }
}
}
impl PProfBackend for FileBackend {
fn save_profile(
&self,
kind: &str,
_start_time: SystemTime,
end_time: SystemTime,
profile_data: Vec<u8>,
) {
std::fs::create_dir_all(&self.config.out_dir).unwrap();
let elapsed = end_time
.duration_since(UNIX_EPOCH)
.expect("start time is before end time");
let suffix = elapsed.as_secs() * 1_000_000_000 + u64::from(elapsed.subsec_nanos());
let filename = format!("{kind}_{suffix}.pb.gz");
let file_path = self.config.out_dir.join(&filename);
std::fs::write(&file_path, profile_data).unwrap();
}
}