|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use database::{ |
| 4 | + metric::Metric, |
| 5 | + selector::{BenchmarkQuery, CompileBenchmarkQuery}, |
| 6 | + ArtifactId, Connection, |
| 7 | +}; |
| 8 | +use tabled::{Table, Tabled}; |
| 9 | + |
| 10 | +/// Compare 2 artifacts and print the result. |
| 11 | +pub async fn compare_artifacts( |
| 12 | + mut conn: Box<dyn Connection>, |
| 13 | + base: String, |
| 14 | + modified: String, |
| 15 | +) -> anyhow::Result<()> { |
| 16 | + let index = database::Index::load(&mut *conn).await; |
| 17 | + |
| 18 | + let query = CompileBenchmarkQuery::default() |
| 19 | + .metric(database::selector::Selector::One(Metric::InstructionsUser)); |
| 20 | + let resp = query |
| 21 | + .execute( |
| 22 | + &mut *conn, |
| 23 | + &index, |
| 24 | + Arc::new(vec![ArtifactId::Tag(base), ArtifactId::Tag(modified)]), |
| 25 | + ) |
| 26 | + .await |
| 27 | + .unwrap(); |
| 28 | + |
| 29 | + let tuple_pstats = resp |
| 30 | + .into_iter() |
| 31 | + .map(|resp| { |
| 32 | + let points = resp.series.points.collect::<Vec<_>>(); |
| 33 | + (points[0], points[1]) |
| 34 | + }) |
| 35 | + .collect::<Vec<_>>(); |
| 36 | + |
| 37 | + #[derive(Tabled)] |
| 38 | + struct Regression { |
| 39 | + count: usize, |
| 40 | + #[tabled(display_with = "display_range")] |
| 41 | + range: (Option<f64>, Option<f64>), |
| 42 | + #[tabled(display_with = "display_mean")] |
| 43 | + mean: Option<f64>, |
| 44 | + } |
| 45 | + |
| 46 | + fn format_value(value: Option<f64>) -> String { |
| 47 | + match value { |
| 48 | + Some(value) => format!("{:+.2}%", value), |
| 49 | + None => "-".to_string(), |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + fn display_range(&(min, max): &(Option<f64>, Option<f64>)) -> String { |
| 54 | + format!("[{}, {}]", &format_value(min), &format_value(max)) |
| 55 | + } |
| 56 | + |
| 57 | + fn display_mean(value: &Option<f64>) -> String { |
| 58 | + match value { |
| 59 | + Some(value) => format!("{:+.2}%", value), |
| 60 | + None => "-".to_string(), |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + impl From<&Vec<f64>> for Regression { |
| 65 | + fn from(value: &Vec<f64>) -> Self { |
| 66 | + let min = value.iter().copied().min_by(|a, b| a.total_cmp(b)); |
| 67 | + let max = value.iter().copied().max_by(|a, b| a.total_cmp(b)); |
| 68 | + let count = value.len(); |
| 69 | + |
| 70 | + Regression { |
| 71 | + range: (min, max), |
| 72 | + count, |
| 73 | + mean: if count == 0 { |
| 74 | + None |
| 75 | + } else { |
| 76 | + Some(value.iter().sum::<f64>() / count as f64) |
| 77 | + }, |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + let change = tuple_pstats |
| 83 | + .iter() |
| 84 | + .filter_map(|&(a, b)| match (a, b) { |
| 85 | + (Some(a), Some(b)) => { |
| 86 | + if a == 0.0 { |
| 87 | + None |
| 88 | + } else { |
| 89 | + Some((b - a) / a) |
| 90 | + } |
| 91 | + } |
| 92 | + (_, _) => None, |
| 93 | + }) |
| 94 | + .collect::<Vec<_>>(); |
| 95 | + let negative_change = change |
| 96 | + .iter() |
| 97 | + .copied() |
| 98 | + .filter(|&c| c < 0.0) |
| 99 | + .collect::<Vec<_>>(); |
| 100 | + let positive_change = change |
| 101 | + .iter() |
| 102 | + .copied() |
| 103 | + .filter(|&c| c > 0.0) |
| 104 | + .collect::<Vec<_>>(); |
| 105 | + |
| 106 | + #[derive(Tabled)] |
| 107 | + struct NamedRegression { |
| 108 | + name: String, |
| 109 | + #[tabled(inline)] |
| 110 | + regression: Regression, |
| 111 | + } |
| 112 | + |
| 113 | + let regressions = [negative_change, positive_change, change] |
| 114 | + .into_iter() |
| 115 | + .map(|c| Regression::from(&c)) |
| 116 | + .zip(["❌", "✅", "✅, ❌"]) |
| 117 | + .map(|(c, label)| NamedRegression { |
| 118 | + name: label.to_string(), |
| 119 | + regression: c, |
| 120 | + }) |
| 121 | + .collect::<Vec<_>>(); |
| 122 | + |
| 123 | + println!("{}", Table::new(regressions)); |
| 124 | + |
| 125 | + Ok(()) |
| 126 | +} |
0 commit comments