use log::{debug, info};
use std::time::Instant;
#[derive(Clone, Debug)]
pub struct WriteStatistics {
start: Instant,
num_chunks: usize,
chunk_label: String,
num_bytes: usize,
}
impl WriteStatistics {
pub fn new(chunk_label: impl Into<String>) -> Self {
Self {
start: Instant::now(),
num_chunks: 0,
chunk_label: chunk_label.into(),
num_bytes: 0,
}
}
pub fn increment_chunks(&mut self, num_chunks: usize) {
self.num_chunks += num_chunks;
}
pub fn increment_bytes(&mut self, num_bytes: usize) {
self.num_bytes += num_bytes;
}
}
impl Drop for WriteStatistics {
fn drop(&mut self) {
let duration = self.start.elapsed();
let mb_per_chunk = self.num_bytes as f64 / (1024.0 * 1024.0) / self.num_chunks as f64;
let bytes_per_second = (self.num_bytes as f64 / duration.as_secs_f64()) as u64;
let gb_per_second = bytes_per_second as f64 / (1024.0 * 1024.0 * 1024.0);
let gb = self.num_bytes as f64 / (1024.0 * 1024.0 * 1024.0);
info!("Created {gb:.02} GB in {duration:?} ({gb_per_second:.02} GB/sec)");
debug!(
"Wrote {} bytes in {} {} {mb_per_chunk:.02} MB/{}",
self.num_bytes, self.num_chunks, self.chunk_label, self.chunk_label
);
}
}