forked from cloudflare/artifact-fs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_test.go
More file actions
456 lines (416 loc) · 14.1 KB
/
Copy pathbench_test.go
File metadata and controls
456 lines (416 loc) · 14.1 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//go:build !windows
package main
import (
"context"
"fmt"
"log/slog"
"math"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"testing"
"time"
"github.com/cloudflare/artifact-fs/internal/fusefs"
"github.com/cloudflare/artifact-fs/internal/gitstore"
"github.com/cloudflare/artifact-fs/internal/hydrator"
"github.com/cloudflare/artifact-fs/internal/logging"
"github.com/cloudflare/artifact-fs/internal/model"
"github.com/cloudflare/artifact-fs/internal/overlay"
"github.com/cloudflare/artifact-fs/internal/snapshot"
)
type repoSpec struct {
name string
url string
branch string
}
var benchRepos = []repoSpec{
{"agents", "https://github.com/cloudflare/agents.git", "main"},
{"workers-sdk", "https://github.com/cloudflare/workers-sdk.git", "main"},
{"ask-bonk", "https://github.com/ask-bonk/ask-bonk.git", "main"},
}
// timing records a labeled duration.
type timing struct {
label string
dur time.Duration
extra string // optional detail (e.g., node count)
}
func TestBenchRepos(t *testing.T) {
if os.Getenv("AFS_RUN_BENCH") != "1" {
t.Skip("skipping benchmarks (set AFS_RUN_BENCH=1 to run)")
}
logger := logging.NewJSONLogger(os.Stderr, slog.LevelWarn)
git := gitstore.New(logger)
for _, repo := range benchRepos {
t.Run(repo.name, func(t *testing.T) {
results := benchmarkRepo(t, git, repo)
printReport(t, repo, results)
})
}
}
func benchmarkRepo(t *testing.T, git *gitstore.Store, repo repoSpec) []timing {
t.Helper()
root := t.TempDir()
ctx := context.Background()
cfg := model.RepoConfig{
ID: model.RepoID(repo.name),
Name: repo.name,
RemoteURL: repo.url,
Branch: repo.branch,
GitDir: filepath.Join(root, "git"),
MetaDBPath: filepath.Join(root, "meta.sqlite"),
OverlayDir: filepath.Join(root, "overlay"),
OverlayDBPath: filepath.Join(root, "overlay", "meta.sqlite"),
BlobCacheDir: filepath.Join(root, "cache"),
MountPath: filepath.Join(root, "mnt"),
MountRoot: filepath.Join(root, "mnt"),
}
var results []timing
// -------------------------------------------------------
// Phase 1: Blobless clone
// -------------------------------------------------------
start := time.Now()
if err := git.CloneBlobless(ctx, cfg); err != nil {
t.Skipf("clone failed (repo may be private or unavailable): %v", err)
}
cloneDur := time.Since(start)
results = append(results, timing{"clone (blobless)", cloneDur, ""})
// -------------------------------------------------------
// Phase 2: Resolve HEAD
// -------------------------------------------------------
start = time.Now()
headOID, headRef, err := git.ResolveHEAD(ctx, cfg)
if err != nil {
t.Fatalf("ResolveHEAD: %v", err)
}
results = append(results, timing{"resolve HEAD", time.Since(start), headRef + " " + headOID[:8]})
// -------------------------------------------------------
// Phase 3: Build tree index (ls-tree + batch-check sizes)
// -------------------------------------------------------
start = time.Now()
nodes, err := git.BuildTreeIndex(ctx, cfg, headOID)
if err != nil {
t.Fatalf("BuildTreeIndex: %v", err)
}
indexDur := time.Since(start)
// Count files, dirs, known sizes
var fileCount, dirCount, symlinkCount int
var knownSizes, unknownSizes int
var totalKnownBytes int64
for _, n := range nodes {
switch n.Type {
case "file":
fileCount++
case "dir":
dirCount++
case "symlink":
symlinkCount++
}
if n.SizeState == "known" {
knownSizes++
totalKnownBytes += n.SizeBytes
} else {
unknownSizes++
}
}
results = append(results, timing{"build tree index", indexDur,
fmt.Sprintf("%d nodes (%d files, %d dirs, %d symlinks)", len(nodes), fileCount, dirCount, symlinkCount)})
results = append(results, timing{" ls-tree + batch-check", indexDur,
fmt.Sprintf("sizes: %d known (%.1f MB), %d unknown", knownSizes, float64(totalKnownBytes)/(1024*1024), unknownSizes)})
// -------------------------------------------------------
// Phase 4: Publish snapshot (SQLite bulk insert)
// -------------------------------------------------------
start = time.Now()
snap, err := snapshot.New(ctx, cfg.MetaDBPath)
if err != nil {
t.Fatalf("snapshot.New: %v", err)
}
gen, err := snap.PublishGeneration(ctx, headOID, headRef, nodes)
if err != nil {
t.Fatalf("PublishGeneration: %v", err)
}
publishDur := time.Since(start)
results = append(results, timing{"publish snapshot", publishDur,
fmt.Sprintf("gen=%d, %d rows inserted", gen, len(nodes))})
// Cold-start total (what a user waits for before mount is ready)
coldStart := cloneDur + indexDur + publishDur
results = append(results, timing{"COLD START TOTAL", coldStart,
"clone + index + publish"})
// -------------------------------------------------------
// Phase 5: Snapshot lookups (simulating readdir + getattr)
// -------------------------------------------------------
// Root readdir
start = time.Now()
rootChildren, err := snap.ListChildren(gen, ".")
if err != nil {
t.Fatalf("ListChildren root: %v", err)
}
rootReaddirDur := time.Since(start)
results = append(results, timing{"readdir root (snapshot)", rootReaddirDur,
fmt.Sprintf("%d children", len(rootChildren))})
// Getattr for all root children (simulates what FUSE does after readdir)
start = time.Now()
for _, c := range rootChildren {
snap.GetNode(gen, c.Path)
}
rootGetattrDur := time.Since(start)
results = append(results, timing{"getattr root children (snapshot)", rootGetattrDur,
fmt.Sprintf("%d lookups", len(rootChildren))})
// Find a deeply nested directory for readdir benchmark
deepDir := findDeepDir(nodes, 3)
if deepDir != "" {
start = time.Now()
deepChildren, _ := snap.ListChildren(gen, deepDir)
results = append(results, timing{"readdir deep dir (snapshot)", time.Since(start),
fmt.Sprintf("%s -> %d children", deepDir, len(deepChildren))})
}
// -------------------------------------------------------
// Phase 6: Overlay setup + resolver performance
// -------------------------------------------------------
os.MkdirAll(cfg.OverlayDir, 0o755)
os.MkdirAll(cfg.BlobCacheDir, 0o755)
ov, err := overlay.New(ctx, cfg)
if err != nil {
t.Fatalf("overlay.New: %v", err)
}
defer ov.Close()
resolver := &fusefs.Resolver{Snapshot: snap, Overlay: ov}
resolver.SetGeneration(gen)
// Merged readdir (snapshot + overlay)
start = time.Now()
mergedEntries, err := resolver.ReaddirTyped(ctx, ".")
if err != nil {
t.Fatalf("ReaddirTyped: %v", err)
}
results = append(results, timing{"readdir root (merged)", time.Since(start),
fmt.Sprintf("%d entries", len(mergedEntries))})
// -------------------------------------------------------
// Phase 7: Cold hydration -- read files that need blob fetch
// -------------------------------------------------------
h := hydrator.New(git)
h.Start(4, cfg) // 4 workers
defer h.Stop()
// Pick a sample of files to hydrate (small text files first, up to 20)
sampleFiles := pickHydrationSample(nodes, 20)
results = append(results, timing{"hydration sample", 0,
fmt.Sprintf("%d files selected", len(sampleFiles))})
// Cold hydration (one by one, measuring each)
var hydrateDurs []time.Duration
var hydrateTotalBytes int64
for _, n := range sampleFiles {
start = time.Now()
_, size, err := h.EnsureHydrated(ctx, cfg, n)
dur := time.Since(start)
if err != nil {
t.Logf("hydrate %s: %v", n.Path, err)
continue
}
hydrateDurs = append(hydrateDurs, dur)
hydrateTotalBytes += size
}
if len(hydrateDurs) > 0 {
p50, p95, p99 := percentiles(hydrateDurs)
results = append(results, timing{"cold hydration (per file)", 0,
fmt.Sprintf("n=%d, p50=%v, p95=%v, p99=%v, total=%.1f KB",
len(hydrateDurs), p50.Round(time.Millisecond), p95.Round(time.Millisecond), p99.Round(time.Millisecond),
float64(hydrateTotalBytes)/1024)})
var totalHydrate time.Duration
for _, d := range hydrateDurs {
totalHydrate += d
}
results = append(results, timing{"cold hydration (total wall)", totalHydrate,
fmt.Sprintf("%d files, %.1f KB", len(hydrateDurs), float64(hydrateTotalBytes)/1024)})
}
// -------------------------------------------------------
// Phase 8: Warm cache reads -- re-read the same files
// -------------------------------------------------------
var warmDurs []time.Duration
for _, n := range sampleFiles {
start = time.Now()
_, _, err := h.EnsureHydrated(ctx, cfg, n)
dur := time.Since(start)
if err != nil {
continue
}
warmDurs = append(warmDurs, dur)
}
if len(warmDurs) > 0 {
p50, p95, p99 := percentiles(warmDurs)
results = append(results, timing{"warm cache read (per file)", 0,
fmt.Sprintf("n=%d, p50=%v, p95=%v, p99=%v",
len(warmDurs), p50, p95, p99)})
}
// -------------------------------------------------------
// Phase 9: Batch hydration -- hydrate many files concurrently
// -------------------------------------------------------
batchFiles := pickHydrationSample(nodes, 100)
// Remove files we already hydrated
hydrated := map[string]bool{}
for _, n := range sampleFiles {
hydrated[n.ObjectOID] = true
}
var freshBatch []model.BaseNode
for _, n := range batchFiles {
if !hydrated[n.ObjectOID] {
freshBatch = append(freshBatch, n)
}
}
if len(freshBatch) > 0 {
start = time.Now()
type hydrateResult struct {
size int64
err error
}
results_ch := make(chan hydrateResult, len(freshBatch))
for _, n := range freshBatch {
go func() {
_, size, err := h.EnsureHydrated(ctx, cfg, n)
results_ch <- hydrateResult{size, err}
}()
}
var batchBytes int64
var batchErrors int
for i := 0; i < len(freshBatch); i++ {
r := <-results_ch
if r.err != nil {
batchErrors++
} else {
batchBytes += r.size
}
}
batchDur := time.Since(start)
results = append(results, timing{"batch hydration (concurrent)", batchDur,
fmt.Sprintf("%d files, %.1f KB, %d errors, throughput=%.1f files/s",
len(freshBatch), float64(batchBytes)/1024, batchErrors,
float64(len(freshBatch)-batchErrors)/batchDur.Seconds())})
}
// -------------------------------------------------------
// Phase 10: Refresh -- fetch + re-index + re-publish
// -------------------------------------------------------
start = time.Now()
fetchErr := git.Fetch(ctx, cfg)
fetchDur := time.Since(start)
if fetchErr != nil {
results = append(results, timing{"fetch", fetchDur, "error: " + fetchErr.Error()})
} else {
results = append(results, timing{"fetch (no-op, already up to date)", fetchDur, ""})
}
// Re-index (simulates what watcher triggers)
start = time.Now()
nodes2, err := git.BuildTreeIndex(ctx, cfg, headOID)
if err != nil {
t.Fatalf("re-index: %v", err)
}
reindexDur := time.Since(start)
results = append(results, timing{"re-index (same HEAD)", reindexDur,
fmt.Sprintf("%d nodes", len(nodes2))})
start = time.Now()
gen2, err := snap.PublishGeneration(ctx, headOID, headRef, nodes2)
if err != nil {
t.Fatalf("re-publish: %v", err)
}
republishDur := time.Since(start)
results = append(results, timing{"re-publish snapshot", republishDur,
fmt.Sprintf("gen=%d", gen2)})
refreshTotal := fetchDur + reindexDur + republishDur
results = append(results, timing{"REFRESH TOTAL", refreshTotal,
"fetch + reindex + publish"})
snap.Close()
return results
}
// printReport outputs a formatted timing report for a repo.
func printReport(t *testing.T, repo repoSpec, results []timing) {
t.Helper()
t.Logf("\n")
t.Logf("═══════════════════════════════════════════════════════")
t.Logf(" BENCHMARK: %s (%s)", repo.name, repo.url)
t.Logf("═══════════════════════════════════════════════════════")
for _, r := range results {
if r.dur > 0 {
t.Logf(" %-40s %10s %s", r.label, r.dur.Round(time.Millisecond), r.extra)
} else {
t.Logf(" %-40s %10s %s", r.label, "", r.extra)
}
}
t.Logf("═══════════════════════════════════════════════════════")
}
// findDeepDir returns a directory at the given depth with the most children.
func findDeepDir(nodes []model.BaseNode, targetDepth int) string {
childCount := map[string]int{}
for _, n := range nodes {
if n.Type == "dir" {
continue
}
depth := strings.Count(n.Path, "/")
if depth >= targetDepth {
parts := strings.SplitN(n.Path, "/", targetDepth+1)
dir := strings.Join(parts[:targetDepth], "/")
childCount[dir]++
}
}
var best string
var bestCount int
for dir, count := range childCount {
if count > bestCount {
best = dir
bestCount = count
}
}
return best
}
// pickHydrationSample selects files for hydration testing, preferring small
// text files to avoid slow binary downloads dominating the benchmark.
func pickHydrationSample(nodes []model.BaseNode, count int) []model.BaseNode {
// Collect files with known OIDs, sorted by priority (text files first, then by size)
type candidate struct {
node model.BaseNode
priority int
}
var candidates []candidate
seen := map[string]bool{}
for _, n := range nodes {
if n.Type != "file" || n.ObjectOID == "" || seen[n.ObjectOID] {
continue
}
seen[n.ObjectOID] = true
pri := hydrator.ClassifyPriority(n.Path)
candidates = append(candidates, candidate{n, pri})
}
sort.Slice(candidates, func(i, j int) bool {
if candidates[i].priority != candidates[j].priority {
return candidates[i].priority > candidates[j].priority
}
return candidates[i].node.SizeBytes < candidates[j].node.SizeBytes
})
if len(candidates) > count {
candidates = candidates[:count]
}
out := make([]model.BaseNode, len(candidates))
for i, c := range candidates {
out[i] = c.node
}
return out
}
func percentiles(durs []time.Duration) (p50, p95, p99 time.Duration) {
if len(durs) == 0 {
return 0, 0, 0
}
sorted := make([]time.Duration, len(durs))
copy(sorted, durs)
slices.Sort(sorted)
p50 = sorted[pctIndex(len(sorted), 50)]
p95 = sorted[pctIndex(len(sorted), 95)]
p99 = sorted[pctIndex(len(sorted), 99)]
return
}
func pctIndex(n, pct int) int {
idx := int(math.Ceil(float64(n)*float64(pct)/100)) - 1
if idx < 0 {
return 0
}
if idx >= n {
return n - 1
}
return idx
}