Skip to content

Commit

Permalink
refactor: Parse the deltas async
Browse files Browse the repository at this point in the history
  • Loading branch information
PurpleBooth committed Aug 31, 2021
1 parent 2409f56 commit f03d69c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async fn main() -> Result<(), crate::errors::Error> {
let statistics = stream::iter(deltas)
.zip(stream::iter(git_repo_args))
.flat_map(|(delta, prefix)| stream::iter(add_prefix((&delta, prefix))))
.fold(Statistics::default(), |acc, change_delta| async move {
acc.add_delta(&change_delta, &strategy)
.fold(Statistics::default(), |acc, change_delta| {
acc.add_delta(change_delta, &strategy)
})
.await;

Expand Down
72 changes: 42 additions & 30 deletions src/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub enum Strategy {
}

impl Statistics {
pub(crate) fn add_delta(self, delta: &ChangeDelta, strategy: &Strategy) -> Statistics {
pub(crate) async fn add_delta(self, delta: ChangeDelta, strategy: &Strategy) -> Statistics {
let mut change_map = self.change_deltas;
let (key, new_delta) = match strategy {
Strategy::Id => (delta.id(), delta.clone()),
Expand All @@ -62,7 +62,7 @@ impl Statistics {
key.clone(),
match change_map.get(&key) {
None => delta.clone(),
Some(existing_delta) => existing_delta.merge(delta),
Some(existing_delta) => existing_delta.merge(&delta),
},
)
}
Expand Down Expand Up @@ -202,46 +202,48 @@ mod tests {
use crate::model::change_delta::ChangeDelta;
use crate::statistics::{CouplingKey, Statistics, Strategy};

#[test]
fn adding_one_file_to_statistics_will_give_a_count_of_zero() {
#[tokio::test]
async fn adding_one_file_to_statistics_will_give_a_count_of_zero() {
let statistics = Statistics::default();
let actual = statistics.add_delta(
&ChangeDelta::new("Id".into(), Utc::now(), vec!["file_1".into()]),
ChangeDelta::new("Id".into(), Utc::now(), vec!["file_1".into()]),
&Strategy::Id,
);
assert_eq!(actual.coupling(), BTreeMap::new());
assert_eq!(actual.await.coupling(), BTreeMap::new());
}

#[test]
fn a_file_two_files_at_the_same_time_twice_is_full_coupling() {
#[tokio::test]
async fn a_file_two_files_at_the_same_time_twice_is_full_coupling() {
let statistics = Statistics::default();
let actual = statistics
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"1".into(),
Utc::now(),
vec!["file_1".into(), "file_2".into()],
),
&Strategy::Id,
)
.await
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"2".into(),
Utc::now(),
vec!["file_1".into(), "file_2".into()],
),
&Strategy::Id,
)
.await
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"3".into(),
Utc::now(),
vec!["file_1".into(), "file_2".into()],
),
&Strategy::Id,
);
assert_eq!(
actual.coupling(),
actual.await.coupling(),
vec![(
CouplingKey::new("file_1".into(), "file_2".into()),
(1.0, 3, 3)
Expand All @@ -251,60 +253,65 @@ mod tests {
);
}

#[test]
fn more_complex_coupling() {
#[tokio::test]
async fn more_complex_coupling() {
let statistics = Statistics::default();
let actual = statistics
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"1".into(),
Utc::now(),
vec!["file_1".into(), "file_2".into()],
),
&Strategy::Id,
)
.await
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"2".into(),
Utc::now(),
vec!["file_3".into(), "file_2".into()],
),
&Strategy::Id,
)
.await
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"3".into(),
Utc::now(),
vec!["file_3".into(), "file_2".into()],
),
&Strategy::Id,
)
.await
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"4".into(),
Utc::now(),
vec!["file_3".into(), "file_5".into()],
),
&Strategy::Id,
)
.await
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"5".into(),
Utc::now(),
vec!["file_3".into(), "file_1".into()],
),
&Strategy::Id,
)
.await
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"6".into(),
Utc::now(),
vec!["file_1".into(), "file_2".into()],
),
&Strategy::Id,
);
assert_eq!(
actual.coupling(),
actual.await.coupling(),
vec![
(
CouplingKey::new("file_1".into(), "file_2".into()),
Expand All @@ -328,51 +335,56 @@ mod tests {
);
}

#[test]
fn statistics_render_pretty() {
#[tokio::test]
async fn statistics_render_pretty() {
let statistics = Statistics::default()
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"1".into(),
Utc::now(),
vec!["file_1".into(), "file_2".into()],
),
&Strategy::Id,
)
.await
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"2".into(),
Utc::now(),
vec!["file_3".into(), "file_2".into()],
),
&Strategy::Id,
)
.await
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"3".into(),
Utc::now(),
vec!["file_3".into(), "file_2".into()],
),
&Strategy::Id,
)
.await
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"4".into(),
Utc::now(),
vec!["file_3".into(), "file_5".into()],
),
&Strategy::Id,
)
.await
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"5".into(),
Utc::now(),
vec!["file_3".into(), "file_1".into()],
),
&Strategy::Id,
)
.await
.add_delta(
&ChangeDelta::new(
ChangeDelta::new(
"6".into(),
Utc::now(),
vec!["file_1".into(), "file_2".into()],
Expand All @@ -381,7 +393,7 @@ mod tests {
&Strategy::Id,
);
assert_eq!(
format!("{}", statistics),
format!("{}", statistics.await),
"+-------------+-------------+------------+----------+---------+
| File A | File B | Together % | Together | Commits |
+=============================================================+
Expand Down

1 comment on commit f03d69c

@PurpleBooth
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On repos about 4000~ commits async starts making a difference. For larger repos it's probably key, though I doubt we'll ever get into the 10,000's of commits range with reasonable performance

Benchmark #1: /Users/billie/Code/git-moves-together/target/release/git-moves-together .
  Time (mean ± σ):     128.600 s ±  2.546 s    [User: 127.245 s, System: 1.265 s]
  Range (min … max):   125.732 s … 133.177 s    10 runs
 
Benchmark #1: git-moves-together .
  Time (mean ± σ):     145.338 s ±  5.303 s    [User: 143.429 s, System: 1.532 s]
  Range (min … max):   139.224 s … 156.573 s    10 runs

Please sign in to comment.