Skip to content

Commit

Permalink
Format benchmarking stats into a table for better readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
apetkov-so committed Jan 22, 2025
1 parent 156ea4c commit f50cdeb
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
31 changes: 31 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/aranya-policy-vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ aranya-policy-module = { version = "0.3.0", path = "../aranya-policy-module" }

heapless = { workspace = true }
postcard = { workspace = true, features = ["alloc"] }
table_formatter = { version = "0.6.1" }

[dev-dependencies]
aranya-crypto = { path = "../aranya-crypto", default-features = false, features = ["alloc"] }
Expand Down
46 changes: 38 additions & 8 deletions crates/aranya-policy-vm/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@
extern crate alloc;

use alloc::{collections::BTreeMap, string::String, vec, vec::Vec};
use core::{
fmt::{self, Display},
ops::Div,
};
use core::{fmt::Display, ops::Div};
use std::{
fmt,
sync::{Mutex, MutexGuard},
time::{Duration, Instant},
};

use table_formatter::{cell, table, table::Align};

/// Measures and records times for named tasks.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Stopwatch {
Expand Down Expand Up @@ -164,10 +164,40 @@ impl BenchMeasurements {

/// Prints benchmarking stats
pub fn print_stats(&self) {
println!("----- Benchmark Results: -----");
for stat in self.stats() {
println!("{}", stat);
}
let header = vec![
cell!("Name"),
cell!("# Samples"),
cell!("Best"),
cell!("Worst"),
cell!("Mean"),
cell!("SD"),
];
let cells = {
self.stats()
.iter()
.map(|s| {
vec![
cell!(s.name.clone()),
cell!(s.num_samples, align = Align::Right),
cell!(format!("{:?}", s.best), align = Align::Right),
cell!(format!("{:?}", s.worst), align = Align::Right),
cell!(format!("{:?}", s.mean), align = Align::Right),
cell!(format!("{:?}", s.std_dev), align = Align::Right),
]
})
.collect()
};
let table = table! {
header
---
cells
};
let mut buf = vec![];
table.render(&mut buf).expect("table should render");
println!(
"{}",
String::from_utf8(buf).expect("table should convert to string")
);
}
}

Expand Down

0 comments on commit f50cdeb

Please sign in to comment.