Skip to content

Commit 28dfc77

Browse files
committed
Introduce TestCase to generalize benchmark parameters
1 parent 3563130 commit 28dfc77

File tree

3 files changed

+54
-49
lines changed

3 files changed

+54
-49
lines changed

site/src/comparison.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::api;
66
use crate::db::{ArtifactId, Benchmark, Lookup, Profile, Scenario};
77
use crate::github;
88
use crate::load::SiteCtxt;
9-
use crate::selector::{self};
9+
use crate::selector::{self, CompileBenchmarkQuery, CompileTestCase, TestCase};
1010

1111
use collector::benchmark::category::Category;
1212
use collector::Bound;
@@ -798,27 +798,28 @@ async fn compare_given_commits(
798798
let aids = Arc::new(vec![a.clone(), b.clone()]);
799799

800800
// get all crates, cache, and profile combinations for the given metric
801-
let query = selector::CompileBenchmarkQuery::all_for_metric(metric);
801+
let query = CompileBenchmarkQuery::all_for_metric(metric);
802802

803803
// `responses` contains series iterators. The first element in the iterator is the data
804804
// for `a` and the second is the data for `b`
805-
let mut responses = ctxt.compile_statistic_series(query, aids).await?;
805+
let mut responses = ctxt.compile_statistic_series(query.clone(), aids).await?;
806806

807807
let conn = ctxt.conn().await;
808808
let statistics_for_a = statistics_from_series(&mut responses);
809809
let statistics_for_b = statistics_from_series(&mut responses);
810810

811811
let mut historical_data =
812-
HistoricalDataMap::calculate(ctxt, a.clone(), master_commits, metric).await?;
812+
HistoricalDataMap::<CompileTestCase>::calculate(ctxt, a.clone(), master_commits, query)
813+
.await?;
813814
let comparisons = statistics_for_a
814815
.into_iter()
815816
.filter_map(|(test_case, a)| {
816817
statistics_for_b
817818
.get(&test_case)
818819
.map(|&b| CompileTestResultComparison {
819-
benchmark: test_case.0,
820-
profile: test_case.1,
821-
scenario: test_case.2,
820+
benchmark: test_case.benchmark,
821+
profile: test_case.profile,
822+
scenario: test_case.scenario,
822823
comparison: TestResultComparison {
823824
metric,
824825
historical_data: historical_data.data.remove(&test_case),
@@ -877,8 +878,7 @@ pub struct ArtifactDescription {
877878
pub bootstrap_total: u64,
878879
}
879880

880-
type StatisticsMap = HashMap<TestCase, f64>;
881-
type TestCase = (Benchmark, Profile, Scenario);
881+
type StatisticsMap<TestCase> = HashMap<TestCase, f64>;
882882

883883
impl ArtifactDescription {
884884
/// For the given `ArtifactId`, consume the first datapoint in each of the given `SeriesResponse`
@@ -939,11 +939,13 @@ impl ArtifactDescription {
939939
}
940940
}
941941

942-
fn statistics_from_series<T>(series: &mut [selector::SeriesResponse<T>]) -> StatisticsMap
942+
fn statistics_from_series<Case: TestCase, T>(
943+
series: &mut [selector::SeriesResponse<Case, T>],
944+
) -> StatisticsMap<Case>
943945
where
944946
T: Iterator<Item = (ArtifactId, Option<f64>)>,
945947
{
946-
let mut stats: StatisticsMap = HashMap::new();
948+
let mut stats: StatisticsMap<Case> = HashMap::new();
947949
for response in series {
948950
let (_, point) = response.series.next().expect("must have element");
949951

@@ -952,14 +954,7 @@ where
952954
} else {
953955
continue;
954956
};
955-
stats.insert(
956-
(
957-
response.key.benchmark,
958-
response.key.profile,
959-
response.key.scenario,
960-
),
961-
value,
962-
);
957+
stats.insert(response.test_case.clone(), value);
963958
}
964959
stats
965960
}
@@ -1053,19 +1048,19 @@ impl ArtifactComparison {
10531048
}
10541049

10551050
/// The historical data for a certain benchmark
1056-
pub struct HistoricalDataMap {
1051+
pub struct HistoricalDataMap<TestCase> {
10571052
/// Historical data on a per test case basis
1058-
pub data: HashMap<(Benchmark, Profile, Scenario), HistoricalData>,
1053+
pub data: HashMap<TestCase, HistoricalData>,
10591054
}
10601055

1061-
impl HistoricalDataMap {
1056+
impl HistoricalDataMap<CompileTestCase> {
10621057
const NUM_PREVIOUS_COMMITS: usize = 30;
10631058

10641059
async fn calculate(
10651060
ctxt: &SiteCtxt,
10661061
from: ArtifactId,
10671062
master_commits: &[collector::MasterCommit],
1068-
metric: Metric,
1063+
query: CompileBenchmarkQuery,
10691064
) -> Result<Self, BoxedError> {
10701065
let mut historical_data = HashMap::new();
10711066

@@ -1082,16 +1077,13 @@ impl HistoricalDataMap {
10821077
});
10831078
}
10841079

1085-
// get all crates, cache, and profile combinations for the given metric
1086-
let query = selector::CompileBenchmarkQuery::all_for_metric(metric);
1087-
10881080
let mut previous_commit_series = ctxt
10891081
.compile_statistic_series(query, previous_commits.clone())
10901082
.await?;
10911083

10921084
for _ in previous_commits.iter() {
1093-
for (test_case, stat) in statistics_from_series(&mut previous_commit_series) {
1094-
historical_data.entry(test_case).or_default().push(stat);
1085+
for (key, stat) in statistics_from_series(&mut previous_commit_series) {
1086+
historical_data.entry(key).or_default().push(stat);
10951087
}
10961088
}
10971089

site/src/request_handlers/graph.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::api::{graph, graphs, ServerResult};
77
use crate::db::{self, ArtifactId, Profile, Scenario};
88
use crate::interpolate::IsInterpolated;
99
use crate::load::SiteCtxt;
10-
use crate::selector::{CompileBenchmarkQuery, Selector, SeriesResponse};
10+
use crate::selector::{CompileBenchmarkQuery, CompileTestCase, Selector, SeriesResponse};
1111

1212
pub async fn handle_graph(
1313
request: graph::Request,
@@ -119,9 +119,9 @@ async fn create_graphs(
119119
}
120120

121121
for response in interpolated_responses {
122-
let benchmark = response.key.benchmark.to_string();
123-
let profile = response.key.profile;
124-
let scenario = response.key.scenario.to_string();
122+
let benchmark = response.test_case.benchmark.to_string();
123+
let profile = response.test_case.profile;
124+
let scenario = response.test_case.scenario.to_string();
125125
let graph_series = graph_series(response.series.into_iter(), request.kind);
126126

127127
benchmarks
@@ -172,7 +172,10 @@ fn master_artifact_ids_for_range(ctxt: &SiteCtxt, start: Bound, end: Bound) -> V
172172
/// test cases per profile type
173173
fn create_summary(
174174
ctxt: &SiteCtxt,
175-
interpolated_responses: &[SeriesResponse<Vec<((ArtifactId, Option<f64>), IsInterpolated)>>],
175+
interpolated_responses: &[SeriesResponse<
176+
CompileTestCase,
177+
Vec<((ArtifactId, Option<f64>), IsInterpolated)>,
178+
>],
176179
graph_kind: GraphKind,
177180
) -> ServerResult<HashMap<Profile, HashMap<String, graphs::Series>>> {
178181
let mut baselines = HashMap::new();
@@ -188,8 +191,8 @@ fn create_summary(
188191
let baseline_responses = interpolated_responses
189192
.iter()
190193
.filter(|sr| {
191-
let p = sr.key.profile;
192-
let s = sr.key.scenario;
194+
let p = sr.test_case.profile;
195+
let s = sr.test_case.scenario;
193196
p == profile && s == Scenario::Empty
194197
})
195198
.map(|sr| sr.series.iter().cloned())
@@ -205,8 +208,8 @@ fn create_summary(
205208
let summary_case_responses = interpolated_responses
206209
.iter()
207210
.filter(|sr| {
208-
let p = sr.key.profile;
209-
let s = sr.key.scenario;
211+
let p = sr.test_case.profile;
212+
let s = sr.test_case.scenario;
210213
p == profile && s == scenario
211214
})
212215
.map(|sr| sr.series.iter().cloned())

site/src/selector.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use database::{Benchmark, Commit, Index, Lookup};
3030

3131
use crate::comparison::Metric;
3232
use std::fmt;
33+
use std::hash::Hash;
3334
use std::ops::RangeInclusive;
3435
use std::sync::Arc;
3536

@@ -158,21 +159,28 @@ impl<T> Selector<T> {
158159
}
159160
}
160161

162+
/// Represents the parameters of a single benchmark execution that collects a set of statistics.
163+
pub trait TestCase: std::fmt::Debug + Clone + Hash + Eq + PartialEq {}
164+
161165
#[derive(Debug)]
162-
pub struct SeriesResponse<T> {
163-
pub key: CompileBenchmarkKey,
166+
pub struct SeriesResponse<Case, T> {
167+
pub test_case: Case,
164168
pub series: T,
165169
}
166170

167-
impl<T> SeriesResponse<T> {
168-
pub fn map<U>(self, m: impl FnOnce(T) -> U) -> SeriesResponse<U> {
171+
impl<TestCase, T> SeriesResponse<TestCase, T> {
172+
pub fn map<U>(self, m: impl FnOnce(T) -> U) -> SeriesResponse<TestCase, U> {
173+
let SeriesResponse {
174+
test_case: key,
175+
series,
176+
} = self;
169177
SeriesResponse {
170-
key: self.key,
171-
series: m(self.series),
178+
test_case: key,
179+
series: m(series),
172180
}
173181
}
174182

175-
pub fn interpolate(self) -> SeriesResponse<Interpolate<T>>
183+
pub fn interpolate(self) -> SeriesResponse<TestCase, Interpolate<T>>
176184
where
177185
T: Iterator,
178186
T::Item: crate::db::Point,
@@ -231,19 +239,21 @@ impl Default for CompileBenchmarkQuery {
231239
}
232240
}
233241

234-
#[derive(Debug)]
235-
pub struct CompileBenchmarkKey {
242+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
243+
pub struct CompileTestCase {
236244
pub benchmark: Benchmark,
237245
pub profile: Profile,
238246
pub scenario: Scenario,
239247
}
240248

249+
impl TestCase for CompileTestCase {}
250+
241251
impl SiteCtxt {
242252
pub async fn compile_statistic_series(
243253
&self,
244254
query: CompileBenchmarkQuery,
245255
artifact_ids: Arc<Vec<ArtifactId>>,
246-
) -> Result<Vec<SeriesResponse<StatisticSeries>>, String> {
256+
) -> Result<Vec<SeriesResponse<CompileTestCase, StatisticSeries>>, String> {
247257
StatisticSeries::execute_compile_query(artifact_ids, self, query).await
248258
}
249259
}
@@ -258,7 +268,7 @@ impl StatisticSeries {
258268
artifact_ids: Arc<Vec<ArtifactId>>,
259269
ctxt: &SiteCtxt,
260270
query: CompileBenchmarkQuery,
261-
) -> Result<Vec<SeriesResponse<Self>>, String> {
271+
) -> Result<Vec<SeriesResponse<CompileTestCase, Self>>, String> {
262272
let dumped = format!("{:?}", query);
263273

264274
let index = ctxt.index.load();
@@ -318,7 +328,7 @@ impl StatisticSeries {
318328
points.into_iter()
319329
},
320330
},
321-
key: CompileBenchmarkKey {
331+
test_case: CompileTestCase {
322332
benchmark,
323333
profile,
324334
scenario,

0 commit comments

Comments
 (0)