Skip to content

Commit dfc63ee

Browse files
authored
Merge pull request #2220 from Jamesbarford/feat/completed-at-not-derived
Feat; add a duration column for benchmark_request's
2 parents cfcd625 + 43c0c4b commit dfc63ee

File tree

3 files changed

+55
-33
lines changed

3 files changed

+55
-33
lines changed

database/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,10 @@ pub enum BenchmarkRequestStatus {
812812
WaitingForArtifacts,
813813
ArtifactsReady,
814814
InProgress,
815-
Completed { completed_at: DateTime<Utc> },
815+
Completed {
816+
completed_at: DateTime<Utc>,
817+
duration: Duration,
818+
},
816819
}
817820

818821
const BENCHMARK_REQUEST_STATUS_WAITING_FOR_ARTIFACTS_STR: &str = "waiting_for_artifacts";
@@ -833,6 +836,7 @@ impl BenchmarkRequestStatus {
833836
pub(crate) fn from_str_and_completion_date(
834837
text: &str,
835838
completion_date: Option<DateTime<Utc>>,
839+
duration_ms: Option<i32>,
836840
) -> anyhow::Result<Self> {
837841
match text {
838842
BENCHMARK_REQUEST_STATUS_WAITING_FOR_ARTIFACTS_STR => Ok(Self::WaitingForArtifacts),
@@ -842,6 +846,9 @@ impl BenchmarkRequestStatus {
842846
completed_at: completion_date.ok_or_else(|| {
843847
anyhow!("No completion date for a completed BenchmarkRequestStatus")
844848
})?,
849+
duration: Duration::from_millis(duration_ms.ok_or_else(|| {
850+
anyhow!("No completion duration for a completed BenchmarkRequestStatus")
851+
})? as u64),
845852
}),
846853
_ => Err(anyhow!("Unknown BenchmarkRequestStatus `{text}`")),
847854
}
@@ -1221,6 +1228,8 @@ impl CollectorConfig {
12211228
/// status page
12221229
#[derive(Debug, PartialEq)]
12231230
pub struct PartialStatusPageData {
1224-
pub completed_requests: Vec<(BenchmarkRequest, String, Vec<String>)>,
1231+
/// A Vector of; completed requests with any associated errors
1232+
pub completed_requests: Vec<(BenchmarkRequest, Vec<String>)>,
1233+
/// In progress requests along with their associated jobs
12251234
pub in_progress: Vec<(BenchmarkRequest, Vec<BenchmarkJob>)>,
12261235
}

database/src/pool.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -973,16 +973,38 @@ mod tests {
973973

974974
assert_eq!(job.request_tag(), benchmark_request.tag().unwrap());
975975

976+
/* Make the job take some amount of time */
977+
std::thread::sleep(Duration::from_millis(1000));
978+
976979
/* Mark the job as complete */
977980
db.mark_benchmark_job_as_completed(job.id(), BenchmarkJobConclusion::Success)
978981
.await
979982
.unwrap();
980983

981984
db.mark_benchmark_request_as_completed(tag).await.unwrap();
982985

983-
let completed = db.load_benchmark_request_index().await.unwrap();
986+
/* From the status page view we can see that the duration has been
987+
* updated. Albeit that it will be a very short duration. */
988+
let status_page_view = db.get_status_page_data().await.unwrap();
989+
let req = &status_page_view
990+
.completed_requests
991+
.iter()
992+
.find(|it| it.0.tag() == Some(tag))
993+
.unwrap()
994+
.0;
995+
996+
assert!(matches!(
997+
req.status(),
998+
BenchmarkRequestStatus::Completed { .. }
999+
));
1000+
let BenchmarkRequestStatus::Completed { duration, .. } = req.status() else {
1001+
unreachable!();
1002+
};
1003+
assert!(duration >= Duration::from_millis(1000));
1004+
1005+
let completed_index = db.load_benchmark_request_index().await.unwrap();
1006+
assert!(completed_index.contains_tag("sha-1"));
9841007

985-
assert!(completed.contains_tag("sha-1"));
9861008
Ok(ctx)
9871009
})
9881010
.await;
@@ -1059,11 +1081,11 @@ mod tests {
10591081
// can't really test duration
10601082
// ensure errors are correct
10611083
assert_eq!(
1062-
status_page_data.completed_requests[0].2[0],
1084+
status_page_data.completed_requests[0].1[0],
10631085
"This is an error".to_string()
10641086
);
10651087
assert_eq!(
1066-
status_page_data.completed_requests[0].2[1],
1088+
status_page_data.completed_requests[0].1[1],
10671089
"This is another error".to_string()
10681090
);
10691091

database/src/pool/postgres.rs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ static MIGRATIONS: &[&str] = &[
381381
r#"
382382
ALTER TABLE benchmark_request ADD COLUMN commit_date TIMESTAMPTZ NULL;
383383
"#,
384+
r#"
385+
ALTER TABLE benchmark_request ADD COLUMN duration_ms INTEGER NULL;
386+
"#,
384387
];
385388

386389
#[async_trait::async_trait]
@@ -667,7 +670,7 @@ impl PostgresConnection {
667670
}
668671

669672
const BENCHMARK_REQUEST_COLUMNS: &str =
670-
"tag, parent_sha, pr, commit_type, status, created_at, completed_at, backends, profiles, commit_date";
673+
"tag, parent_sha, pr, commit_type, status, created_at, completed_at, backends, profiles, commit_date, duration_ms";
671674

672675
#[async_trait::async_trait]
673676
impl<P> Connection for P
@@ -1891,7 +1894,14 @@ where
18911894
benchmark_request
18921895
SET
18931896
status = $1,
1894-
completed_at = NOW()
1897+
completed_at = NOW(),
1898+
duration_ms = (
1899+
SELECT (MAX(EXTRACT('epoch' FROM job_queue.completed_at)) -
1900+
MIN(EXTRACT('epoch' FROM job_queue.started_at))) * 1000 AS duration_ms
1901+
FROM
1902+
job_queue
1903+
WHERE job_queue.request_tag = $2
1904+
)
18951905
WHERE
18961906
benchmark_request.tag = $2
18971907
AND benchmark_request.status != $1
@@ -2020,22 +2030,6 @@ where
20202030
ORDER BY
20212031
completed_at
20222032
DESC LIMIT {max_completed_requests}
2023-
), jobs AS (
2024-
SELECT
2025-
completed.tag,
2026-
job_queue.started_at,
2027-
job_queue.completed_at
2028-
FROM
2029-
job_queue
2030-
LEFT JOIN completed ON job_queue.request_tag = completed.tag
2031-
), stats AS (
2032-
SELECT
2033-
tag,
2034-
MAX(jobs.completed_at - jobs.started_at) AS duration
2035-
FROM
2036-
jobs
2037-
GROUP BY
2038-
tag
20392033
), artifacts AS (
20402034
SELECT
20412035
artifact.id,
@@ -2056,11 +2050,9 @@ where
20562050
)
20572051
SELECT
20582052
completed.*,
2059-
stats.duration::TEXT,
20602053
errors.errors AS errors
20612054
FROM
20622055
completed
2063-
LEFT JOIN stats ON stats.tag = completed.tag
20642056
LEFT JOIN errors ON errors.tag = completed.tag;
20652057
"
20662058
);
@@ -2081,19 +2073,16 @@ where
20812073
})
20822074
.collect();
20832075

2084-
let completed_requests: Vec<(BenchmarkRequest, String, Vec<String>)> = self
2076+
let completed_requests: Vec<(BenchmarkRequest, Vec<String>)> = self
20852077
.conn()
20862078
.query(&completed_requests_query, &[])
20872079
.await?
20882080
.iter()
20892081
.map(|it| {
20902082
(
20912083
row_to_benchmark_request(it),
2092-
// Duration being a string feels odd, but we don't need to
2093-
// perform any computations on it
2094-
it.get::<_, String>(10),
20952084
// The errors, if there are none this will be an empty vector
2096-
it.get::<_, Vec<String>>(11),
2085+
it.get::<_, Option<Vec<String>>>(11).unwrap_or_default(),
20972086
)
20982087
})
20992088
.collect();
@@ -2217,11 +2206,13 @@ fn row_to_benchmark_request(row: &Row) -> BenchmarkRequest {
22172206
let backends = row.get::<_, String>(7);
22182207
let profiles = row.get::<_, String>(8);
22192208
let commit_date = row.get::<_, Option<DateTime<Utc>>>(9);
2209+
let duration_ms = row.get::<_, Option<i32>>(10);
22202210

22212211
let pr = pr.map(|v| v as u32);
22222212

2223-
let status = BenchmarkRequestStatus::from_str_and_completion_date(status, completed_at)
2224-
.expect("Invalid BenchmarkRequestStatus data in the database");
2213+
let status =
2214+
BenchmarkRequestStatus::from_str_and_completion_date(status, completed_at, duration_ms)
2215+
.expect("Invalid BenchmarkRequestStatus data in the database");
22252216

22262217
match commit_type {
22272218
BENCHMARK_REQUEST_TRY_STR => BenchmarkRequest {

0 commit comments

Comments
 (0)