use crate::stats::LatencyStats;
use crate::timer::{DefaultTimer, Timer};
pub struct Profiler<T = DefaultTimer> {
timer: T,
stats: LatencyStats,
}
impl Profiler {
pub fn new() -> Self {
Self::with_timer(DefaultTimer::default())
}
}
impl<T: Timer> Profiler<T> {
pub fn with_timer(timer: T) -> Self {
Self {
timer,
stats: LatencyStats::default(),
}
}
pub fn profile<R>(&mut self, f: impl FnOnce() -> R) -> R {
let start = self.timer.now();
let result = f();
let elapsed = self.timer.elapsed(start);
self.stats.record(elapsed);
result
}
pub fn stats(&self) -> &LatencyStats {
&self.stats
}
pub fn reset_stats(&mut self) {
self.stats.reset();
}
}
impl<T: Timer + Default> Default for Profiler<T> {
fn default() -> Self {
Self::with_timer(T::default())
}
}
#[cfg(test)]
mod tests {
use super::*;
use core::cell::Cell;
use oneliner_runtime::ModelInference;
struct DummyTimer {
tick: Cell<u64>,
}
impl DummyTimer {
fn new() -> Self {
Self { tick: Cell::new(0) }
}
}
impl Timer for DummyTimer {
type Instant = u64;
fn now(&self) -> Self::Instant {
let tick = self.tick.get();
self.tick.set(tick + 10);
tick
}
fn elapsed(&self, start: Self::Instant) -> core::time::Duration {
core::time::Duration::from_micros(self.tick.get() - start)
}
}
struct DummyModel;
impl ModelInference for DummyModel {
type InputTensor = u8;
type OutputTensor = u16;
fn create_input_tensor() -> Self::InputTensor {
0
}
fn run(&mut self, input: &Self::InputTensor) -> Self::OutputTensor {
*input as u16 + 1
}
}
#[test]
fn profiles_closures_and_accumulates_stats() {
let mut profiler = Profiler::with_timer(DummyTimer::new());
let mut model = DummyModel;
let output = profiler.profile(|| model.run(&7));
assert_eq!(output, 8);
assert_eq!(profiler.stats().samples, 1);
assert_eq!(profiler.stats().average(), Some(core::time::Duration::from_micros(10)));
let output = profiler.profile(|| model.run(&8));
assert_eq!(output, 9);
assert_eq!(profiler.stats().samples, 2);
assert_eq!(
profiler.stats().min,
Some(core::time::Duration::from_micros(10))
);
assert_eq!(
profiler.stats().max,
Some(core::time::Duration::from_micros(10))
);
assert_eq!(
profiler.stats().average(),
Some(core::time::Duration::from_micros(10))
);
}
#[test]
fn reset_clears_statistics() {
let mut profiler = Profiler::with_timer(DummyTimer::new());
let mut model = DummyModel;
profiler.profile(|| model.run(&1));
assert_eq!(profiler.stats().samples, 1);
profiler.reset_stats();
assert_eq!(profiler.stats().samples, 0);
assert_eq!(profiler.stats().min, None);
assert_eq!(profiler.stats().max, None);
}
#[test]
fn default_timer_builds() {
let _profiler = Profiler::new();
}
}