|
| 1 | +// Copyright 2026 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package ptstorage_test |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "sync/atomic" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/cockroachdb/cockroach/pkg/base" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts/ptpb" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts/ptstorage" |
| 18 | + "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" |
| 19 | + "github.com/cockroachdb/cockroach/pkg/sql/isql" |
| 20 | + "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" |
| 21 | + "github.com/cockroachdb/cockroach/pkg/util/ctxgroup" |
| 22 | + "github.com/cockroachdb/cockroach/pkg/util/hlc" |
| 23 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 24 | + "github.com/cockroachdb/cockroach/pkg/util/uuid" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +// workerCounts is the parallelism sweep shared by the throughput benchmarks |
| 29 | +// in this file. |
| 30 | +// var workerCounts = []int{1, 2, 4, 8, 16, 32, 64, 128} |
| 31 | +var workerCounts = []int{128} |
| 32 | + |
| 33 | +// startBenchServer brings up a single-node in-process server suitable for the |
| 34 | +// throughput benchmarks. |
| 35 | +func startBenchServer(b *testing.B) (protectedts.Storage, func()) { |
| 36 | + ctx := context.Background() |
| 37 | + srv := serverutils.StartServerOnly(b, base.TestServerArgs{}) |
| 38 | + s := srv.ApplicationLayer() |
| 39 | + |
| 40 | + protectedts.MaxBytes.Override(ctx, &s.ClusterSettings().SV, 0) |
| 41 | + protectedts.MaxSpans.Override(ctx, &s.ClusterSettings().SV, 0) |
| 42 | + |
| 43 | + pts := ptstorage.WithDatabase( |
| 44 | + ptstorage.New(s.ClusterSettings(), &protectedts.TestingKnobs{}), |
| 45 | + s.InternalDB().(isql.DB), |
| 46 | + ) |
| 47 | + return pts, func() { srv.Stopper().Stop(ctx) } |
| 48 | +} |
| 49 | + |
| 50 | +// runConcurrent invokes op b.N times spread across `workers` goroutines and |
| 51 | +// then waits for them all to finish. op receives the per-invocation index in |
| 52 | +// [0, b.N), which is useful for indexing into a per-iteration data set. |
| 53 | +// |
| 54 | +// Errors returned by op are counted and reported as the "errs/op" metric |
| 55 | +// rather than failing the benchmark. Under heavy contention some operations |
| 56 | +// can exhaust the internal kv retry budget; counting rather than failing lets |
| 57 | +// us still produce a comparable baseline number. |
| 58 | +func runConcurrent(b *testing.B, ctx context.Context, workers int, op func(idx int) error) { |
| 59 | + var ( |
| 60 | + next atomic.Int64 |
| 61 | + errs atomic.Int64 |
| 62 | + ) |
| 63 | + |
| 64 | + b.ResetTimer() |
| 65 | + err := ctxgroup.GroupWorkers(ctx, workers, func(context.Context, int) error { |
| 66 | + for { |
| 67 | + idx := int(next.Add(1) - 1) |
| 68 | + if idx >= b.N { |
| 69 | + return nil |
| 70 | + } |
| 71 | + if err := op(idx); err != nil { |
| 72 | + errs.Add(1) |
| 73 | + } |
| 74 | + } |
| 75 | + }) |
| 76 | + require.NoError(b, err) |
| 77 | + b.StopTimer() |
| 78 | + b.ReportMetric(float64(errs.Load())/float64(b.N), "errs/op") |
| 79 | +} |
| 80 | + |
| 81 | +// BenchmarkProtect measures the throughput of protectedts.Storage.Protect |
| 82 | +// calls as a function of writer concurrency. Each iteration writes one new |
| 83 | +// record. Per-op wall-clock time reported by the framework is the inverse of |
| 84 | +// sustained throughput: ops/sec = 1e9 / ns/op. |
| 85 | +func BenchmarkProtect(b *testing.B) { |
| 86 | + defer log.Scope(b).Close(b) |
| 87 | + ctx := context.Background() |
| 88 | + |
| 89 | + pts, stop := startBenchServer(b) |
| 90 | + defer stop() |
| 91 | + |
| 92 | + now := hlc.Timestamp{WallTime: 1} |
| 93 | + target := ptpb.MakeSchemaObjectsTarget([]descpb.ID{42}) |
| 94 | + |
| 95 | + for _, workers := range workerCounts { |
| 96 | + b.Run(fmt.Sprintf("workers=%d", workers), func(b *testing.B) { |
| 97 | + runConcurrent(b, ctx, workers, func(_ int) error { |
| 98 | + rec := ptpb.Record{ |
| 99 | + ID: uuid.MakeV4().GetBytes(), |
| 100 | + Timestamp: now, |
| 101 | + Mode: ptpb.PROTECT_AFTER, |
| 102 | + Target: target, |
| 103 | + } |
| 104 | + return pts.Protect(ctx, &rec) |
| 105 | + }) |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// BenchmarkRelease measures the throughput of protectedts.Storage.Release |
| 111 | +// calls as a function of writer concurrency. Each sub-benchmark first |
| 112 | +// pre-populates b.N records (the setup time is excluded from the |
| 113 | +// measurement) and then releases each one exactly once. |
| 114 | +// |
| 115 | +// Like Protect, Release is a read-modify-write against the singleton meta |
| 116 | +// row, so it shares Protect's contention shape on that row. |
| 117 | +func BenchmarkRelease(b *testing.B) { |
| 118 | + defer log.Scope(b).Close(b) |
| 119 | + ctx := context.Background() |
| 120 | + |
| 121 | + pts, stop := startBenchServer(b) |
| 122 | + defer stop() |
| 123 | + |
| 124 | + now := hlc.Timestamp{WallTime: 1} |
| 125 | + target := ptpb.MakeSchemaObjectsTarget([]descpb.ID{42}) |
| 126 | + |
| 127 | + for _, workers := range workerCounts { |
| 128 | + b.Run(fmt.Sprintf("workers=%d", workers), func(b *testing.B) { |
| 129 | + ids := prePopulate(b, ctx, pts, workers, b.N, now, target) |
| 130 | + runConcurrent(b, ctx, workers, func(idx int) error { |
| 131 | + return pts.Release(ctx, ids[idx]) |
| 132 | + }) |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// prePopulate inserts n protected timestamp records in parallel using |
| 138 | +// `workers` goroutines and returns their IDs. It is intended for use as |
| 139 | +// benchmark setup (before b.ResetTimer); a Protect failure here is fatal |
| 140 | +// because it means the benchmark has nothing meaningful to measure. |
| 141 | +func prePopulate( |
| 142 | + b *testing.B, |
| 143 | + ctx context.Context, |
| 144 | + pts protectedts.Storage, |
| 145 | + workers, n int, |
| 146 | + ts hlc.Timestamp, |
| 147 | + target *ptpb.Target, |
| 148 | +) []uuid.UUID { |
| 149 | + ids := make([]uuid.UUID, n) |
| 150 | + var next atomic.Int64 |
| 151 | + err := ctxgroup.GroupWorkers(ctx, workers, func(context.Context, int) error { |
| 152 | + for { |
| 153 | + idx := int(next.Add(1) - 1) |
| 154 | + if idx >= n { |
| 155 | + return nil |
| 156 | + } |
| 157 | + id := uuid.MakeV4() |
| 158 | + rec := ptpb.Record{ |
| 159 | + ID: id.GetBytes(), |
| 160 | + Timestamp: ts, |
| 161 | + Mode: ptpb.PROTECT_AFTER, |
| 162 | + Target: target, |
| 163 | + } |
| 164 | + if err := pts.Protect(ctx, &rec); err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + ids[idx] = id |
| 168 | + } |
| 169 | + }) |
| 170 | + require.NoError(b, err) |
| 171 | + return ids |
| 172 | +} |
0 commit comments