add: Throughput testing helpers

This commit is contained in:
Jordan Pittman 2024-10-12 18:36:00 -04:00
parent 25a88c57bb
commit 8c5aec02c4
2 changed files with 51 additions and 0 deletions

View file

@ -2,6 +2,7 @@ mod escape;
mod fast_stack;
mod gurantee;
mod segment;
mod throughput;
#[allow(unused_imports)]
pub use crate::util::escape::*;
@ -14,3 +15,6 @@ pub use crate::util::gurantee::*;
#[allow(unused_imports)]
pub use crate::util::segment::*;
#[allow(unused_imports)]
pub use crate::util::throughput::*;

View file

@ -0,0 +1,47 @@
use std::fmt::Display;
pub struct Throughput {
rate: f64,
elapsed: std::time::Duration,
}
impl Display for Throughput {
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}/s over {:.2}s", format_byte_size(self.rate), self.elapsed.as_secs_f64())
}
}
impl Throughput {
#[inline(always)]
pub fn compute<F>(iterations: usize, memory_baseline: usize, cb: F) -> Self
where
F: Fn(),
{
let now = std::time::Instant::now();
for _ in 0..iterations {
cb();
}
let elapsed = now.elapsed();
let memory_size = iterations * memory_baseline;
Self {
rate: memory_size as f64 / elapsed.as_secs_f64(),
elapsed,
}
}
}
#[inline(always)]
fn format_byte_size(size: f64) -> String {
let units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
let unit = 1000;
let mut size = size;
let mut i = 0;
while size > unit as f64 {
size /= unit as f64;
i += 1;
}
format!("{:.2} {}", size, units[i])
}