forked from go-git/go-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworktree_clone_bench_test.go
More file actions
180 lines (155 loc) · 4.16 KB
/
worktree_clone_bench_test.go
File metadata and controls
180 lines (155 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package git
import (
"fmt"
"path/filepath"
"sync"
"testing"
"time"
"github.com/go-git/go-billy/v6/util"
"github.com/stretchr/testify/require"
"github.com/go-git/go-git/v6/plumbing/object"
)
// BenchmarkCloneLargeRepo benchmarks cloning a repository with many files
// to exercise the checkoutChange codepath and FindEntry performance.
func BenchmarkCloneLargeRepo(b *testing.B) {
const (
numFiles = 2000
numSubdirs = 2
numGoroutines = 10
filesPerSubdir = numFiles / numSubdirs
)
tmpDir := b.TempDir()
sourceDir := filepath.Join(tmpDir, "source")
sourceRepo, err := PlainInit(sourceDir, false)
require.NoError(b, err)
sourceWt, err := sourceRepo.Worktree()
require.NoError(b, err)
content := []byte("test content for benchmark\n")
var wg sync.WaitGroup
fileChan := make(chan string, numFiles)
for range numGoroutines {
wg.Add(1)
go func() {
defer wg.Done()
for filePath := range fileChan {
dir := filepath.Dir(filePath)
err := sourceWt.Filesystem.MkdirAll(dir, 0o755)
if err != nil {
b.Errorf("failed to create directory %s: %v", dir, err)
continue
}
err = util.WriteFile(sourceWt.Filesystem, filePath, content, 0o644)
if err != nil {
b.Errorf("failed to write file %s: %v", filePath, err)
}
}
}()
}
for i := range numFiles {
subdir := fmt.Sprintf("dir%d", i%numSubdirs)
fileName := fmt.Sprintf("file%04d.txt", i)
filePath := filepath.Join(subdir, fileName)
fileChan <- filePath
}
close(fileChan)
wg.Wait()
// Add all files to index using AddGlob for each directory as Add is too slow at the moment.
for i := range numSubdirs {
err = sourceWt.AddGlob(fmt.Sprintf("dir%d/*", i))
require.NoError(b, err)
}
sig := &object.Signature{
Name: "Benchmark",
Email: "benchmark@test.com",
When: time.Now(),
}
_, err = sourceWt.Commit("Initial commit with many files", &CommitOptions{
Author: sig,
Committer: sig,
})
require.NoError(b, err)
i := 0
for b.Loop() {
cloneDir := filepath.Join(tmpDir, fmt.Sprintf("clone-%d", i))
_, err := PlainClone(cloneDir, &CloneOptions{
URL: sourceDir,
Shared: true,
})
if err != nil {
b.Fatalf("failed to clone repository: %v", err)
}
i++
}
}
// BenchmarkCloneDeepRepo benchmarks cloning a repository with files in deep
// directory structures to better demonstrate FindEntry caching benefits.
func BenchmarkCloneDeepRepo(b *testing.B) {
const (
numFiles = 2000
dirDepth = 5
numGoroutines = 10
)
tmpDir := b.TempDir()
sourceDir := filepath.Join(tmpDir, "source")
sourceRepo, err := PlainInit(sourceDir, false)
require.NoError(b, err)
sourceWt, err := sourceRepo.Worktree()
require.NoError(b, err)
content := []byte("test content for benchmark\n")
var wg sync.WaitGroup
fileChan := make(chan string, numFiles)
for range numGoroutines {
wg.Add(1)
go func() {
defer wg.Done()
for filePath := range fileChan {
dir := filepath.Dir(filePath)
err := sourceWt.Filesystem.MkdirAll(dir, 0o755)
if err != nil {
b.Errorf("failed to create directory %s: %v", dir, err)
continue
}
err = util.WriteFile(sourceWt.Filesystem, filePath, content, 0o644)
if err != nil {
b.Errorf("failed to write file %s: %v", filePath, err)
}
}
}()
}
for i := range numFiles {
pathParts := make([]string, dirDepth+1)
for d := range dirDepth {
pathParts[d] = fmt.Sprintf("level%d", d)
}
pathParts[dirDepth] = fmt.Sprintf("file%04d.txt", i)
filePath := filepath.Join(pathParts...)
fileChan <- filePath
}
close(fileChan)
wg.Wait()
// Add all files to index using AddGlob as Add is too slow at the moment.
err = sourceWt.AddGlob("level0/*/*/*/*/*")
require.NoError(b, err)
sig := &object.Signature{
Name: "Benchmark",
Email: "benchmark@test.com",
When: time.Now(),
}
_, err = sourceWt.Commit("Initial commit with many files", &CommitOptions{
Author: sig,
Committer: sig,
})
require.NoError(b, err)
i := 0
for b.Loop() {
cloneDir := filepath.Join(tmpDir, fmt.Sprintf("clone-%d", i))
_, err := PlainClone(cloneDir, &CloneOptions{
URL: sourceDir,
Shared: true,
})
if err != nil {
b.Fatalf("failed to clone repository: %v", err)
}
i++
}
}