Debian Codegraph @

worktree_status_bench_test.go

raw
1 package git
2
3 import (
4 "fmt"
5 "path/filepath"
6 "sync"
7 "testing"
8 "time"
9
10 "github.com/go-git/go-billy/v5/util"
11 "github.com/stretchr/testify/require"
12
13 "github.com/go-git/go-git/v5/plumbing/object"
14 )
15
16 // setupBenchmarkRepo creates a test repository with the specified number of files.
17 // It returns the worktree for benchmarking.
18 func setupBenchmarkRepo(b *testing.B, numFiles, numSubdirs, numGoroutines int) *Worktree {
19 b.Helper()
20
21 tmpDir := b.TempDir()
22 repoDir := filepath.Join(tmpDir, "repo")
23
24 repo, err := PlainInit(repoDir, false)
25 require.NoError(b, err)
26
27 wt, err := repo.Worktree()
28 require.NoError(b, err)
29
30 content := []byte("test content for benchmark\n")
31
32 var wg sync.WaitGroup
33 fileChan := make(chan string, numFiles)
34
35 for range numGoroutines {
36 wg.Add(1)
37 go func() {
38 defer wg.Done()
39 for filePath := range fileChan {
40 dir := filepath.Dir(filePath)
41 err := wt.Filesystem.MkdirAll(dir, 0o755)
42 if err != nil {
43 b.Errorf("failed to create directory %s: %v", dir, err)
44 continue
45 }
46
47 err = util.WriteFile(wt.Filesystem, filePath, content, 0o644)
48 if err != nil {
49 b.Errorf("failed to write file %s: %v", filePath, err)
50 }
51 }
52 }()
53 }
54
55 for i := range numFiles {
56 subdir := fmt.Sprintf("dir%d", i%numSubdirs)
57 fileName := fmt.Sprintf("file%04d.txt", i)
58 filePath := filepath.Join(subdir, fileName)
59 fileChan <- filePath
60 }
61 close(fileChan)
62 wg.Wait()
63
64 for i := range numSubdirs {
65 err = wt.AddGlob(fmt.Sprintf("dir%d/*", i))
66 require.NoError(b, err)
67 }
68
69 sig := &object.Signature{
70 Name: "Benchmark",
71 Email: "[email protected]",
72 When: time.Now(),
73 }
74 _, err = wt.Commit("Initial commit with many files", &CommitOptions{
75 Author: sig,
76 Committer: sig,
77 })
78 require.NoError(b, err)
79
80 return wt
81 }
82
83 // BenchmarkStatus benchmarks Status() on a repository with 2000 files.
84 // It includes sub-benchmarks for clean and modified scenarios.
85 func BenchmarkStatus(b *testing.B) {
86 const (
87 numFiles = 2000
88 numSubdirs = 10
89 numGoroutines = 10
90 )
91
92 wt := setupBenchmarkRepo(b, numFiles, numSubdirs, numGoroutines)
93
94 b.Run("Clean", benchmarkStatusClean(wt))
95 b.Run("Modified", benchmarkStatusModified(wt, numFiles, numSubdirs))
96 }
97
98 // benchmarkStatusClean returns a benchmark function for testing Status() on a clean repository.
99 // This represents the worst-case scenario for the current implementation where
100 // every file's hash is computed unnecessarily since nothing has changed.
101 func benchmarkStatusClean(wt *Worktree) func(b *testing.B) {
102 return func(b *testing.B) {
103 for b.Loop() {
104 status, err := wt.Status()
105 if err != nil {
106 b.Fatalf("failed to get status: %v", err)
107 }
108 if !status.IsClean() {
109 b.Fatalf("expected clean status, got: %v", status)
110 }
111 }
112 }
113 }
114
115 // benchmarkStatusModified returns a benchmark function for testing Status() on a repository
116 // with some modified files. This represents a more realistic scenario where a small
117 // percentage of files have changed.
118 func benchmarkStatusModified(wt *Worktree, numFiles, numSubdirs int) func(b *testing.B) {
119 return func(b *testing.B) {
120 const modifiedPercent = 1
121
122 numModified := (numFiles * modifiedPercent) / 100
123 if numModified == 0 {
124 numModified = 1
125 }
126 modifiedContent := []byte("modified content\n")
127 for i := range numModified {
128 subdir := fmt.Sprintf("dir%d", i%numSubdirs)
129 fileName := fmt.Sprintf("file%04d.txt", i)
130 filePath := filepath.Join(subdir, fileName)
131 err := util.WriteFile(wt.Filesystem, filePath, modifiedContent, 0o644)
132 require.NoError(b, err)
133 }
134
135 for b.Loop() {
136 status, err := wt.Status()
137 if err != nil {
138 b.Fatalf("failed to get status: %v", err)
139 }
140 if status.IsClean() {
141 b.Fatalf("expected modified status, got clean")
142 }
143 modCount := 0
144 for _, fileStatus := range status {
145 if fileStatus.Worktree == Modified {
146 modCount++
147 }
148 }
149 if modCount != numModified {
150 b.Fatalf("expected %d modified files, got %d", numModified, modCount)
151 }
152 }
153 }
154 }
155
156 // BenchmarkStatusLarge benchmarks Status() on a large repository with 5000 files.
157 func BenchmarkStatusLarge(b *testing.B) {
158 const (
159 numFiles = 5000
160 numSubdirs = 20
161 numGoroutines = 10
162 )
163
164 wt := setupBenchmarkRepo(b, numFiles, numSubdirs, numGoroutines)
165
166 b.Run("Clean", benchmarkStatusClean(wt))
167 }