use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use super::facts::{samples_between, usage_of};
use super::log::{TITLE_SOURCE_MAX, title};
use super::{Job, JobState, JobSummary, Sample, Step};
use crate::api::{SAMPLE_INTERVAL, Tone, cpu_time, duration, in_cores};
use crate::config::ByteSize;
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct CompareQuery {
pub with: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", tag = "kind")]
pub enum Baseline {
Chosen {
job: u64,
branch: Option<String>,
queued_at: u64,
asked: bool,
},
Unavailable {
reason: Unavailable,
},
Incompatible {
job: u64,
},
#[serde(other)]
Other,
}
impl Baseline {
pub fn against(&self, now: u64) -> Option<String> {
let Baseline::Chosen {
job,
branch,
queued_at,
..
} = self
else {
return None;
};
let on = branch
.as_deref()
.map(|branch| format!(" on {}", title(branch)))
.unwrap_or_default();
Some(format!(
"against job {job}{on}, queued {}",
crate::api::ago(*queued_at, now)
))
}
pub fn words(&self) -> Option<String> {
match self {
Baseline::Chosen { .. } => None,
Baseline::Unavailable { reason } => Some(reason.words().to_owned()),
Baseline::Incompatible { job } => Some(format!(
"job {job} is not a run of this job: another repository, workflow or name"
)),
Baseline::Other => Some("not compared".to_owned()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Unavailable {
NoEarlierSuccess,
UnknownWorkflow,
UnknownBranch,
#[serde(other)]
Other,
}
impl Unavailable {
pub fn words(self) -> &'static str {
match self {
Unavailable::NoEarlierSuccess => {
"no earlier run of this job on its branch succeeded here"
}
Unavailable::UnknownWorkflow => {
"its workflow is unknown, so no earlier run is known to be the same job"
}
Unavailable::UnknownBranch => {
"its branch is unknown, so no earlier run is known to be on it"
}
Unavailable::Other => "no baseline",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum FigureKind {
Wait,
Run,
MemoryPeak,
CpuTime,
CpuAverage,
Received,
StepTime,
StepMemory,
StepCpu,
#[serde(other)]
Other,
}
impl FigureKind {
pub fn label(self) -> &'static str {
match self {
FigureKind::Wait => "Wait",
FigureKind::Run => "Run time",
FigureKind::MemoryPeak | FigureKind::StepMemory => "Memory peak",
FigureKind::CpuTime => "CPU time",
FigureKind::CpuAverage | FigureKind::StepCpu => "Average CPU",
FigureKind::Received => "Received",
FigureKind::StepTime => "Time",
FigureKind::Other => "Figure",
}
}
pub fn words(self, value: u64) -> String {
match self {
FigureKind::Wait | FigureKind::Run | FigureKind::StepTime => duration(value),
FigureKind::MemoryPeak | FigureKind::Received | FigureKind::StepMemory => {
ByteSize(value).to_string()
}
FigureKind::CpuTime => cpu_time(value),
FigureKind::CpuAverage | FigureKind::StepCpu => in_cores(value),
FigureKind::Other => value.to_string(),
}
}
fn is_cost(self) -> bool {
matches!(
self,
FigureKind::Wait
| FigureKind::Run
| FigureKind::MemoryPeak
| FigureKind::CpuTime
| FigureKind::StepTime
| FigureKind::StepMemory
)
}
fn floor(self) -> u64 {
match self {
FigureKind::Wait | FigureKind::Run | FigureKind::StepTime => 5,
_ => 0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChangeKind {
More,
Less,
Same,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct FigureDelta {
pub figure: FigureKind,
pub this: Option<u64>,
pub baseline: Option<u64>,
#[serde(default)]
pub sampled: bool,
}
impl FigureDelta {
fn new(figure: FigureKind, this: Option<u64>, baseline: Option<u64>) -> FigureDelta {
FigureDelta {
figure,
this,
baseline,
sampled: false,
}
}
pub fn this_words(&self) -> String {
self.this
.map_or_else(|| "unknown".to_owned(), |value| self.figure.words(value))
}
pub fn baseline_words(&self) -> String {
self.baseline
.map_or_else(|| "unknown".to_owned(), |value| self.figure.words(value))
}
pub fn change(&self) -> ChangeKind {
match (self.this, self.baseline) {
(Some(this), Some(baseline)) if this > baseline => ChangeKind::More,
(Some(this), Some(baseline)) if this < baseline => ChangeKind::Less,
(Some(_), Some(_)) => ChangeKind::Same,
_ => ChangeKind::Unknown,
}
}
pub fn change_words(&self) -> String {
let (Some(this), Some(baseline)) = (self.this, self.baseline) else {
return "unknown".to_owned();
};
let (sign, by) = match self.change() {
ChangeKind::More => ('+', this - baseline),
ChangeKind::Less => ('-', baseline - this),
ChangeKind::Same | ChangeKind::Unknown => return "same".to_owned(),
};
let words = format!("{sign}{}", self.figure.words(by));
match u128::from(by)
.saturating_mul(100)
.checked_div(u128::from(baseline))
{
Some(percent) => format!("{words} ({sign}{percent}%)"),
None => words,
}
}
pub fn tone(&self) -> Tone {
let (Some(this), Some(baseline)) = (self.this, self.baseline) else {
return Tone::Quiet;
};
let by = this.abs_diff(baseline);
let marked = by >= self.figure.floor() && u128::from(by) * 10 > u128::from(baseline);
match self.change() {
ChangeKind::More if marked && self.figure.is_cost() => Tone::Warn,
ChangeKind::Less if marked && self.figure.is_cost() => Tone::Good,
_ => Tone::Neutral,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum StepMatch {
Matched,
OnlyThis,
OnlyBaseline,
Ambiguous,
#[serde(other)]
Other,
}
impl StepMatch {
pub fn words(self) -> Option<&'static str> {
match self {
StepMatch::Matched => None,
StepMatch::OnlyThis => Some("only in this run"),
StepMatch::OnlyBaseline => Some("only in the baseline"),
StepMatch::Ambiguous => Some("name not unique, not compared"),
StepMatch::Other => Some("not compared"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StepDelta {
pub name: String,
pub matched: StepMatch,
pub this: Option<u32>,
pub baseline: Option<u32>,
pub time: FigureDelta,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub memory: Option<FigureDelta>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cpu: Option<FigureDelta>,
}
pub(crate) const SAMPLE_GAP_INTERVALS: u64 = 3;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StepUsage {
pub number: u32,
pub first: u64,
pub last: u64,
pub widest_gap: u64,
pub memory_peak: u64,
pub cpu_average: u64,
}
impl StepUsage {
pub fn of(samples: &[Sample], steps: &[Step]) -> Vec<StepUsage> {
steps
.iter()
.filter_map(|step| {
let (from, to) = (step.started_at?, step.completed_at?);
let inside = samples_between(samples, from, to);
let usage = usage_of(&inside)?;
let widest_gap = inside
.windows(2)
.map(|pair| pair[1].at.saturating_sub(pair[0].at))
.max()
.unwrap_or(0);
Some(StepUsage {
number: step.number,
first: inside.first()?.at,
last: inside.last()?.at,
widest_gap,
memory_peak: usage.memory_peak,
cpu_average: usage.cpu_average,
})
})
.collect()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BaselineRead {
Found {
job: Box<JobSummary>,
steps: ComparedSteps,
usage: Vec<StepUsage>,
asked: bool,
},
Unavailable(Unavailable),
Incompatible(u64),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JobComparison {
pub job: u64,
pub baseline: Baseline,
pub figures: Vec<FigureDelta>,
pub steps: Vec<StepDelta>,
#[serde(default, skip_serializing_if = "is_false")]
pub steps_cut: bool,
}
#[expect(
clippy::trivially_copy_pass_by_ref,
reason = "serde's skip_serializing_if passes a reference"
)]
fn is_false(value: &bool) -> bool {
!value
}
pub const COMPARE_STEPS_MAX: usize = 500;
pub const COMPARE_STEP_BYTES: usize = 128 * 1024;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ComparedSteps {
pub steps: Vec<Step>,
pub cut: bool,
}
impl ComparedSteps {
pub fn of(steps: &[Step]) -> ComparedSteps {
let mut ordered: Vec<&Step> = steps.iter().collect();
ordered.sort_by_key(|step| step.number);
let mut read = ComparedSteps::default();
for step in ordered {
let mut step = step.clone();
let cut = step.name.floor_char_boundary(TITLE_SOURCE_MAX);
step.name.truncate(cut);
if !read.push(step) {
read.cut = true;
break;
}
}
read
}
pub fn push(&mut self, step: Step) -> bool {
let bytes: usize = self.steps.iter().map(|step| step.name.len()).sum();
let full = self.steps.len() >= COMPARE_STEPS_MAX
|| (!self.steps.is_empty() && bytes + step.name.len() > COMPARE_STEP_BYTES);
if !full {
self.steps.push(step);
}
!full
}
}
fn step_time(step: &Step) -> Option<u64> {
Some(step.completed_at?.saturating_sub(step.started_at?))
}
fn covers(usage: &StepUsage, step: &Step) -> bool {
let tolerance = SAMPLE_INTERVAL * SAMPLE_GAP_INTERVALS;
let (Some(from), Some(to)) = (step.started_at, step.completed_at) else {
return false;
};
usage.first.saturating_sub(from) <= tolerance
&& to.saturating_sub(usage.last) <= tolerance
&& usage.widest_gap <= tolerance
}
impl JobComparison {
pub fn same_job(job: &JobSummary, other: &JobSummary) -> bool {
other.id != job.id
&& other.repository == job.repository
&& job.workflow.is_some()
&& other.workflow == job.workflow
&& other.name == job.name
}
pub fn is_baseline(job: &JobSummary, other: &JobSummary) -> bool {
JobComparison::same_job(job, other)
&& job.branch.is_some()
&& other.branch == job.branch
&& other.state == JobState::Completed
&& other.conclusion.as_deref() == Some("success")
&& other.runner.is_some()
&& other.timeline.started_at.is_some()
&& (other.timeline.queued_at, other.id) < (job.timeline.queued_at, job.id)
}
pub fn baseline_of<'history>(
job: &JobSummary,
history: &'history [JobSummary],
) -> Result<&'history JobSummary, Unavailable> {
if job.workflow.is_none() {
return Err(Unavailable::UnknownWorkflow);
}
if job.branch.is_none() {
return Err(Unavailable::UnknownBranch);
}
history
.iter()
.filter(|other| JobComparison::is_baseline(job, other))
.max_by_key(|other| (other.timeline.queued_at, other.id))
.ok_or(Unavailable::NoEarlierSuccess)
}
pub fn in_history(
id: u64,
with: Option<u64>,
history: &[Job],
usage: impl Fn(&Job) -> Vec<StepUsage>,
) -> Option<JobComparison> {
let find = |id: u64| history.iter().find(|job| job.summary.id == id);
let this = find(id)?;
let found = |other: &Job, asked: bool| BaselineRead::Found {
job: Box::new(other.summary.clone()),
steps: ComparedSteps::of(&other.steps),
usage: usage(other),
asked,
};
let baseline = if let Some(with) = with {
let other = find(with)?;
if JobComparison::same_job(&this.summary, &other.summary) {
found(other, true)
} else {
BaselineRead::Incompatible(with)
}
} else {
let summaries: Vec<JobSummary> =
history.iter().map(|job| job.summary.clone()).collect();
match JobComparison::baseline_of(&this.summary, &summaries) {
Ok(other) => find(other.id).map_or(
BaselineRead::Unavailable(Unavailable::NoEarlierSuccess),
|other| found(other, false),
),
Err(reason) => BaselineRead::Unavailable(reason),
}
};
let (steps, usage) = match baseline {
BaselineRead::Found { .. } => (ComparedSteps::of(&this.steps), usage(this)),
_ => (ComparedSteps::default(), Vec::new()),
};
Some(JobComparison::of(&this.summary, &steps, &usage, baseline))
}
pub fn of(
job: &JobSummary,
steps: &ComparedSteps,
usage: &[StepUsage],
baseline: BaselineRead,
) -> JobComparison {
let (other, other_steps, other_usage, asked) = match baseline {
BaselineRead::Found {
job,
steps,
usage,
asked,
} => (job, steps, usage, asked),
BaselineRead::Unavailable(reason) => {
return JobComparison::alone(job, Baseline::Unavailable { reason });
}
BaselineRead::Incompatible(other) => {
return JobComparison::alone(job, Baseline::Incompatible { job: other });
}
};
JobComparison {
job: job.id,
baseline: Baseline::Chosen {
job: other.id,
branch: other.branch.clone(),
queued_at: other.timeline.queued_at,
asked,
},
figures: figures(job, Some(&other)),
steps: deltas(&steps.steps, usage, &other_steps.steps, &other_usage),
steps_cut: steps.cut || other_steps.cut,
}
}
fn alone(job: &JobSummary, baseline: Baseline) -> JobComparison {
JobComparison {
job: job.id,
baseline,
figures: figures(job, None),
steps: Vec::new(),
steps_cut: false,
}
}
pub fn steps_cut_words(&self) -> Option<&'static str> {
self.steps_cut.then_some(
"some steps are not compared: a run has more steps, or longer step names, than a comparison reads",
)
}
}
fn figures(job: &JobSummary, baseline: Option<&JobSummary>) -> Vec<FigureDelta> {
let read = |job: &JobSummary, figure: FigureKind| -> Option<u64> {
let timeline = &job.timeline;
let metrics = &job.metrics;
match figure {
FigureKind::Wait => timeline
.started_at
.map(|started| started.saturating_sub(timeline.queued_at)),
_ if !job.state.is_final() => None,
FigureKind::Run => Some(timeline.finished_at?.saturating_sub(timeline.started_at?)),
FigureKind::MemoryPeak => metrics.memory_peak,
FigureKind::CpuTime => metrics.cpu_time,
FigureKind::CpuAverage => metrics.cpu_average,
FigureKind::Received => metrics.network_received,
_ => None,
}
};
[
FigureKind::Wait,
FigureKind::Run,
FigureKind::MemoryPeak,
FigureKind::CpuTime,
FigureKind::CpuAverage,
FigureKind::Received,
]
.into_iter()
.map(|figure| {
FigureDelta::new(
figure,
read(job, figure),
baseline.and_then(|baseline| read(baseline, figure)),
)
})
.collect()
}
fn names(steps: &[Step]) -> BTreeMap<&str, usize> {
let mut names = BTreeMap::new();
for step in steps {
*names.entry(step.name.as_str()).or_insert(0) += 1;
}
names
}
fn deltas(
ours_steps: &[Step],
usage: &[StepUsage],
theirs_steps: &[Step],
other_usage: &[StepUsage],
) -> Vec<StepDelta> {
let ours = names(ours_steps);
let theirs = names(theirs_steps);
let unique = |name: &str| {
ours.get(name).copied().unwrap_or(0) <= 1 && theirs.get(name).copied().unwrap_or(0) <= 1
};
let usage_of = |usage: &[StepUsage], step: &Step| {
usage
.iter()
.find(|usage| usage.number == step.number)
.filter(|usage| covers(usage, step))
.copied()
};
let sampled = |figure: FigureKind, this: u64, other: u64| FigureDelta {
figure,
this: Some(this),
baseline: Some(other),
sampled: true,
};
let mut deltas = Vec::new();
for step in ours_steps {
let paired = unique(&step.name)
.then(|| theirs_steps.iter().find(|theirs| theirs.name == step.name))
.flatten();
let matched = match (paired, unique(&step.name)) {
(Some(_), _) => StepMatch::Matched,
(None, false) => StepMatch::Ambiguous,
(None, true) => StepMatch::OnlyThis,
};
let ours_used = usage_of(usage, step);
let theirs_used = paired.and_then(|theirs| usage_of(other_usage, theirs));
let both = ours_used.zip(theirs_used);
deltas.push(StepDelta {
name: title(&step.name),
matched,
this: Some(step.number),
baseline: paired.map(|theirs| theirs.number),
time: FigureDelta::new(
FigureKind::StepTime,
step_time(step),
paired.and_then(step_time),
),
memory: both.map(|(ours, theirs)| {
sampled(FigureKind::StepMemory, ours.memory_peak, theirs.memory_peak)
}),
cpu: both.map(|(ours, theirs)| {
sampled(FigureKind::StepCpu, ours.cpu_average, theirs.cpu_average)
}),
});
}
for theirs in theirs_steps {
let unpaired = !unique(&theirs.name) || !ours.contains_key(theirs.name.as_str());
if !unpaired {
continue;
}
deltas.push(StepDelta {
name: title(&theirs.name),
matched: if unique(&theirs.name) {
StepMatch::OnlyBaseline
} else {
StepMatch::Ambiguous
},
this: None,
baseline: Some(theirs.number),
time: FigureDelta::new(FigureKind::StepTime, None, step_time(theirs)),
memory: None,
cpu: None,
});
}
deltas
}
#[cfg(test)]
#[cfg(any(feature = "host", feature = "client"))]
mod tests;