Skip to content

Commit 8a2b4bf

Browse files
committed
Generalize metric parsing
1 parent 863d2be commit 8a2b4bf

File tree

1 file changed

+68
-31
lines changed

1 file changed

+68
-31
lines changed

site/src/comparison.rs

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::selector::{self, Tag};
1010

1111
use collector::category::Category;
1212
use collector::Bound;
13-
use serde::{Deserialize, Serialize};
13+
use serde::{Deserialize, Deserializer, Serialize};
1414

1515
use std::cmp::Ordering;
1616
use std::collections::{HashMap, HashSet};
@@ -48,20 +48,25 @@ pub async fn handle_triage(
4848
let metric = Metric::Instructions;
4949
let benchmark_map = ctxt.get_benchmark_category_map().await;
5050
loop {
51-
let comparison =
52-
match compare_given_commits(before, next.clone(), metric, ctxt, &master_commits)
53-
.await
54-
.map_err(|e| format!("error comparing commits: {}", e))?
55-
{
56-
Some(c) => c,
57-
None => {
58-
log::info!(
59-
"No data found for end bound {:?}. Ending comparison...",
60-
next
61-
);
62-
break;
63-
}
64-
};
51+
let comparison = match compare_given_commits(
52+
before,
53+
next.clone(),
54+
metric.clone(),
55+
ctxt,
56+
&master_commits,
57+
)
58+
.await
59+
.map_err(|e| format!("error comparing commits: {}", e))?
60+
{
61+
Some(c) => c,
62+
None => {
63+
log::info!(
64+
"No data found for end bound {:?}. Ending comparison...",
65+
next
66+
);
67+
break;
68+
}
69+
};
6570
num_comparisons += 1;
6671
log::info!(
6772
"Comparing {} to {}",
@@ -180,34 +185,39 @@ async fn populate_report(
180185
}
181186
}
182187

183-
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
188+
#[derive(Clone, Debug, PartialEq, Serialize)]
184189
pub enum Metric {
185190
#[serde(rename = "instructions:u")]
186191
Instructions,
187192
#[serde(rename = "cycles:u")]
188193
Cycles,
189-
#[serde(rename = "faults")]
190-
Faults,
191194
#[serde(rename = "max-rss")]
192195
MaxRSS,
193-
#[serde(rename = "task-clock")]
194-
TaskClock,
195-
#[serde(rename = "wall-time")]
196-
WallTime,
197-
#[serde(rename = "cpu-clock")]
198-
CpuClock,
196+
Custom(String),
197+
}
198+
199+
impl<'de> Deserialize<'de> for Metric {
200+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
201+
where
202+
D: Deserializer<'de>,
203+
{
204+
let metric = String::deserialize(deserializer)?;
205+
Ok(match metric.as_str() {
206+
"instructions:u" => Metric::Instructions,
207+
"cycles:u" => Metric::Cycles,
208+
"max-rss" => Metric::MaxRSS,
209+
_ => Metric::Custom(metric),
210+
})
211+
}
199212
}
200213

201214
impl Metric {
202-
pub fn as_str(&self) -> &'static str {
215+
pub fn as_str(&self) -> &str {
203216
match self {
204217
Self::Instructions => "instructions:u",
205218
Self::Cycles => "cycles:u",
206-
Self::Faults => "faults",
207219
Self::MaxRSS => "max-rss",
208-
Self::TaskClock => "task-clock",
209-
Self::WallTime => "wall-time",
210-
Self::CpuClock => "cpu-clock",
220+
Self::Custom(str) => str.as_str(),
211221
}
212222
}
213223

@@ -662,7 +672,7 @@ async fn compare_given_commits(
662672
let statistics_for_b = statistics_from_series(&mut responses);
663673

664674
let mut historical_data =
665-
HistoricalDataMap::calculate(ctxt, a.clone(), master_commits, metric).await?;
675+
HistoricalDataMap::calculate(ctxt, a.clone(), master_commits, metric.clone()).await?;
666676
let comparisons = statistics_for_a
667677
.into_iter()
668678
.filter_map(|(test_case, a)| {
@@ -672,7 +682,7 @@ async fn compare_given_commits(
672682
benchmark: test_case.0,
673683
profile: test_case.1,
674684
scenario: test_case.2,
675-
metric,
685+
metric: metric.clone(),
676686
historical_data: historical_data.data.remove(&test_case),
677687
results: (a, b),
678688
})
@@ -1484,6 +1494,33 @@ mod tests {
14841494
);
14851495
}
14861496

1497+
#[test]
1498+
fn parse_metric_instructions() {
1499+
let metric: Metric = serde_json::from_str(r#""instructions:u""#).unwrap();
1500+
assert!(matches!(metric, Metric::Instructions));
1501+
}
1502+
1503+
#[test]
1504+
fn parse_metric_cycles() {
1505+
let metric: Metric = serde_json::from_str(r#""cycles:u""#).unwrap();
1506+
assert!(matches!(metric, Metric::Cycles));
1507+
}
1508+
1509+
#[test]
1510+
fn parse_metric_max_rss() {
1511+
let metric: Metric = serde_json::from_str(r#""max-rss""#).unwrap();
1512+
assert!(matches!(metric, Metric::MaxRSS));
1513+
}
1514+
1515+
#[test]
1516+
fn parse_metric_custom() {
1517+
let metric: Metric = serde_json::from_str(r#""foo""#).unwrap();
1518+
match metric {
1519+
Metric::Custom(str) => assert_eq!(str, "foo"),
1520+
_ => panic!(),
1521+
}
1522+
}
1523+
14871524
// (category, before, after)
14881525
fn check_table(values: Vec<(Category, f64, f64)>, expected: &str) {
14891526
let mut primary_comparisons = HashSet::new();

0 commit comments

Comments
 (0)