Skip to content

Commit 956ae04

Browse files
committed
Add function for finding computed test cases for an artifact
1 parent 39fab1a commit 956ae04

File tree

3 files changed

+142
-4
lines changed

3 files changed

+142
-4
lines changed

database/src/pool.rs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
use crate::selector::CompileTestCase;
12
use crate::{
23
ArtifactCollection, ArtifactId, ArtifactIdNumber, BenchmarkRequest, CodegenBackend,
34
CompileBenchmark, Target,
45
};
56
use crate::{CollectionId, Index, Profile, QueuedCommit, Scenario, Step};
67
use chrono::{DateTime, Utc};
7-
use hashbrown::HashMap;
8+
use hashbrown::{HashMap, HashSet};
89
use std::sync::{Arc, Mutex};
910
use std::time::Duration;
1011
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
@@ -183,6 +184,17 @@ pub trait Connection: Send + Sync {
183184
/// Add an item to the `benchmark_requests`, if the `benchmark_request`
184185
/// exists it will be ignored
185186
async fn insert_benchmark_request(&self, benchmark_request: &BenchmarkRequest);
187+
188+
/// Returns a set of compile-time benchmark test cases that were already computed for the
189+
/// given artifact.
190+
/// Note that for efficiency reasons, the function only checks if we have at least a single
191+
/// result for a given test case. It does not check if *all* test results from all test
192+
/// iterations were finished.
193+
/// Therefore, the result is an over-approximation.
194+
async fn get_compile_test_cases_with_measurements(
195+
&self,
196+
artifact_row_id: &ArtifactIdNumber,
197+
) -> anyhow::Result<HashSet<CompileTestCase>>;
186198
}
187199

188200
#[async_trait::async_trait]
@@ -302,6 +314,9 @@ impl Pool {
302314

303315
#[cfg(test)]
304316
mod tests {
317+
use super::*;
318+
use crate::metric::Metric;
319+
use crate::{tests::run_db_test, Commit, CommitType, Date};
305320
use chrono::Utc;
306321
use std::str::FromStr;
307322

@@ -424,4 +439,64 @@ mod tests {
424439
})
425440
.await;
426441
}
442+
443+
#[tokio::test]
444+
async fn get_compile_test_cases_with_data() {
445+
run_db_test(|ctx| async {
446+
let db = ctx.db_client().connection().await;
447+
448+
let collection = db.collection_id("test").await;
449+
let artifact = db
450+
.artifact_id(&ArtifactId::Commit(create_commit(
451+
"abcdef",
452+
Utc::now(),
453+
CommitType::Try,
454+
)))
455+
.await;
456+
db.record_compile_benchmark("benchmark", None, "primary".to_string())
457+
.await;
458+
459+
db.record_statistic(
460+
collection,
461+
artifact,
462+
"benchmark",
463+
Profile::Check,
464+
Scenario::IncrementalFresh,
465+
CodegenBackend::Llvm,
466+
Target::X86_64UnknownLinuxGnu,
467+
Metric::CacheMisses.as_str(),
468+
1.0,
469+
)
470+
.await;
471+
472+
assert_eq!(
473+
db.get_compile_test_cases_with_measurements(&artifact)
474+
.await
475+
.unwrap(),
476+
HashSet::from([CompileTestCase {
477+
benchmark: "benchmark".into(),
478+
profile: Profile::Check,
479+
scenario: Scenario::IncrementalFresh,
480+
backend: CodegenBackend::Llvm,
481+
target: Target::X86_64UnknownLinuxGnu,
482+
}])
483+
);
484+
485+
let artifact2 = db
486+
.artifact_id(&ArtifactId::Commit(create_commit(
487+
"abcdef2",
488+
Utc::now(),
489+
CommitType::Try,
490+
)))
491+
.await;
492+
assert!(db
493+
.get_compile_test_cases_with_measurements(&artifact2)
494+
.await
495+
.unwrap()
496+
.is_empty());
497+
498+
Ok(ctx)
499+
})
500+
.await;
501+
}
427502
}

database/src/pool/postgres.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use crate::pool::{Connection, ConnectionManager, ManagedConnection, Transaction};
2+
use crate::selector::CompileTestCase;
23
use crate::{
34
ArtifactCollection, ArtifactId, ArtifactIdNumber, Benchmark, BenchmarkRequest, CodegenBackend,
45
CollectionId, Commit, CommitType, CompileBenchmark, Date, Index, Profile, QueuedCommit,
56
Scenario, Target,
67
};
78
use anyhow::Context as _;
89
use chrono::{DateTime, TimeZone, Utc};
9-
use hashbrown::HashMap;
10+
use hashbrown::{HashMap, HashSet};
1011
use native_tls::{Certificate, TlsConnector};
1112
use postgres_native_tls::MakeTlsConnector;
1213
use std::str::FromStr;
@@ -382,6 +383,7 @@ pub struct CachedStatements {
382383
get_runtime_pstat: Statement,
383384
record_artifact_size: Statement,
384385
get_artifact_size: Statement,
386+
get_compile_test_cases_with_measurements: Statement,
385387
}
386388

387389
pub struct PostgresTransaction<'a> {
@@ -563,7 +565,16 @@ impl PostgresConnection {
563565
get_artifact_size: conn.prepare("
564566
select component, size from artifact_size
565567
where aid = $1
566-
").await.unwrap()
568+
").await.unwrap(),
569+
get_compile_test_cases_with_measurements: conn.prepare("
570+
SELECT DISTINCT crate, profile, scenario, backend, target
571+
FROM pstat_series
572+
WHERE id IN (
573+
SELECT DISTINCT series
574+
FROM pstat
575+
WHERE aid = $1
576+
)
577+
").await.unwrap(),
567578
}),
568579
conn,
569580
}
@@ -1413,6 +1424,30 @@ where
14131424
.await
14141425
.unwrap();
14151426
}
1427+
1428+
async fn get_compile_test_cases_with_measurements(
1429+
&self,
1430+
artifact_row_id: &ArtifactIdNumber,
1431+
) -> anyhow::Result<HashSet<CompileTestCase>> {
1432+
let rows = self
1433+
.conn()
1434+
.query(
1435+
&self.statements().get_compile_test_cases_with_measurements,
1436+
&[&(artifact_row_id.0 as i32)],
1437+
)
1438+
.await
1439+
.context("cannot query compile-time test cases with measurements")?;
1440+
Ok(rows
1441+
.into_iter()
1442+
.map(|row| CompileTestCase {
1443+
benchmark: Benchmark::from(row.get::<_, &str>(0)),
1444+
profile: Profile::from_str(row.get::<_, &str>(1)).unwrap(),
1445+
scenario: row.get::<_, &str>(2).parse().unwrap(),
1446+
backend: CodegenBackend::from_str(row.get::<_, &str>(3)).unwrap(),
1447+
target: Target::from_str(row.get::<_, &str>(4)).unwrap(),
1448+
})
1449+
.collect())
1450+
}
14161451
}
14171452

14181453
fn parse_artifact_id(ty: &str, sha: &str, date: Option<DateTime<Utc>>) -> ArtifactId {

database/src/pool/sqlite.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::pool::{Connection, ConnectionManager, ManagedConnection, Transaction};
2+
use crate::selector::CompileTestCase;
23
use crate::{
34
ArtifactCollection, ArtifactId, Benchmark, BenchmarkRequest, CodegenBackend, CollectionId,
45
Commit, CommitType, CompileBenchmark, Date, Profile, Target,
56
};
67
use crate::{ArtifactIdNumber, Index, QueuedCommit};
78
use chrono::{DateTime, TimeZone, Utc};
8-
use hashbrown::HashMap;
9+
use hashbrown::{HashMap, HashSet};
910
use rusqlite::params;
1011
use rusqlite::OptionalExtension;
1112
use std::path::PathBuf;
@@ -1256,6 +1257,33 @@ impl Connection for SqliteConnection {
12561257
async fn insert_benchmark_request(&self, _benchmark_request: &BenchmarkRequest) {
12571258
panic!("Queueing for SQLite has not been implemented, if you are wanting to test the queueing functionality please use postgres. Presuming you have docker installed, at the root of the repo you can run `make start-postgres` to spin up a postgres database.");
12581259
}
1260+
1261+
async fn get_compile_test_cases_with_measurements(
1262+
&self,
1263+
artifact_row_id: &ArtifactIdNumber,
1264+
) -> anyhow::Result<HashSet<CompileTestCase>> {
1265+
Ok(self
1266+
.raw_ref()
1267+
.prepare_cached(
1268+
"SELECT DISTINCT crate, profile, scenario, backend, target
1269+
FROM pstat_series
1270+
WHERE id IN (
1271+
SELECT DISTINCT series
1272+
FROM pstat
1273+
WHERE aid = ?
1274+
);",
1275+
)?
1276+
.query_map(params![artifact_row_id.0], |row| {
1277+
Ok(CompileTestCase {
1278+
benchmark: Benchmark::from(row.get::<_, String>(0)?.as_str()),
1279+
profile: row.get::<_, String>(1)?.parse().unwrap(),
1280+
scenario: row.get::<_, String>(2)?.parse().unwrap(),
1281+
backend: row.get::<_, String>(3)?.parse().unwrap(),
1282+
target: row.get::<_, String>(4)?.parse().unwrap(),
1283+
})
1284+
})?
1285+
.collect::<Result<_, _>>()?)
1286+
}
12591287
}
12601288

12611289
fn parse_artifact_id(ty: &str, sha: &str, date: Option<i64>) -> ArtifactId {

0 commit comments

Comments
 (0)