Skip to content

Commit 296f585

Browse files
committed
Created AddCollector command line option and created more tests for config and dequeuing a job
1 parent 57fb0f5 commit 296f585

File tree

6 files changed

+229
-2
lines changed

6 files changed

+229
-2
lines changed

collector/src/bin/collector.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::marker::PhantomData;
1212
use std::path::{Path, PathBuf};
1313
use std::process;
1414
use std::process::Command;
15+
use std::str::FromStr;
1516
use std::time::Duration;
1617
use std::{str, time::Instant};
1718

@@ -668,6 +669,24 @@ enum Commands {
668669
modified: Option<String>,
669670
},
670671

672+
/// Registers a collector in the database
673+
AddCollector {
674+
#[command(flatten)]
675+
db: DbOption,
676+
677+
#[arg(long)]
678+
collector_name: String,
679+
680+
#[arg(long)]
681+
target: String,
682+
683+
#[arg(long)]
684+
is_active: bool,
685+
686+
#[arg(long)]
687+
benchmark_set: u32,
688+
},
689+
671690
/// Polls the job queue for work to benchmark
672691
DequeueJob {
673692
/// The unique identifier for the collector
@@ -1281,6 +1300,27 @@ Make sure to modify `{dir}/perf-config.json` if the category/artifact don't matc
12811300
Ok(0)
12821301
}
12831302

1303+
Commands::AddCollector {
1304+
db,
1305+
collector_name,
1306+
target,
1307+
is_active,
1308+
benchmark_set,
1309+
} => {
1310+
let pool = Pool::open(&db.db);
1311+
let rt = build_async_runtime();
1312+
let conn = rt.block_on(pool.connection());
1313+
1314+
let target = database::Target::from_str(&target).map_err(|e| anyhow::anyhow!(e))?;
1315+
rt.block_on(conn.add_collector_config(
1316+
&collector_name,
1317+
&target,
1318+
benchmark_set,
1319+
is_active,
1320+
))?;
1321+
Ok(0)
1322+
}
1323+
12841324
Commands::DequeueJob {
12851325
collector_name,
12861326
db,

collector/src/compile/benchmark/collector_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use database::BenchmarkSet;
33

44
use super::target::Target;
55

6+
#[derive(Debug, Clone, PartialEq)]
67
pub struct CollectorConfig {
78
name: String,
89
target: Target,

database/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,38 @@ pub struct BenchmarkJob {
11121112
retry: u32,
11131113
}
11141114

1115+
impl BenchmarkJob {
1116+
pub fn target(&self) -> &Target {
1117+
&self.target
1118+
}
1119+
1120+
pub fn backend(&self) -> &CodegenBackend {
1121+
&self.backend
1122+
}
1123+
1124+
pub fn profile(&self) -> &Profile {
1125+
&self.profile
1126+
}
1127+
1128+
pub fn request_tag(&self) -> &str {
1129+
&self.request_tag
1130+
}
1131+
1132+
pub fn benchmark_set(&self) -> &BenchmarkSet {
1133+
&self.benchmark_set
1134+
}
1135+
1136+
pub fn collector_name(&self) -> Option<&str> {
1137+
match &self.status {
1138+
BenchmarkJobStatus::Queued => None,
1139+
BenchmarkJobStatus::InProgress { collector_name, .. }
1140+
| BenchmarkJobStatus::Completed { collector_name, .. } => Some(collector_name),
1141+
}
1142+
}
1143+
}
1144+
11151145
/// The configuration for a collector
1146+
#[derive(Debug, PartialEq)]
11161147
pub struct CollectorConfig {
11171148
name: String,
11181149
target: Target,

database/src/pool.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ pub trait Connection: Send + Sync {
234234
artifact_row_id: &ArtifactIdNumber,
235235
) -> anyhow::Result<HashSet<CompileTestCase>>;
236236

237+
/// Add the confiuguration for a collector
238+
async fn add_collector_config(
239+
&self,
240+
collector_name: &str,
241+
target: &Target,
242+
benchmark_set: u32,
243+
is_active: bool,
244+
) -> anyhow::Result<CollectorConfig>;
245+
237246
/// Get the confiuguration for a collector by the name of the collector
238247
async fn get_collector_config(&self, collector_name: &str) -> anyhow::Result<CollectorConfig>;
239248

@@ -720,6 +729,27 @@ mod tests {
720729
.await;
721730
}
722731

732+
#[tokio::test]
733+
async fn add_collector_config() {
734+
run_postgres_test(|ctx| async {
735+
let db = ctx.db_client().connection().await;
736+
737+
let insert_config_result = db
738+
.add_collector_config("collector-1", &Target::X86_64UnknownLinuxGnu, 1, true)
739+
.await;
740+
assert!(insert_config_result.is_ok());
741+
742+
let get_config_result = db.get_collector_config("collector-1").await;
743+
assert!(get_config_result.is_ok());
744+
745+
// What we entered into the database should be identical to what is
746+
// returned from the database
747+
assert_eq!(insert_config_result.unwrap(), get_config_result.unwrap());
748+
Ok(ctx)
749+
})
750+
.await;
751+
}
752+
723753
#[tokio::test]
724754
async fn dequeue_benchmark_job_empty_queue() {
725755
run_postgres_test(|ctx| async {
@@ -740,4 +770,70 @@ mod tests {
740770
})
741771
.await;
742772
}
773+
774+
#[tokio::test]
775+
async fn dequeue_benchmark_job() {
776+
run_postgres_test(|ctx| async {
777+
let db = ctx.db_client().connection().await;
778+
let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
779+
780+
let insert_result = db
781+
.add_collector_config("collector-1", &Target::X86_64UnknownLinuxGnu, 1, true)
782+
.await;
783+
assert!(insert_result.is_ok());
784+
785+
let collector_config = insert_result.unwrap();
786+
787+
let benchmark_request =
788+
BenchmarkRequest::create_master("sha-1", "parent-sha-1", 42, time);
789+
790+
// Insert the request so we don't violate the foreign key
791+
db.insert_benchmark_request(&benchmark_request)
792+
.await
793+
.unwrap();
794+
795+
// Now we can insert the job
796+
let enqueue_result = db
797+
.enqueue_benchmark_job(
798+
benchmark_request.tag().unwrap(),
799+
&Target::X86_64UnknownLinuxGnu,
800+
&CodegenBackend::Llvm,
801+
&Profile::Opt,
802+
1u32,
803+
)
804+
.await;
805+
assert!(enqueue_result.is_ok());
806+
807+
let benchmark_job = db
808+
.dequeue_benchmark_job(
809+
collector_config.name(),
810+
collector_config.target(),
811+
collector_config.benchmark_set(),
812+
)
813+
.await;
814+
assert!(benchmark_job.is_ok());
815+
816+
let benchmark_job = benchmark_job.unwrap();
817+
assert!(benchmark_job.is_some());
818+
819+
// Ensure the properties of the job match both the request and the
820+
// collector configuration
821+
let benchmark_job = benchmark_job.unwrap();
822+
assert_eq!(
823+
benchmark_job.request_tag(),
824+
benchmark_request.tag().unwrap()
825+
);
826+
assert_eq!(
827+
benchmark_job.benchmark_set(),
828+
collector_config.benchmark_set()
829+
);
830+
assert_eq!(
831+
benchmark_job.collector_name().unwrap(),
832+
collector_config.name(),
833+
);
834+
835+
Ok(ctx)
836+
})
837+
.await;
838+
}
743839
}

database/src/pool/postgres.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,56 @@ where
17251725
.collect())
17261726
}
17271727

1728+
async fn add_collector_config(
1729+
&self,
1730+
collector_name: &str,
1731+
target: &Target,
1732+
benchmark_set: u32,
1733+
is_active: bool,
1734+
) -> anyhow::Result<CollectorConfig> {
1735+
let row = self
1736+
.conn()
1737+
.query_one(
1738+
"INSERT INTO collector_config(
1739+
name,
1740+
target,
1741+
date_added,
1742+
last_heartbeat_at,
1743+
benchmark_set,
1744+
is_active
1745+
) VALUES (
1746+
$1,
1747+
$2,
1748+
NOW(),
1749+
NOW(),
1750+
$3,
1751+
$4
1752+
)
1753+
RETURNING
1754+
last_heartbeat_at,
1755+
date_added
1756+
",
1757+
&[
1758+
&collector_name,
1759+
&target,
1760+
&(benchmark_set as i32),
1761+
&is_active,
1762+
],
1763+
)
1764+
.await
1765+
.context("failed to create collector config")?;
1766+
1767+
let collector_config = CollectorConfig {
1768+
name: collector_name.into(),
1769+
target: *target,
1770+
benchmark_set: BenchmarkSet(benchmark_set),
1771+
is_active,
1772+
last_heartbeat_at: row.get::<_, DateTime<Utc>>(0),
1773+
date_added: row.get::<_, DateTime<Utc>>(1),
1774+
};
1775+
Ok(collector_config)
1776+
}
1777+
17281778
async fn get_collector_config(&self, collector_name: &str) -> anyhow::Result<CollectorConfig> {
17291779
let row = self
17301780
.conn()
@@ -1735,11 +1785,10 @@ where
17351785
is_active,
17361786
last_heartbeat_at,
17371787
date_added
1738-
17391788
FROM
17401789
collector_config
17411790
WHERE
1742-
collector_name = $1;",
1791+
name = $1;",
17431792
&[&collector_name],
17441793
)
17451794
.await?;

database/src/pool/sqlite.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,16 @@ impl Connection for SqliteConnection {
13431343
) -> anyhow::Result<Option<BenchmarkJob>> {
13441344
no_queue_implementation_abort!()
13451345
}
1346+
1347+
async fn add_collector_config(
1348+
&self,
1349+
_collector_name: &str,
1350+
_target: &Target,
1351+
_benchmark_set: u32,
1352+
_is_active: bool,
1353+
) -> anyhow::Result<CollectorConfig> {
1354+
no_queue_implementation_abort!()
1355+
}
13461356
}
13471357

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

0 commit comments

Comments
 (0)