Skip to content

Commit 43d74a7

Browse files
authored
Merge pull request #2212 from Jamesbarford/feat/collector-dequeue-job
Feat; collector dequeue job
2 parents 98ca3ce + 52bfe1f commit 43d74a7

File tree

6 files changed

+539
-15
lines changed

6 files changed

+539
-15
lines changed

collector/src/bin/collector.rs

Lines changed: 89 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,37 @@ enum Commands {
666667
/// The name of the modified artifact to be compared.
667668
modified: Option<String>,
668669
},
670+
671+
/// Registers a collector in the database
672+
AddCollector {
673+
#[command(flatten)]
674+
db: DbOption,
675+
676+
#[arg(long)]
677+
collector_name: String,
678+
679+
#[arg(long)]
680+
target: String,
681+
682+
#[arg(long)]
683+
is_active: bool,
684+
685+
#[arg(long)]
686+
benchmark_set: u32,
687+
},
688+
689+
/// Polls the job queue for work to benchmark
690+
DequeueJob {
691+
/// The unique identifier for the collector
692+
#[arg(long)]
693+
collector_name: String,
694+
695+
#[arg(long)]
696+
target: String,
697+
698+
#[command(flatten)]
699+
db: DbOption,
700+
},
669701
}
670702

671703
#[derive(Debug, clap::Parser)]
@@ -1266,6 +1298,63 @@ Make sure to modify `{dir}/perf-config.json` if the category/artifact don't matc
12661298
rt.block_on(compare_artifacts(conn, metric, base, modified))?;
12671299
Ok(0)
12681300
}
1301+
1302+
Commands::AddCollector {
1303+
db,
1304+
collector_name,
1305+
target,
1306+
is_active,
1307+
benchmark_set,
1308+
} => {
1309+
let pool = Pool::open(&db.db);
1310+
let rt = build_async_runtime();
1311+
let conn = rt.block_on(pool.connection());
1312+
1313+
let target = database::Target::from_str(&target).map_err(|e| anyhow::anyhow!(e))?;
1314+
rt.block_on(conn.add_collector_config(
1315+
&collector_name,
1316+
&target,
1317+
benchmark_set,
1318+
is_active,
1319+
))?;
1320+
Ok(0)
1321+
}
1322+
1323+
Commands::DequeueJob {
1324+
collector_name,
1325+
db,
1326+
target,
1327+
} => {
1328+
let pool = Pool::open(&db.db);
1329+
let rt = build_async_runtime();
1330+
let conn = rt.block_on(pool.connection());
1331+
1332+
// Obtain the configuration and validate that it matches the
1333+
// collector's setup
1334+
let collector_config: database::CollectorConfig =
1335+
rt.block_on(conn.get_collector_config(&collector_name))?;
1336+
1337+
let collector_target = collector_config.target();
1338+
if collector_target.as_str() != target {
1339+
panic!(
1340+
"Mismatching target for collector expected `{collector_target}` got `{target}`"
1341+
);
1342+
}
1343+
1344+
// Dequeue a job
1345+
let benchmark_job = rt.block_on(conn.dequeue_benchmark_job(
1346+
&collector_name,
1347+
collector_config.target(),
1348+
collector_config.benchmark_set(),
1349+
))?;
1350+
1351+
if let Some(benchmark_job) = benchmark_job {
1352+
// TODO; process the job
1353+
println!("{:?}", benchmark_job);
1354+
}
1355+
1356+
Ok(0)
1357+
}
12691358
}
12701359
}
12711360

collector/src/compile/benchmark/target.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::{fmt, str::FromStr};
2+
13
/// Target representing an Rust target triple, for a full list of targets and
24
/// their support see;
35
/// https://doc.rust-lang.org/nightly/rustc/platform-support.html
@@ -15,12 +17,36 @@ impl Default for Target {
1517
}
1618
}
1719

20+
impl FromStr for Target {
21+
type Err = String;
22+
fn from_str(s: &str) -> Result<Self, Self::Err> {
23+
Ok(match s.to_ascii_lowercase().as_str() {
24+
"x86_64-unknown-linux-gnu" => Target::X86_64UnknownLinuxGnu,
25+
_ => return Err(format!("{} is not a valid target", s)),
26+
})
27+
}
28+
}
29+
30+
impl fmt::Display for Target {
31+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32+
write!(f, "{}", self.as_str())
33+
}
34+
}
35+
1836
impl Target {
1937
pub fn all() -> Vec<Self> {
2038
vec![Self::X86_64UnknownLinuxGnu]
2139
}
2240
}
2341

42+
impl Target {
43+
pub fn as_str(self) -> &'static str {
44+
match self {
45+
Target::X86_64UnknownLinuxGnu => "x86_64-unknown-linux-gnu",
46+
}
47+
}
48+
}
49+
2450
impl From<database::Target> for Target {
2551
fn from(value: database::Target) -> Self {
2652
match value {

database/src/lib.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,10 +1054,12 @@ pub enum BenchmarkJobStatus {
10541054
Queued,
10551055
InProgress {
10561056
started_at: DateTime<Utc>,
1057+
collector_name: String,
10571058
},
10581059
Completed {
10591060
started_at: DateTime<Utc>,
10601061
completed_at: DateTime<Utc>,
1062+
collector_name: String,
10611063
success: bool,
10621064
},
10631065
}
@@ -1109,3 +1111,70 @@ pub struct BenchmarkJob {
11091111
status: BenchmarkJobStatus,
11101112
retry: u32,
11111113
}
1114+
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+
1145+
/// The configuration for a collector
1146+
#[derive(Debug, PartialEq)]
1147+
pub struct CollectorConfig {
1148+
name: String,
1149+
target: Target,
1150+
benchmark_set: BenchmarkSet,
1151+
is_active: bool,
1152+
last_heartbeat_at: DateTime<Utc>,
1153+
date_added: DateTime<Utc>,
1154+
}
1155+
1156+
impl CollectorConfig {
1157+
pub fn name(&self) -> &str {
1158+
&self.name
1159+
}
1160+
1161+
pub fn target(&self) -> &Target {
1162+
&self.target
1163+
}
1164+
1165+
pub fn benchmark_set(&self) -> &BenchmarkSet {
1166+
&self.benchmark_set
1167+
}
1168+
1169+
pub fn is_active(&self) -> bool {
1170+
self.is_active
1171+
}
1172+
1173+
pub fn last_heartbeat_at(&self) -> DateTime<Utc> {
1174+
self.last_heartbeat_at
1175+
}
1176+
1177+
pub fn date_added(&self) -> DateTime<Utc> {
1178+
self.date_added
1179+
}
1180+
}

0 commit comments

Comments
 (0)