MCPcopy
hub / github.com/go-git/go-git / setupBenchmarkRepo

Function setupBenchmarkRepo

worktree_status_bench_test.go:18–81  ·  view source on GitHub ↗

setupBenchmarkRepo creates a test repository with the specified number of files. It returns the worktree for benchmarking.

(b *testing.B, numFiles, numSubdirs, numGoroutines int)

Source from the content-addressed store, hash-verified

16// setupBenchmarkRepo creates a test repository with the specified number of files.
17// It returns the worktree for benchmarking.
18func 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: "benchmark@test.com",
72 When: time.Now(),
73 }
74 _, err = wt.Commit("Initial commit with many files", &CommitOptions{
75 Author: sig,

Callers 2

BenchmarkStatusFunction · 0.85
BenchmarkStatusLargeFunction · 0.85

Calls 7

PlainInitFunction · 0.85
JoinMethod · 0.80
WorktreeMethod · 0.80
AddGlobMethod · 0.80
CommitMethod · 0.65
AddMethod · 0.45
MkdirAllMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…