Skip to content

Commit fb9d060

Browse files
committed
Created AddCollector command line option and created more tests for config and dequeuing a job
1 parent d4cf6a1 commit fb9d060

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

@@ -666,6 +667,24 @@ enum Commands {
666667
modified: Option<String>,
667668
},
668669

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

1301+
Commands::AddCollector {
1302+
db,
1303+
collector_name,
1304+
target,
1305+
is_active,
1306+
benchmark_set,
1307+
} => {
1308+
let pool = Pool::open(&db.db);
1309+
let rt = build_async_runtime();
1310+
let conn = rt.block_on(pool.connection());
1311+
1312+
let target = database::Target::from_str(&target).map_err(|e| anyhow::anyhow!(e))?;
1313+
rt.block_on(conn.add_collector_config(
1314+
&collector_name,
1315+
&target,
1316+
benchmark_set,
1317+
is_active,
1318+
))?;
1319+
Ok(0)
1320+
}
1321+
12821322
Commands::DequeueJob {
12831323
collector_name,
12841324
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
@@ -222,6 +222,15 @@ pub trait Connection: Send + Sync {
222222
benchmark_set: u32,
223223
) -> anyhow::Result<()>;
224224

225+
/// Add the confiuguration for a collector
226+
async fn add_collector_config(
227+
&self,
228+
collector_name: &str,
229+
target: &Target,
230+
benchmark_set: u32,
231+
is_active: bool,
232+
) -> anyhow::Result<CollectorConfig>;
233+
225234
/// Get the confiuguration for a collector by the name of the collector
226235
async fn get_collector_config(&self, collector_name: &str) -> anyhow::Result<CollectorConfig>;
227236

@@ -652,6 +661,27 @@ mod tests {
652661
.await;
653662
}
654663

664+
#[tokio::test]
665+
async fn add_collector_config() {
666+
run_postgres_test(|ctx| async {
667+
let db = ctx.db_client().connection().await;
668+
669+
let insert_config_result = db
670+
.add_collector_config("collector-1", &Target::X86_64UnknownLinuxGnu, 1, true)
671+
.await;
672+
assert!(insert_config_result.is_ok());
673+
674+
let get_config_result = db.get_collector_config("collector-1").await;
675+
assert!(get_config_result.is_ok());
676+
677+
// What we entered into the database should be identical to what is
678+
// returned from the database
679+
assert_eq!(insert_config_result.unwrap(), get_config_result.unwrap());
680+
Ok(ctx)
681+
})
682+
.await;
683+
}
684+
655685
#[tokio::test]
656686
async fn dequeue_benchmark_job_empty_queue() {
657687
run_postgres_test(|ctx| async {
@@ -672,4 +702,70 @@ mod tests {
672702
})
673703
.await;
674704
}
705+
706+
#[tokio::test]
707+
async fn dequeue_benchmark_job() {
708+
run_postgres_test(|ctx| async {
709+
let db = ctx.db_client().connection().await;
710+
let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
711+
712+
let insert_result = db
713+
.add_collector_config("collector-1", &Target::X86_64UnknownLinuxGnu, 1, true)
714+
.await;
715+
assert!(insert_result.is_ok());
716+
717+
let collector_config = insert_result.unwrap();
718+
719+
let benchmark_request =
720+
BenchmarkRequest::create_master("sha-1", "parent-sha-1", 42, time);
721+
722+
// Insert the request so we don't violate the foreign key
723+
db.insert_benchmark_request(&benchmark_request)
724+
.await
725+
.unwrap();
726+
727+
// Now we can insert the job
728+
let enqueue_result = db
729+
.enqueue_benchmark_job(
730+
benchmark_request.tag().unwrap(),
731+
&Target::X86_64UnknownLinuxGnu,
732+
&CodegenBackend::Llvm,
733+
&Profile::Opt,
734+
1u32,
735+
)
736+
.await;
737+
assert!(enqueue_result.is_ok());
738+
739+
let benchmark_job = db
740+
.dequeue_benchmark_job(
741+
collector_config.name(),
742+
collector_config.target(),
743+
collector_config.benchmark_set(),
744+
)
745+
.await;
746+
assert!(benchmark_job.is_ok());
747+
748+
let benchmark_job = benchmark_job.unwrap();
749+
assert!(benchmark_job.is_some());
750+
751+
// Ensure the properties of the job match both the request and the
752+
// collector configuration
753+
let benchmark_job = benchmark_job.unwrap();
754+
assert_eq!(
755+
benchmark_job.request_tag(),
756+
benchmark_request.tag().unwrap()
757+
);
758+
assert_eq!(
759+
benchmark_job.benchmark_set(),
760+
collector_config.benchmark_set()
761+
);
762+
assert_eq!(
763+
benchmark_job.collector_name().unwrap(),
764+
collector_config.name(),
765+
);
766+
767+
Ok(ctx)
768+
})
769+
.await;
770+
}
675771
}

database/src/pool/postgres.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,56 @@ where
16951695
Ok(())
16961696
}
16971697

1698+
async fn add_collector_config(
1699+
&self,
1700+
collector_name: &str,
1701+
target: &Target,
1702+
benchmark_set: u32,
1703+
is_active: bool,
1704+
) -> anyhow::Result<CollectorConfig> {
1705+
let row = self
1706+
.conn()
1707+
.query_one(
1708+
"INSERT INTO collector_config(
1709+
name,
1710+
target,
1711+
date_added,
1712+
last_heartbeat_at,
1713+
benchmark_set,
1714+
is_active
1715+
) VALUES (
1716+
$1,
1717+
$2,
1718+
NOW(),
1719+
NOW(),
1720+
$3,
1721+
$4
1722+
)
1723+
RETURNING
1724+
last_heartbeat_at,
1725+
date_added
1726+
",
1727+
&[
1728+
&collector_name,
1729+
&target,
1730+
&(benchmark_set as i32),
1731+
&is_active,
1732+
],
1733+
)
1734+
.await
1735+
.context("failed to create collector config")?;
1736+
1737+
let collector_config = CollectorConfig {
1738+
name: collector_name.into(),
1739+
target: *target,
1740+
benchmark_set: BenchmarkSet(benchmark_set),
1741+
is_active,
1742+
last_heartbeat_at: row.get::<_, DateTime<Utc>>(0),
1743+
date_added: row.get::<_, DateTime<Utc>>(1),
1744+
};
1745+
Ok(collector_config)
1746+
}
1747+
16981748
async fn get_collector_config(&self, collector_name: &str) -> anyhow::Result<CollectorConfig> {
16991749
let row = self
17001750
.conn()
@@ -1705,11 +1755,10 @@ where
17051755
is_active,
17061756
last_heartbeat_at,
17071757
date_added
1708-
17091758
FROM
17101759
collector_config
17111760
WHERE
1712-
collector_name = $1;",
1761+
name = $1;",
17131762
&[&collector_name],
17141763
)
17151764
.await?;

database/src/pool/sqlite.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,16 @@ impl Connection for SqliteConnection {
13201320
) -> anyhow::Result<Option<BenchmarkJob>> {
13211321
no_queue_implementation_abort!()
13221322
}
1323+
1324+
async fn add_collector_config(
1325+
&self,
1326+
_collector_name: &str,
1327+
_target: &Target,
1328+
_benchmark_set: u32,
1329+
_is_active: bool,
1330+
) -> anyhow::Result<CollectorConfig> {
1331+
no_queue_implementation_abort!()
1332+
}
13231333
}
13241334

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

0 commit comments

Comments
 (0)