Skip to content

Commit 3288e0e

Browse files
committed
PR feedback & tests
1 parent 307ecfb commit 3288e0e

File tree

10 files changed

+226
-141
lines changed

10 files changed

+226
-141
lines changed

database/src/lib.rs

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -799,28 +799,10 @@ pub struct ArtifactCollection {
799799
pub end_time: DateTime<Utc>,
800800
}
801801

802-
#[derive(Debug)]
803-
pub enum BenchmarkRequestCommitType {
804-
Try { pr: u32 },
805-
Master { pr: u32 },
806-
Release { tag: String },
807-
}
808-
809-
impl fmt::Display for BenchmarkRequestCommitType {
810-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
811-
match self {
812-
BenchmarkRequestCommitType::Try { pr: _ } => write!(f, "try"),
813-
BenchmarkRequestCommitType::Master { pr: _ } => write!(f, "master"),
814-
BenchmarkRequestCommitType::Release { tag: _ } => write!(f, "release"),
815-
}
816-
}
817-
}
818-
819802
#[derive(Debug)]
820803
pub enum BenchmarkRequestStatus {
821804
WaitingForArtifacts,
822805
WaitingForParent,
823-
Queued,
824806
InProgress,
825807
Completed,
826808
}
@@ -830,7 +812,6 @@ impl fmt::Display for BenchmarkRequestStatus {
830812
match self {
831813
BenchmarkRequestStatus::WaitingForArtifacts => write!(f, "waiting_for_artifacts"),
832814
BenchmarkRequestStatus::WaitingForParent => write!(f, "waiting_for_parent"),
833-
BenchmarkRequestStatus::Queued => write!(f, "queued"),
834815
BenchmarkRequestStatus::InProgress => write!(f, "in_progress"),
835816
BenchmarkRequestStatus::Completed => write!(f, "completed"),
836817
}
@@ -855,6 +836,24 @@ pub enum BenchmarkRequestType {
855836
Release { tag: String },
856837
}
857838

839+
impl BenchmarkRequestType {
840+
pub fn commit_type_str(&self) -> &str {
841+
match self {
842+
BenchmarkRequestType::Try {
843+
sha: _,
844+
parent_sha: _,
845+
pr: _,
846+
} => "try",
847+
BenchmarkRequestType::Master {
848+
sha: _,
849+
parent_sha: _,
850+
pr: _,
851+
} => "master",
852+
BenchmarkRequestType::Release { tag: _ } => "release",
853+
}
854+
}
855+
}
856+
858857
impl fmt::Display for BenchmarkRequestType {
859858
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
860859
match self {
@@ -948,4 +947,61 @@ impl BenchmarkRequest {
948947
profiles: profiles.to_string(),
949948
}
950949
}
950+
951+
/// Get either the `sha` for a `try` or `master` commit or a `tag` for a
952+
/// `release`
953+
pub fn tag(&self) -> &str {
954+
match &self.commit_type {
955+
BenchmarkRequestType::Try {
956+
sha,
957+
parent_sha: _,
958+
pr: _,
959+
}
960+
| BenchmarkRequestType::Master {
961+
sha,
962+
parent_sha: _,
963+
pr: _,
964+
} => sha,
965+
BenchmarkRequestType::Release { tag } => tag,
966+
}
967+
}
968+
969+
pub fn pr(&self) -> Option<&u32> {
970+
match &self.commit_type {
971+
BenchmarkRequestType::Try {
972+
sha: _,
973+
parent_sha: _,
974+
pr,
975+
}
976+
| BenchmarkRequestType::Master {
977+
sha: _,
978+
parent_sha: _,
979+
pr,
980+
} => Some(pr),
981+
BenchmarkRequestType::Release { tag: _ } => None,
982+
}
983+
}
984+
985+
pub fn commit_type(&self) -> &str {
986+
self.commit_type.commit_type_str()
987+
}
988+
989+
pub fn parent_sha(&self) -> Option<&str> {
990+
match &self.commit_type {
991+
BenchmarkRequestType::Try {
992+
sha: _,
993+
parent_sha,
994+
pr: _,
995+
}
996+
| BenchmarkRequestType::Master {
997+
sha: _,
998+
parent_sha,
999+
pr: _,
1000+
} => match parent_sha {
1001+
Some(parent_sha) => Some(parent_sha),
1002+
None => None,
1003+
},
1004+
BenchmarkRequestType::Release { tag: _ } => None,
1005+
}
1006+
}
9511007
}

database/src/pool.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ mod tests {
306306
use std::str::FromStr;
307307

308308
use super::*;
309-
use crate::{tests::run_db_test, Commit, CommitType, Date};
309+
use crate::{tests::run_db_test, BenchmarkRequestStatus, Commit, CommitType, Date};
310310

311311
/// Create a Commit
312312
fn create_commit(commit_sha: &str, time: chrono::DateTime<Utc>, r#type: CommitType) -> Commit {
@@ -375,4 +375,50 @@ mod tests {
375375
})
376376
.await;
377377
}
378+
379+
#[tokio::test]
380+
async fn insert_benchmark_requests() {
381+
run_db_test(|ctx| async {
382+
let db = ctx.db_client();
383+
let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
384+
let master_benchmark_request = BenchmarkRequest::create_master(
385+
"a-sha-1",
386+
Some("parent-sha-1"),
387+
42,
388+
time,
389+
BenchmarkRequestStatus::WaitingForParent,
390+
"llvm",
391+
"",
392+
);
393+
394+
let try_benchmark_request = BenchmarkRequest::create_try(
395+
"b-sha-2",
396+
Some("parent-sha-2"),
397+
32,
398+
time,
399+
BenchmarkRequestStatus::WaitingForParent,
400+
"cranelift",
401+
"",
402+
);
403+
404+
let release_benchmark_request = BenchmarkRequest::create_release(
405+
"1.8.0",
406+
time,
407+
BenchmarkRequestStatus::WaitingForParent,
408+
"cranelift,llvm",
409+
"",
410+
);
411+
412+
let db = db.connection().await;
413+
db.insert_benchmark_request(&master_benchmark_request).await;
414+
db.insert_benchmark_request(&try_benchmark_request).await;
415+
db.insert_benchmark_request(&release_benchmark_request)
416+
.await;
417+
// duplicate insert
418+
db.insert_benchmark_request(&master_benchmark_request).await;
419+
420+
Ok(ctx)
421+
})
422+
.await;
423+
}
378424
}

database/src/pool/postgres.rs

Lines changed: 38 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::pool::{Connection, ConnectionManager, ManagedConnection, Transaction};
22
use crate::{
3-
ArtifactCollection, ArtifactId, ArtifactIdNumber, Benchmark, BenchmarkRequest,
4-
BenchmarkRequestType, CodegenBackend, CollectionId, Commit, CommitType, CompileBenchmark, Date,
5-
Index, Profile, QueuedCommit, Scenario, Target,
3+
ArtifactCollection, ArtifactId, ArtifactIdNumber, Benchmark, BenchmarkRequest, CodegenBackend,
4+
CollectionId, Commit, CommitType, CompileBenchmark, Date, Index, Profile, QueuedCommit,
5+
Scenario, Target,
66
};
77
use anyhow::Context as _;
88
use chrono::{DateTime, TimeZone, Utc};
@@ -287,20 +287,20 @@ static MIGRATIONS: &[&str] = &[
287287
alter table pstat_series add constraint test_case UNIQUE(crate, profile, scenario, backend, target, metric);
288288
"#,
289289
r#"
290-
CREATE TABLE IF NOT EXISTS benchmark_requests (
290+
CREATE TABLE IF NOT EXISTS benchmark_request (
291291
id SERIAL PRIMARY KEY,
292292
tag TEXT NOT NULL UNIQUE,
293293
parent_sha TEXT,
294-
commit_type TEXT,
294+
commit_type TEXT NOT NULL,
295295
pr INTEGER,
296296
created_at TIMESTAMPTZ NOT NULL,
297297
completed_at TIMESTAMPTZ,
298298
status TEXT NOT NULL,
299-
backends TEXT,
300-
profiles TEXT
299+
backends TEXT NOT NULL,
300+
profiles TEXT NOT NULL
301301
);
302-
CREATE INDEX IF NOT EXISTS benchmark_requests_status_idx on benchmark_requests (status);
303-
CREATE INDEX IF NOT EXISTS benchmark_requests_commit_type on benchmark_requests (commit_type);
302+
CREATE INDEX IF NOT EXISTS benchmark_request_status_idx on benchmark_request (status);
303+
CREATE INDEX IF NOT EXISTS benchmark_request_commit_type on benchmark_request (commit_type);
304304
"#,
305305
];
306306

@@ -1384,63 +1384,35 @@ where
13841384
}
13851385

13861386
async fn insert_benchmark_request(&self, benchmark_request: &BenchmarkRequest) {
1387-
match &benchmark_request.commit_type {
1388-
BenchmarkRequestType::Try {
1389-
sha,
1390-
parent_sha,
1391-
pr,
1392-
}
1393-
| BenchmarkRequestType::Master {
1394-
sha,
1395-
parent_sha,
1396-
pr,
1397-
} => {
1398-
self.conn()
1399-
.execute(
1400-
r#"
1401-
INSERT INTO
1402-
benchmark_requests(parent_sha, pr, tag, created_at,
1403-
commit_type, status, backends, profiles)
1404-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
1405-
ON CONFLICT DO NOTHING;
1406-
"#,
1407-
&[
1408-
&parent_sha,
1409-
pr,
1410-
sha,
1411-
&benchmark_request.created_at,
1412-
&benchmark_request.commit_type.to_string(),
1413-
&benchmark_request.status.to_string(),
1414-
&benchmark_request.backends,
1415-
&benchmark_request.profiles,
1416-
],
1417-
)
1418-
.await
1419-
.unwrap();
1420-
}
1421-
BenchmarkRequestType::Release { tag } => {
1422-
self.conn()
1423-
.execute(
1424-
r#"
1425-
INSERT INTO
1426-
benchmark_requests(tag, created_at, commit_type,
1427-
status, backends, profiles)
1428-
VALUES ($1, $2, $3, $4, $5, $6);
1429-
ON CONFLICT DO NOTHING;
1430-
"#,
1431-
&[
1432-
tag,
1433-
&benchmark_request.created_at,
1434-
&benchmark_request.commit_type.to_string(),
1435-
&benchmark_request.status.to_string(),
1436-
&benchmark_request.backends,
1437-
&benchmark_request.profiles,
1438-
],
1439-
)
1440-
.await
1441-
.unwrap();
1442-
}
1443-
}
1387+
self.conn()
1388+
.execute(
1389+
r#"
1390+
INSERT INTO benchmark_request(
1391+
tag,
1392+
parent_sha,
1393+
pr,
1394+
commit_type,
1395+
status,
1396+
created_at,
1397+
backends,
1398+
profiles
1399+
)
1400+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
1401+
ON CONFLICT DO NOTHING;
1402+
"#,
1403+
&[
1404+
&benchmark_request.tag(),
1405+
&benchmark_request.parent_sha(),
1406+
&benchmark_request.pr().map(|it| *it as i32),
1407+
&benchmark_request.commit_type(),
1408+
&benchmark_request.status.to_string(),
1409+
&benchmark_request.created_at,
1410+
&benchmark_request.backends,
1411+
&benchmark_request.profiles,
1412+
],
1413+
)
1414+
.await
1415+
.unwrap();
14441416
}
14451417
}
14461418

database/src/pool/sqlite.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ impl Connection for SqliteConnection {
12541254
}
12551255

12561256
async fn insert_benchmark_request(&self, _benchmark_request: &BenchmarkRequest) {
1257-
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 postrgres database.");
1257+
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.");
12581258
}
12591259
}
12601260

database/src/tests/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ where
126126
let ctx = TestContext::new_postgres(&db_url).await;
127127
let ctx = f(ctx).await.expect("Postgres test failed");
128128
ctx.finish().await;
129+
// We don't want to run both postgres tests & sqlite tests as some tests
130+
// are only designed for postgres
131+
return;
129132
} else {
130133
// The github CI does not yet support running containers on Windows,
131134
// meaning that the test suite would fail.

site/src/job_queue.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::{str::FromStr, sync::Arc};
2+
3+
use crate::load::SiteCtxt;
4+
use chrono::Utc;
5+
use database::{BenchmarkRequest, BenchmarkRequestStatus};
6+
use parking_lot::RwLock;
7+
use tokio::time::{self, Duration};
8+
9+
impl SiteCtxt {
10+
/// Store the latest master commits or do nothing if all of them are
11+
/// already in the database
12+
pub async fn enqueue_master_commits(&self) {
13+
let conn = self.conn().await;
14+
let master_commits = &self.get_master_commits().commits;
15+
// TODO; delete at some point in the future
16+
let cutoff: chrono::DateTime<Utc> =
17+
chrono::DateTime::from_str("2025-06-01T00:00:00.000Z").unwrap();
18+
19+
for master_commit in master_commits {
20+
// We don't want to add masses of obsolete data
21+
if master_commit.time >= cutoff {
22+
let pr = master_commit.pr.unwrap_or(0);
23+
let benchmark = BenchmarkRequest::create_master(
24+
&master_commit.sha,
25+
Some(&master_commit.parent_sha),
26+
pr,
27+
master_commit.time,
28+
BenchmarkRequestStatus::WaitingForParent,
29+
"",
30+
"",
31+
);
32+
conn.insert_benchmark_request(&benchmark).await;
33+
}
34+
}
35+
}
36+
}
37+
38+
/// Inserts and manages the queue at `seconds` interval
39+
async fn cron_enqueue_jobs(site_ctxt: Arc<RwLock<Option<Arc<SiteCtxt>>>>, seconds: u64) {
40+
let mut interval = time::interval(Duration::from_secs(seconds));
41+
42+
loop {
43+
let ctxt = site_ctxt.clone();
44+
if let Some(ctxt_clone) = {
45+
let guard = ctxt.read();
46+
guard.as_ref().cloned()
47+
} {
48+
ctxt_clone.enqueue_master_commits().await;
49+
}
50+
51+
interval.tick().await;
52+
println!("Cron job executed at: {:?}", std::time::SystemTime::now());
53+
}
54+
}
55+
56+
/// Entry point for the cron
57+
pub async fn cron_main(site_ctxt: Arc<RwLock<Option<Arc<SiteCtxt>>>>, seconds: u64) {
58+
cron_enqueue_jobs(site_ctxt, seconds).await;
59+
}

0 commit comments

Comments
 (0)