Skip to content

Commit f045fac

Browse files
committed
Implement runtime benchmark DB structure for Postgre
1 parent 7051619 commit f045fac

File tree

5 files changed

+473
-334
lines changed

5 files changed

+473
-334
lines changed

collector/src/runtime/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ pub async fn bench_runtime(
2626
filter: BenchmarkFilter,
2727
iterations: u32,
2828
) -> anyhow::Result<()> {
29-
for benchmark in suite.benchmark_names() {
30-
conn.record_runtime_benchmark(benchmark).await;
31-
}
32-
3329
let total_benchmark_count = suite.total_benchmark_count();
3430
let filtered = suite.filtered_benchmark_count(&filter);
3531
println!(

database/src/bin/import-sqlite.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,20 @@ async fn main() {
7979
.await;
8080
}
8181
}
82+
83+
for (&(benchmark, metric), id) in sqlite_idx.runtime_statistic_descriptions() {
84+
let stat = sqlite_conn
85+
.get_runtime_pstats(&[id], &[Some(sqlite_aid)])
86+
.await
87+
.pop()
88+
.unwrap()
89+
.pop()
90+
.unwrap();
91+
if let Some(stat) = stat {
92+
postgres_conn
93+
.record_runtime_statistic(cid, postgres_aid, &benchmark, metric.as_str(), stat)
94+
.await;
95+
}
96+
}
8297
}
8398
}

database/src/pool.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ pub trait Connection: Send + Sync {
2626
);
2727
async fn get_compile_benchmarks(&self) -> Vec<CompileBenchmark>;
2828

29-
async fn record_runtime_benchmark(&self, name: &str);
30-
3129
async fn artifact_by_name(&self, artifact: &str) -> Option<ArtifactId>;
3230

3331
/// This records the duration of a collection run, i.e., collecting all of
@@ -112,6 +110,11 @@ pub trait Connection: Send + Sync {
112110
pstat_series_row_ids: &[u32],
113111
artifact_row_id: &[Option<ArtifactIdNumber>],
114112
) -> Vec<Vec<Option<f64>>>;
113+
async fn get_runtime_pstats(
114+
&self,
115+
runtime_pstat_series_row_ids: &[u32],
116+
artifact_row_id: &[Option<ArtifactIdNumber>],
117+
) -> Vec<Vec<Option<f64>>>;
115118
async fn get_error(&self, artifact_row_id: ArtifactIdNumber) -> HashMap<String, String>;
116119

117120
async fn queue_pr(

database/src/pool/postgres.rs

Lines changed: 114 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,21 @@ static MIGRATIONS: &[&str] = &[
215215
r#"
216216
alter table pull_request_build add column commit_date timestamptz;
217217
"#,
218+
r#"
219+
create table runtime_pstat_series(
220+
id integer primary key generated always as identity,
221+
benchmark text not null,
222+
metric text not null,
223+
UNIQUE(benchmark, metric)
224+
);
225+
create table runtime_pstat(
226+
series integer references runtime_pstat_series(id) on delete cascade on update cascade,
227+
aid integer references artifact(id) on delete cascade on update cascade,
228+
cid integer references collection(id) on delete cascade on update cascade,
229+
value double precision not null,
230+
PRIMARY KEY(series, aid, cid)
231+
);
232+
"#,
218233
];
219234

220235
#[async_trait::async_trait]
@@ -293,6 +308,10 @@ pub struct CachedStatements {
293308
record_duration: Statement,
294309
in_progress_steps: Statement,
295310
get_benchmarks: Statement,
311+
insert_runtime_pstat_series: Statement,
312+
select_runtime_pstat_series: Statement,
313+
insert_runtime_pstat: Statement,
314+
get_runtime_pstat: Statement,
296315
}
297316

298317
pub struct PostgresTransaction<'a> {
@@ -449,7 +468,36 @@ impl PostgresConnection {
449468
get_benchmarks: conn.prepare("
450469
select name, category
451470
from benchmark
452-
").await.unwrap()
471+
").await.unwrap(),
472+
insert_runtime_pstat_series: conn.prepare("insert into runtime_pstat_series (benchmark, metric) VALUES ($1, $2) ON CONFLICT DO NOTHING RETURNING id").await.unwrap(),
473+
select_runtime_pstat_series: conn.prepare("select id from runtime_pstat_series where benchmark = $1 and metric = $2").await.unwrap(),
474+
insert_runtime_pstat: conn
475+
.prepare("insert into runtime_pstat (series, aid, cid, value) VALUES ($1, $2, $3, $4)")
476+
.await
477+
.unwrap(),
478+
get_runtime_pstat: conn
479+
.prepare("
480+
WITH aids AS (
481+
select aid, num from unnest($2::int[]) with ordinality aids(aid, num)
482+
),
483+
sids AS (
484+
select sid, idx from unnest($1::int[]) with ordinality sids(sid, idx)
485+
)
486+
select ARRAY(
487+
(
488+
select min(runtime_pstat.value) from aids
489+
left outer join runtime_pstat
490+
on (aids.aid = runtime_pstat.aid and runtime_pstat.series = sids.sid)
491+
group by aids.num
492+
order by aids.num
493+
)
494+
) from
495+
sids
496+
group by (sids.idx, sids.sid)
497+
order by sids.idx
498+
")
499+
.await
500+
.unwrap()
453501
}),
454502
conn,
455503
}
@@ -616,6 +664,31 @@ where
616664
.map(|row| row.get::<_, Vec<Option<f64>>>(0))
617665
.collect()
618666
}
667+
async fn get_runtime_pstats(
668+
&self,
669+
runtime_pstat_series_row_ids: &[u32],
670+
artifact_row_ids: &[Option<crate::ArtifactIdNumber>],
671+
) -> Vec<Vec<Option<f64>>> {
672+
let runtime_pstat_series_row_ids = runtime_pstat_series_row_ids
673+
.iter()
674+
.map(|sid| *sid as i32)
675+
.collect::<Vec<_>>();
676+
let artifact_row_ids = artifact_row_ids
677+
.iter()
678+
.map(|id| id.map(|id| id.0 as i32))
679+
.collect::<Vec<_>>();
680+
let rows = self
681+
.conn()
682+
.query(
683+
&self.statements().get_runtime_pstat,
684+
&[&runtime_pstat_series_row_ids, &artifact_row_ids],
685+
)
686+
.await
687+
.unwrap();
688+
rows.into_iter()
689+
.map(|row| row.get::<_, Vec<Option<f64>>>(0))
690+
.collect()
691+
}
619692
async fn get_error(&self, artifact_row_id: crate::ArtifactIdNumber) -> HashMap<String, String> {
620693
let rows = self
621694
.conn()
@@ -763,13 +836,47 @@ where
763836
}
764837
async fn record_runtime_statistic(
765838
&self,
766-
_collection: CollectionId,
767-
_artifact: ArtifactIdNumber,
768-
_benchmark: &str,
769-
_metric: &str,
770-
_value: f64,
839+
collection: CollectionId,
840+
artifact: ArtifactIdNumber,
841+
benchmark: &str,
842+
metric: &str,
843+
value: f64,
771844
) {
772-
unimplemented!()
845+
let sid = self
846+
.conn()
847+
.query_opt(
848+
&self.statements().select_runtime_pstat_series,
849+
&[&benchmark, &metric],
850+
)
851+
.await
852+
.unwrap();
853+
let sid: i32 = match sid {
854+
Some(id) => id.get(0),
855+
None => {
856+
self.conn()
857+
.query_opt(
858+
&self.statements().insert_runtime_pstat_series,
859+
&[&benchmark, &metric],
860+
)
861+
.await
862+
.unwrap();
863+
self.conn()
864+
.query_one(
865+
&self.statements().select_runtime_pstat_series,
866+
&[&benchmark, &metric],
867+
)
868+
.await
869+
.unwrap()
870+
.get(0)
871+
}
872+
};
873+
self.conn()
874+
.execute(
875+
&self.statements().insert_runtime_pstat,
876+
&[&sid, &(artifact.0 as i32), &{ collection.0 }, &value],
877+
)
878+
.await
879+
.unwrap();
773880
}
774881

775882
async fn record_rustc_crate(
@@ -954,9 +1061,6 @@ where
9541061
.unwrap();
9551062
}
9561063
}
957-
async fn record_runtime_benchmark(&self, _name: &str) {
958-
unimplemented!()
959-
}
9601064

9611065
async fn collector_start(&self, aid: ArtifactIdNumber, steps: &[String]) {
9621066
// Clean up -- we'll re-insert any missing things in the loop below.

0 commit comments

Comments
 (0)