Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark recurseKustomizationFiles() #622

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 93 additions & 0 deletions controllers/kustomization_decryptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"encoding/base64"
"fmt"
"io/fs"
"math"
"math/rand"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -55,6 +57,11 @@ import (
"github.com/fluxcd/pkg/apis/meta"
)

const (
percent10 = 10.0
percent80 = 80.0
)

func TestKustomizationReconciler_Decryptor(t *testing.T) {
g := NewWithT(t)

Expand Down Expand Up @@ -1573,6 +1580,92 @@ func Test_recurseKustomizationFiles(t *testing.T) {
}
}

// benchmarkRecKustFiles runs benchmark for recurseKustomizationFiles().
func benchmarkRecKustFiles(numKusNodes int, percentSymlinks float64, b *testing.B) {
b.StopTimer()

numSymlinksFloat := (percentSymlinks / 100.0) * float64(numKusNodes)
numSymlinks := int(math.Round(numSymlinksFloat))

g := NewWithT(b)
tmpDir := b.TempDir()

// Create manifest directories. Write all the symlinked manifests in bar.
// /tmp/test-dir/
// ├── bar
// │   └── 1
// │  └── kustomization.yaml
// │
// └── foo
// ├── 0
// │ └── kustomization.yaml
// └── 1 -> ../bar/1
os.MkdirAll(filepath.Join(tmpDir, "foo"), 0o700)
os.MkdirAll(filepath.Join(tmpDir, "bar"), 0o700)

// Generate index of kustomizations that'll be symlinked.
rand.Seed(42) // Reproducible.
randKusNodes := rand.Perm(numKusNodes)[0:numSymlinks]
symlinkKusNodes := map[int]struct{}{}
for _, n := range randKusNodes {
symlinkKusNodes[n] = struct{}{}
}

// Create kustomization nodes.
// The kustomizations are chained to one another. The first kustomization
// refers to the second, the second refers to the third, and so on.
for kn := 0; kn < numKusNodes; kn++ {
// /tmp/test-dir/foo/0
kPath := filepath.Join(tmpDir, "foo", fmt.Sprint(kn))
kus := kustypes.Kustomization{
TypeMeta: kustypes.TypeMeta{
APIVersion: kustypes.KustomizationVersion,
Kind: kustypes.KustomizationKind,
},
Resources: []string{},
}
// Append next node reference, except for when it is the last node.
if kn != numKusNodes-1 {
kus.Resources = append(kus.Resources, filepath.Join("..", fmt.Sprint(kn+1)))
}
b, err := yaml.Marshal(kus)
g.Expect(err).ToNot(HaveOccurred())

// If this node is a symlink, create a symlink and write to the
// respective source file. Else, write to the actual file.
if _, ok := symlinkKusNodes[kn]; ok {
srcPath := filepath.Join(tmpDir, "bar", fmt.Sprint(kn))
g.Expect(os.MkdirAll(srcPath, 0o700)).ToNot(HaveOccurred())
// Relative path from foo/0 to bar/0 is ../bar/0.
g.Expect(os.Symlink(filepath.Join("..", "bar", fmt.Sprint(kn)), kPath)).ToNot(HaveOccurred())
g.Expect(os.WriteFile(filepath.Join(srcPath, "kustomization.yaml"), b, 0o644)).ToNot(HaveOccurred())
} else {
g.Expect(os.MkdirAll(kPath, 0o700)).ToNot(HaveOccurred())
g.Expect(os.WriteFile(filepath.Join(kPath, "kustomization.yaml"), b, 0o644)).ToNot(HaveOccurred())
}
}

// no-op visit.
visit := func(root, path string, kus *kustypes.Kustomization) error {
return nil
}

b.StartTimer()
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to also report on allocs so we can grasp the impact as the code changes?

Suggested change
b.StartTimer()
b.StartTimer()
b.ReportAllocs()

for n := 0; n < b.N; n++ {
visited := make(map[string]struct{})
recurseKustomizationFiles(tmpDir, filepath.Join(tmpDir, "foo", "0"), visit, visited)
}
}

func BenchmarkRecKust10Nodes10pcSymlink(b *testing.B) { benchmarkRecKustFiles(10, percent10, b) }
func BenchmarkRecKust10Nodes80pcSymlink(b *testing.B) { benchmarkRecKustFiles(10, percent80, b) }
func BenchmarkRecKust100Nodes10pcSymlink(b *testing.B) { benchmarkRecKustFiles(100, percent10, b) }
func BenchmarkRecKust100Nodes80pcSymlink(b *testing.B) { benchmarkRecKustFiles(100, percent80, b) }
func BenchmarkRecKust1000Nodes10pcSymlink(b *testing.B) { benchmarkRecKustFiles(1000, percent10, b) }
func BenchmarkRecKust1000Nodes80pcSymlink(b *testing.B) { benchmarkRecKustFiles(1000, percent80, b) }
func BenchmarkRecKust10000Nodes10pcSymlink(b *testing.B) { benchmarkRecKustFiles(10000, percent10, b) }
func BenchmarkRecKust10000Nodes80pcSymlink(b *testing.B) { benchmarkRecKustFiles(10000, percent80, b) }

func Test_isSOPSEncryptedResource(t *testing.T) {
g := NewWithT(t)

Expand Down