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