Skip to content

Commit

Permalink
chore: use a more realistic change detection benchmark. (#1912)
Browse files Browse the repository at this point in the history
  • Loading branch information
i4ki authored Oct 10, 2024
2 parents c251105 + 0d72b03 commit cf1cecd
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions benchmarks/changed/changed_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,46 @@ func BenchmarkChangeDetection(b *testing.B) {
const nstacks = 10

s := sandbox.New(b)
layout := []string{}
layout := []string{
"f:modules/common/common-1/main.tf:# nothing",
"f:modules/common/common-2/main.tf:# nothing",
"f:modules/common/common-3/main.tf:# nothing",
}

for i := 0; i < nstacks; i++ {
layout = append(layout, fmt.Sprintf("f:modules/mod-%d/main.tf:%s", i, "# nothing here"))
layout = append(layout, fmt.Sprintf("s:stack-%d", i))
layout = append(layout, fmt.Sprintf("f:stack-%d/main.tf:%s", i, Block("module",
Labels("something"),
Str("source", fmt.Sprintf("../modules/mod-%d", i)),
layout = append(layout, fmt.Sprintf("f:stack-%d/main.tf:%s", i, Doc(
Block("module",
Labels("something"),
Str("source", fmt.Sprintf("../modules/mod-%d", i)),
),
).String()))

// only even stacks has the common modules
if i%2 == 0 {
layout = append(layout, fmt.Sprintf("f:stack-%d/use-common.tf:%s", i, Doc(
Block("module",
Labels("common_mod1"),
Str("source", "../modules/common/common-1"),
),
Block("module",
Labels("common_mod2"),
Str("source", "../modules/common/common-2"),
),
Block("module",
Labels("common_mod3"),
Str("source", "../modules/common/common-3"),
),
)))
}
}
s.BuildTree(layout)
s.Git().CommitAll("create repo")
s.Git().Push("main")
s.Git().CheckoutNew("modify-some-modules")
test.WriteFile(b, filepath.Join(s.RootDir(), fmt.Sprintf("modules/mod-%d", nstacks-1)), "main.tf", "# modified")
test.WriteFile(b, filepath.Join(s.RootDir(), "modules/common/common-3"), "main.tf", "# modified")
s.Git().CommitAll("module modified")

manager := stack.NewGitAwareManager(s.Config(), s.Git().Unwrap())
Expand All @@ -44,8 +70,14 @@ func BenchmarkChangeDetection(b *testing.B) {
for i := 0; i < b.N; i++ {
report, err := manager.ListChanged(stack.ChangeConfig{BaseRef: "origin/main"})
assert.NoError(b, err)
assert.EqualInts(b, 1, len(report.Stacks))
assert.EqualStrings(b, fmt.Sprintf("/stack-%d", nstacks-1), report.Stacks[0].Stack.Dir.String())
assert.EqualInts(b, 6, len(report.Stacks))

assert.EqualStrings(b, "/stack-0", report.Stacks[0].Stack.Dir.String())
assert.EqualStrings(b, "/stack-2", report.Stacks[1].Stack.Dir.String())
assert.EqualStrings(b, "/stack-4", report.Stacks[2].Stack.Dir.String())
assert.EqualStrings(b, "/stack-6", report.Stacks[3].Stack.Dir.String())
assert.EqualStrings(b, "/stack-8", report.Stacks[4].Stack.Dir.String())
assert.EqualStrings(b, fmt.Sprintf("/stack-%d", nstacks-1), report.Stacks[5].Stack.Dir.String())
}
}

Expand Down

0 comments on commit cf1cecd

Please sign in to comment.