-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_benchmark_test.go
More file actions
158 lines (136 loc) · 4.37 KB
/
Copy pathcache_benchmark_test.go
File metadata and controls
158 lines (136 loc) · 4.37 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
package repomap
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime/trace"
"strings"
"testing"
)
var benchmarkCacheStartupResult *Map
// BenchmarkCacheStartupCleanCold measures a cache-disabled startup against an
// isolated clean clone of a generated medium-sized repository.
func BenchmarkCacheStartupCleanCold(b *testing.B) {
repo := cacheStartupBenchmarkClone(b, cacheStartupBenchmarkSource(b))
cacheStartupBenchmark(b, repo, "")
}
// BenchmarkCacheStartupCleanDiskWarm measures a startup that hydrates an
// unchanged cache from disk.
func BenchmarkCacheStartupCleanDiskWarm(b *testing.B) {
repo := cacheStartupBenchmarkClone(b, cacheStartupBenchmarkSource(b))
cacheDir := b.TempDir()
cacheStartupBenchmarkSeed(b, repo, cacheDir)
cacheStartupBenchmark(b, repo, cacheDir)
}
// BenchmarkCacheStartupDirtyCold measures a cache-disabled startup after an
// uncommitted tracked TypeScript change.
func BenchmarkCacheStartupDirtyCold(b *testing.B) {
repo := cacheStartupBenchmarkClone(b, cacheStartupBenchmarkSource(b))
cacheStartupBenchmarkDirty(b, repo)
cacheStartupBenchmark(b, repo, "")
}
// BenchmarkCacheStartupDirtyUnchangedDiskWarm measures the incremental
// disk-cache path when the worktree is dirty but unchanged since cache seeding.
func BenchmarkCacheStartupDirtyUnchangedDiskWarm(b *testing.B) {
repo := cacheStartupBenchmarkClone(b, cacheStartupBenchmarkSource(b))
cacheStartupBenchmarkDirty(b, repo)
cacheDir := b.TempDir()
cacheStartupBenchmarkSeed(b, repo, cacheDir)
cacheStartupBenchmark(b, repo, cacheDir)
}
func cacheStartupBenchmarkSource(b *testing.B) string {
b.Helper()
repo := filepath.Join(b.TempDir(), "source")
cacheStartupBenchmarkWriteFile(b, filepath.Join(repo, "go.mod"), "module example.com/cache-startup-benchmark\n\ngo 1.26\n")
for pkg := range 6 {
for file := range 10 {
name := fmt.Sprintf("Record%02d%02d", pkg, file)
source := fmt.Sprintf(`package p%02d
type %s struct {
ID int
}
func New%s(id int) %s {
return %s{ID: id}
}
func (r %s) Next() int {
return r.ID + 1
}
`, pkg, name, name, name, name, name)
path := filepath.Join(repo, fmt.Sprintf("pkg%02d", pkg), fmt.Sprintf("file%02d.go", file))
cacheStartupBenchmarkWriteFile(b, path, source)
}
}
cacheStartupBenchmarkWriteFile(b, filepath.Join(repo, "web", "dashboard.ts"), `export function dashboardName(): string {
return "cache-startup";
}
`)
cacheStartupBenchmarkGit(b, repo, "init")
cacheStartupBenchmarkGit(b, repo, "add", ".")
cacheStartupBenchmarkGit(b, repo, "commit", "-m", "initial fixture")
return repo
}
func cacheStartupBenchmarkClone(b *testing.B, source string) string {
b.Helper()
clone := filepath.Join(b.TempDir(), "clone")
cacheStartupBenchmarkGit(b, "", "clone", "--quiet", source, clone)
return clone
}
func cacheStartupBenchmarkDirty(b *testing.B, repo string) {
b.Helper()
path := filepath.Join(repo, "web", "dashboard.ts")
data, err := os.ReadFile(path)
if err != nil {
b.Fatal(err)
}
cacheStartupBenchmarkWriteFile(b, path, string(data)+"\nexport const dirtyCacheStartup = true;\n")
}
func cacheStartupBenchmarkSeed(b *testing.B, repo, cacheDir string) {
b.Helper()
m := New(repo, DefaultConfig())
m.SetCacheDir(cacheDir)
if err := m.Build(context.Background()); err != nil {
b.Fatal(err)
}
if !New(repo, DefaultConfig()).LoadCache(cacheDir) {
b.Fatal("seeded cache is not loadable")
}
}
func cacheStartupBenchmark(b *testing.B, repo, cacheDir string) {
b.Helper()
b.ResetTimer()
for b.Loop() {
trace.WithRegion(b.Context(), "cache-startup-build", func() {
m := New(repo, DefaultConfig())
if cacheDir != "" {
m.SetCacheDir(cacheDir)
}
if err := m.Build(context.Background()); err != nil {
b.Fatal(err)
}
benchmarkCacheStartupResult = m
})
}
}
func cacheStartupBenchmarkWriteFile(b *testing.B, path, content string) {
b.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
b.Fatal(err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
b.Fatal(err)
}
}
func cacheStartupBenchmarkGit(b *testing.B, dir string, args ...string) {
b.Helper()
gitArgs := append([]string{"-c", "user.email=benchmark@example.com", "-c", "user.name=Cache Benchmark"}, args...)
cmd := exec.CommandContext(b.Context(), "git", gitArgs...)
if dir != "" {
cmd.Dir = dir
}
out, err := cmd.CombinedOutput()
if err != nil {
b.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
}
}