|
| 1 | +package tree |
| 2 | + |
| 3 | +import ( |
| 4 | + "container/list" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// Q2, MEASURED BEFORE IT IS BUILT. Gluck endorsed epoch buckets over a heap |
| 10 | +// for the eviction scan, with the instruction to measure bucket MAINTENANCE |
| 11 | +// against the scan it replaces first. |
| 12 | +// |
| 13 | +// The two costs sit on DIFFERENT PATHS and that is the whole finding: |
| 14 | +// |
| 15 | +// THE SCAN is on the daemon's standing sweep. A read never evicts |
| 16 | +// (charge raises pressure; Sweep lowers it), so its cost is |
| 17 | +// paid once per beat by one goroutine. |
| 18 | +// THE INDEX would be maintained on TOUCH, which is on the READ PATH, |
| 19 | +// where every reader is concurrent and nothing shared is |
| 20 | +// written today. |
| 21 | +// |
| 22 | +// So the comparison is not "R^2 versus log R". It is "one background walk per |
| 23 | +// beat versus a shared write per read", and this file measures both. |
| 24 | + |
| 25 | +func buildCache(tb testing.TB, b *Budget, runs int) *Cache[fatUnit] { |
| 26 | + c := New[fatUnit]( |
| 27 | + func(co Coord) ([]fatUnit, error) { |
| 28 | + out := make([]fatUnit, 0, co.To-co.From) |
| 29 | + for k := co.From + 1; k <= co.To; k++ { |
| 30 | + out = append(out, fatUnit{key: k, payload: make([]byte, 1024)}) |
| 31 | + } |
| 32 | + return out, nil |
| 33 | + }, |
| 34 | + b, |
| 35 | + func(u fatUnit) int { return len(u.payload) }, |
| 36 | + func(u fatUnit) uint64 { return u.key }, |
| 37 | + ) |
| 38 | + tb.Cleanup(c.Close) |
| 39 | + lineage := []Ref{{Node: "p"}} |
| 40 | + for i := 0; len(c.runs("p")) < runs; i++ { |
| 41 | + lo := uint64(i * runChunk) |
| 42 | + if _, err := c.Range(lineage, lo, lo+runChunk); err != nil { |
| 43 | + tb.Fatal(err) |
| 44 | + } |
| 45 | + if i > runs*4 { |
| 46 | + tb.Fatalf("fixture: %d runs after %d ranges", len(c.runs("p")), i) |
| 47 | + } |
| 48 | + } |
| 49 | + return c |
| 50 | +} |
| 51 | + |
| 52 | +// fatUnit is a 1 KiB unit: the fixture measures RUN COUNT, and a run is cut |
| 53 | +// by bytes, so the unit size is what decides how many runs a range builds. |
| 54 | +type fatUnit struct { |
| 55 | + key uint64 |
| 56 | + payload []byte |
| 57 | +} |
| 58 | + |
| 59 | +// THE SCAN, AT PRODUCTION SIZE. The segment cache holds 32 MiB in runs cut at |
| 60 | +// 32 KiB, so R is in the high hundreds to low thousands; the decoded and |
| 61 | +// composed tenants add their own. This times a FULL SWEEP -- the compounding |
| 62 | +// case, one rescan per dropped run. |
| 63 | +func BenchmarkSweepFull(b *testing.B) { |
| 64 | + for _, runs := range []int{256, 1024, 4096} { |
| 65 | + b.Run(sizeName(runs), func(b *testing.B) { |
| 66 | + for i := 0; i < b.N; i++ { |
| 67 | + b.StopTimer() |
| 68 | + bud := NewBudget(0) |
| 69 | + c := buildCache(b, bud, runs) |
| 70 | + resident := len(c.runs("p")) |
| 71 | + b.StartTimer() |
| 72 | + dropped, _ := bud.TrimIdle(0) |
| 73 | + b.StopTimer() |
| 74 | + if dropped != resident-1 { |
| 75 | + b.Fatalf("dropped %d of %d", dropped, resident) |
| 76 | + } |
| 77 | + c.Close() |
| 78 | + b.StartTimer() |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// ONE EVICTION under pressure, which is what the standing sweep actually does |
| 85 | +// per beat when a tenant has just charged past its limit. |
| 86 | +func BenchmarkSweepOneEviction(b *testing.B) { |
| 87 | + for _, runs := range []int{256, 1024, 4096} { |
| 88 | + b.Run(sizeName(runs), func(b *testing.B) { |
| 89 | + bud := NewBudget(0) |
| 90 | + c := buildCache(b, bud, runs) |
| 91 | + resident, _, _ := bud.Stats() |
| 92 | + b.ResetTimer() |
| 93 | + for i := 0; i < b.N; i++ { |
| 94 | + bud.SetLimit(resident - int64(i+1)*1024) |
| 95 | + bud.pressure.Store(true) |
| 96 | + bud.Sweep() |
| 97 | + } |
| 98 | + b.StopTimer() |
| 99 | + c.Close() |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// THE MAINTENANCE, ON THE PATH IT WOULD LIVE ON. An epoch bucket index must be |
| 105 | +// updated when a run is TOUCHED, and a touch happens on every warm read. Today |
| 106 | +// that is one atomic store, and only when the epoch is stale. |
| 107 | +// |
| 108 | +// The in-run control is the read itself: both arms do the same Range over the |
| 109 | +// same warm cache in the same binary, and the only difference is what the |
| 110 | +// touch does. |
| 111 | +func BenchmarkTouchShape(b *testing.B) { |
| 112 | + bud := NewBudget(0) |
| 113 | + c := buildCache(b, bud, 256) |
| 114 | + lineage := []Ref{{Node: "p"}} |
| 115 | + |
| 116 | + // ONE UNIT PER READ. A 64-unit Range copies 64 KiB and a mutex |
| 117 | + // acquisition disappears inside it -- the mistake that produced a |
| 118 | + // retracted -43.8% on this very package (plans/tree-shaped-log.md). |
| 119 | + b.Run("today_atomic_epoch", func(b *testing.B) { |
| 120 | + b.RunParallel(func(pb *testing.PB) { |
| 121 | + var i uint64 |
| 122 | + for pb.Next() { |
| 123 | + k := i % 2000 |
| 124 | + i++ |
| 125 | + if _, err := c.Range(lineage, k, k+1); err != nil { |
| 126 | + b.Fatal(err) |
| 127 | + } |
| 128 | + } |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + // The bucket arm: the same read, plus the shared write an index needs. |
| 133 | + // A container/list under one mutex is the cheapest honest stand-in -- |
| 134 | + // UIBudget's deleted LRU was exactly this shape, and so is any bucket |
| 135 | + // map that must move a run between two buckets atomically. |
| 136 | + var mu sync.Mutex |
| 137 | + l := list.New() |
| 138 | + elems := make([]*list.Element, 256) |
| 139 | + for i := range elems { |
| 140 | + elems[i] = l.PushFront(i) |
| 141 | + } |
| 142 | + b.Run("with_bucket_move", func(b *testing.B) { |
| 143 | + b.RunParallel(func(pb *testing.PB) { |
| 144 | + var i uint64 |
| 145 | + for pb.Next() { |
| 146 | + k := i % 2000 |
| 147 | + if _, err := c.Range(lineage, k, k+1); err != nil { |
| 148 | + b.Fatal(err) |
| 149 | + } |
| 150 | + mu.Lock() |
| 151 | + l.MoveToFront(elems[i%256]) |
| 152 | + mu.Unlock() |
| 153 | + i++ |
| 154 | + } |
| 155 | + }) |
| 156 | + }) |
| 157 | +} |
| 158 | + |
| 159 | +func sizeName(n int) string { |
| 160 | + switch n { |
| 161 | + case 256: |
| 162 | + return "R256" |
| 163 | + case 1024: |
| 164 | + return "R1024" |
| 165 | + case 4096: |
| 166 | + return "R4096" |
| 167 | + } |
| 168 | + return "R?" |
| 169 | +} |
0 commit comments