Skip to content

Commit

Permalink
fully implement failing commit hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Swatinem committed Jan 7, 2025
1 parent 3441d93 commit 10743ee
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
29 changes: 27 additions & 2 deletions src/binary/format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::HashSet;
use std::fmt;
use std::ops::Range;

use commithashes_set::CommitHashesSet;
use flags_set::FlagsSet;
use smallvec::SmallVec;
use timestamps::{adjust_selection_range, offset_from_today};
Expand Down Expand Up @@ -102,6 +104,7 @@ impl<'data> TestAnalytics<'data> {
} else {
None
};
let mut failing_commits = HashSet::new();

let num_days = self.header.num_days as usize;
let tests = self.tests.iter().enumerate().filter_map(move |(i, test)| {
Expand All @@ -123,7 +126,11 @@ impl<'data> TestAnalytics<'data> {
return None;
}

let aggregates = Aggregates::from_data(&self.testdata[adjusted_range]);
let aggregates = Aggregates::from_data(
self.commithashes_bytes,
&mut failing_commits,
&self.testdata[adjusted_range],
);

Some(Test {
container: self,
Expand Down Expand Up @@ -195,23 +202,39 @@ pub struct Aggregates {
pub flake_rate: f32,

pub avg_duration: f64,

pub failing_commits: usize,
}

impl Aggregates {
fn from_data(data: &[raw::TestData]) -> Self {
fn from_data(
commithashes_bytes: &[u8],
all_failing_commits: &mut HashSet<CommitHash>,
data: &[raw::TestData],
) -> Self {
let mut total_pass_count = 0;
let mut total_fail_count = 0;
let mut total_skip_count = 0;
let mut total_flaky_fail_count = 0;
let mut total_duration = 0.;

for testdata in data {
total_pass_count += testdata.total_pass_count as u32;
total_fail_count += testdata.total_fail_count as u32;
total_skip_count += testdata.total_skip_count as u32;
total_flaky_fail_count += testdata.total_flaky_fail_count as u32;
total_duration += testdata.total_duration as f64;

// TODO: make sure we validate this data ahead of time!
let failing_commits =
CommitHashesSet::read_raw(commithashes_bytes, testdata.failing_commits_set)
.unwrap();
all_failing_commits.extend(failing_commits);
}

let failing_commits = all_failing_commits.len();
all_failing_commits.clear();

let total_run_count = total_pass_count + total_fail_count;
let (failure_rate, flake_rate, avg_duration) = if total_run_count > 0 {
(
Expand All @@ -233,6 +256,8 @@ impl Aggregates {
flake_rate,

avg_duration,

failing_commits,
}
}
}
61 changes: 61 additions & 0 deletions src/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,65 @@ mod tests {
let mut tests = parsed.tests(5..7, None).unwrap();
assert!(tests.next().is_none());
}

#[test]
fn test_commit_hashes() {
let mut test = test();
test.outcome = Outcome::Failure;
let commit_1 = CommitHash([0; 20]);
let commit_2 = CommitHash([1; 20]);
let commit_3 = CommitHash([2; 20]);
let commit_4 = CommitHash([3; 20]);

let mut writer = TestAnalyticsWriter::new(7);

let mut session = writer.start_session(DAY, commit_1, &[]);
session.insert(&test);

let mut buf1 = vec![];
writer.serialize(&mut buf1).unwrap();

let mut writer = TestAnalyticsWriter::new(7);

let mut session = writer.start_session(DAY, commit_2, &[]);
session.insert(&test);
let mut session = writer.start_session(2 * DAY, commit_3, &[]);
session.insert(&test);
let mut session = writer.start_session(3 * DAY, commit_4, &[]);
session.insert(&test);

let mut buf2 = vec![];
writer.serialize(&mut buf2).unwrap();

let parsed1 = TestAnalytics::parse(&buf1, 3 * DAY).unwrap();
let parsed2 = TestAnalytics::parse(&buf2, 3 * DAY).unwrap();

let merged = TestAnalyticsWriter::merge(&parsed1, &parsed2).unwrap();
let mut buf = vec![];
merged.serialize(&mut buf).unwrap();

let parsed = TestAnalytics::parse(&buf, 3 * DAY).unwrap();
let mut writer = TestAnalyticsWriter::from_existing_format(&parsed).unwrap();
let was_rewritten = writer.rewrite(14, 3 * DAY, Some(0)).unwrap();
assert!(was_rewritten);
let mut buf = vec![];
writer.serialize(&mut buf).unwrap();

let parsed = TestAnalytics::parse(&buf, 3 * DAY).unwrap();

let mut tests = parsed.tests(0..1, None).unwrap();
let abc = tests.next().unwrap();
assert_eq!(abc.aggregates().failing_commits, 1); // commit 4
assert!(tests.next().is_none());

let mut tests = parsed.tests(2..3, None).unwrap();
let abc = tests.next().unwrap();
assert_eq!(abc.aggregates().failing_commits, 2); // commit 1, commit 2
assert!(tests.next().is_none());

let mut tests = parsed.tests(0..60, None).unwrap();
let abc = tests.next().unwrap();
assert_eq!(abc.aggregates().failing_commits, 4); // commit 1 - 4
assert!(tests.next().is_none());
}
}
4 changes: 2 additions & 2 deletions src/binary/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ unsafe impl Pod for TestData {}

#[derive(Debug, Clone, Copy, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[repr(C)]
pub struct CommitHash([u8; 20]);
pub struct CommitHash(pub [u8; 20]);
unsafe impl Pod for CommitHash {}

#[cfg(test)]
Expand All @@ -77,7 +77,7 @@ mod tests {
assert_eq!(mem::size_of::<TestData>(), 24);
assert_eq!(mem::align_of::<TestData>(), 4);

assert_eq!(mem::size_of::<CommitHash>(), 24);
assert_eq!(mem::size_of::<CommitHash>(), 20);
assert_eq!(mem::align_of::<CommitHash>(), 1);
}
}

0 comments on commit 10743ee

Please sign in to comment.