Skip to content

Commit

Permalink
Suppress metrics table in passing test runs (private-attribution#1400)
Browse files Browse the repository at this point in the history
  • Loading branch information
andyleiserson authored Nov 4, 2024
1 parent dc8f4bb commit 91aa71d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
2 changes: 1 addition & 1 deletion ipa-core/src/protocol/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ mod tests {
let input_size = input.len();
let snapshot = world.metrics_snapshot();
// this will print all metrics if test fails
snapshot.print(&mut std::io::stdout()).unwrap();
println!("{snapshot}");

// for semi-honest protocols, amplification factor per helper is 1.
// that is, for every communication, there is exactly one send and receive of the same data
Expand Down
62 changes: 30 additions & 32 deletions ipa-core/src/telemetry/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
hash_map::{Entry, Iter},
HashMap,
},
fmt::Debug,
fmt::Display,
};

use ipa_metrics::{MetricPartition, MetricsStore};
Expand Down Expand Up @@ -74,6 +74,35 @@ impl<'a> IntoIterator for &'a CounterDetails {
}
}

impl Display for Metrics {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
if self.counters.is_empty() {
return Ok(());
}

let mut metrics_table = comfy_table::Table::new();
metrics_table.set_header(vec!["metric", "value", "dimensions"]);

for (key_name, counter_stats) in &self.counters {
let mut dim_cell_content = String::new();
for (dim, values) in counter_stats {
dim_cell_content += format!("{dim}\n").as_str();
for (dim_value, &counter_val) in values {
dim_cell_content += format!("{dim_value} = {counter_val}\n").as_str();
}
}

metrics_table.add_row(vec![
key_name,
counter_stats.total_value.to_string().as_str(),
dim_cell_content.as_str(),
]);
}

metrics_table.fmt(f)
}
}

impl Metrics {
/// Builds a new metric snapshot for the specified partition.
///
Expand Down Expand Up @@ -133,37 +162,6 @@ impl Metrics {
snapshot: details,
}
}

/// Dumps the stats to the provided Write interface.
///
/// ## Errors
/// returns an IO error if it fails to write to the provided writer.
pub fn print(&self, w: &mut impl std::io::Write) -> Result<(), std::io::Error> {
let mut metrics_table = comfy_table::Table::new();
metrics_table.set_header(vec!["metric", "value", "dimensions"]);

for (key_name, counter_stats) in &self.counters {
let mut dim_cell_content = String::new();
for (dim, values) in counter_stats {
dim_cell_content += format!("{dim}\n").as_str();
for (dim_value, &counter_val) in values {
dim_cell_content += format!("{dim_value} = {counter_val}\n").as_str();
}
}

metrics_table.add_row(vec![
key_name,
counter_stats.total_value.to_string().as_str(),
dim_cell_content.as_str(),
]);
}

if metrics_table.row_iter().len() > 0 {
writeln!(w, "{metrics_table}")?;
}

Ok(())
}
}

#[derive(Clone)]
Expand Down

0 comments on commit 91aa71d

Please sign in to comment.