|
| 1 | +package benchmark |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/authzed/spicedb/internal/datastore/mysql" |
| 12 | + "github.com/authzed/spicedb/internal/testfixtures" |
| 13 | + testdatastore "github.com/authzed/spicedb/internal/testserver/datastore" |
| 14 | + "github.com/authzed/spicedb/internal/testserver/datastore/config" |
| 15 | + dsconfig "github.com/authzed/spicedb/pkg/cmd/datastore" |
| 16 | + "github.com/authzed/spicedb/pkg/datastore" |
| 17 | + "github.com/authzed/spicedb/pkg/datastore/options" |
| 18 | + "github.com/authzed/spicedb/pkg/datastore/queryshape" |
| 19 | + "github.com/authzed/spicedb/pkg/tuple" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + mysqlBenchNumDocuments = 5000 |
| 24 | + mysqlBenchUsersPerDoc = 10 |
| 25 | +) |
| 26 | + |
| 27 | +// BenchmarkMySQLRelQueries benchmarks the relationship-query shapes affected by the |
| 28 | +// MySQL index-hint / pagination work. Requires Docker (spins up mysql:8.4). |
| 29 | +// |
| 30 | +// Run with: |
| 31 | +// |
| 32 | +// go test ./internal/datastore/benchmark/ -bench BenchmarkMySQLRelQueries \ |
| 33 | +// -benchmem -run '^$' -timeout 30m |
| 34 | +func BenchmarkMySQLRelQueries(b *testing.B) { |
| 35 | + b.StopTimer() |
| 36 | + ctx := b.Context() |
| 37 | + |
| 38 | + engine := testdatastore.RunDatastoreEngine(b, mysql.Engine) |
| 39 | + ds := engine.NewDatastore(b, config.DatastoreConfigInitFunc( |
| 40 | + b, |
| 41 | + dsconfig.WithRevisionQuantization(5*time.Second), |
| 42 | + dsconfig.WithGCWindow(2*time.Hour), |
| 43 | + dsconfig.WithGCInterval(1*time.Hour), |
| 44 | + dsconfig.WithWatchBufferLength(1000), |
| 45 | + dsconfig.WithWriteAcquisitionTimeout(5*time.Second), |
| 46 | + )) |
| 47 | + b.Cleanup(func() { _ = ds.Close() }) |
| 48 | + |
| 49 | + ds, _ = testfixtures.StandardDatastoreWithSchema(b, ds) |
| 50 | + |
| 51 | + // Write a meaningful amount of data. Subject user "0" ends up a viewer of every |
| 52 | + // document, which exercises the reverse (MatchingResourcesForSubject) shape well. |
| 53 | + for docNum := range mysqlBenchNumDocuments { |
| 54 | + _, err := ds.ReadWriteTx(ctx, func(ctx context.Context, rwt datastore.ReadWriteTransaction) error { |
| 55 | + updates := make([]tuple.RelationshipUpdate, 0, mysqlBenchUsersPerDoc) |
| 56 | + for userNum := range mysqlBenchUsersPerDoc { |
| 57 | + updates = append(updates, tuple.Create(docViewer(strconv.Itoa(docNum), strconv.Itoa(userNum)))) |
| 58 | + } |
| 59 | + return rwt.WriteRelationships(ctx, updates) |
| 60 | + }) |
| 61 | + require.NoError(b, err) |
| 62 | + } |
| 63 | + |
| 64 | + time.Sleep(1 * time.Second) |
| 65 | + |
| 66 | + headRevResult, err := ds.HeadRevision(ctx) |
| 67 | + require.NoError(b, err) |
| 68 | + headRev := headRevResult.Revision |
| 69 | + |
| 70 | + drain := func(b *testing.B, iter datastore.RelationshipIterator) int { |
| 71 | + count := 0 |
| 72 | + for _, err := range iter { |
| 73 | + require.NoError(b, err) |
| 74 | + count++ |
| 75 | + } |
| 76 | + return count |
| 77 | + } |
| 78 | + |
| 79 | + b.StartTimer() |
| 80 | + |
| 81 | + // Forward: full resource + subject (direct check). |
| 82 | + b.Run("ForwardDirectCheck", func(b *testing.B) { |
| 83 | + for range b.N { |
| 84 | + iter, err := ds.SnapshotReader(headRev).QueryRelationships(ctx, datastore.RelationshipsFilter{ |
| 85 | + OptionalResourceType: testfixtures.DocumentNS.Name, |
| 86 | + OptionalResourceIds: []string{"100"}, |
| 87 | + OptionalResourceRelation: "viewer", |
| 88 | + OptionalSubjectsSelectors: []datastore.SubjectsSelector{{ |
| 89 | + OptionalSubjectType: testfixtures.UserNS.Name, |
| 90 | + OptionalSubjectIds: []string{"0"}, |
| 91 | + }}, |
| 92 | + }, options.WithQueryShape(queryshape.CheckPermissionSelectDirectSubjects)) |
| 93 | + require.NoError(b, err) |
| 94 | + require.Equal(b, 1, drain(b, iter)) |
| 95 | + } |
| 96 | + }) |
| 97 | + |
| 98 | + // Forward: all subjects for one resource. |
| 99 | + b.Run("ForwardAllSubjects", func(b *testing.B) { |
| 100 | + for range b.N { |
| 101 | + iter, err := ds.SnapshotReader(headRev).QueryRelationships(ctx, datastore.RelationshipsFilter{ |
| 102 | + OptionalResourceType: testfixtures.DocumentNS.Name, |
| 103 | + OptionalResourceIds: []string{"100"}, |
| 104 | + OptionalResourceRelation: "viewer", |
| 105 | + }, options.WithQueryShape(queryshape.AllSubjectsForResources)) |
| 106 | + require.NoError(b, err) |
| 107 | + require.Equal(b, mysqlBenchUsersPerDoc, drain(b, iter)) |
| 108 | + } |
| 109 | + }) |
| 110 | + |
| 111 | + // Reverse: all resources for a specific subject (Finding 1 main target). |
| 112 | + b.Run("ReverseMatchingResources", func(b *testing.B) { |
| 113 | + for range b.N { |
| 114 | + iter, err := ds.SnapshotReader(headRev).ReverseQueryRelationships(ctx, datastore.SubjectsFilter{ |
| 115 | + SubjectType: testfixtures.UserNS.Name, |
| 116 | + OptionalSubjectIds: []string{"0"}, |
| 117 | + }, |
| 118 | + options.WithResRelation(&options.ResourceRelation{ |
| 119 | + Namespace: testfixtures.DocumentNS.Name, |
| 120 | + Relation: "viewer", |
| 121 | + }), |
| 122 | + options.WithSortForReverse(options.BySubject), |
| 123 | + options.WithQueryShapeForReverse(queryshape.MatchingResourcesForSubject), |
| 124 | + ) |
| 125 | + require.NoError(b, err) |
| 126 | + require.Equal(b, mysqlBenchNumDocuments, drain(b, iter)) |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + // Enumerate by type + relation, no object_id (Finding 3 candidate workload). |
| 131 | + b.Run("EnumerateByTypeAndRelation", func(b *testing.B) { |
| 132 | + for range b.N { |
| 133 | + iter, err := ds.SnapshotReader(headRev).QueryRelationships(ctx, datastore.RelationshipsFilter{ |
| 134 | + OptionalResourceType: testfixtures.DocumentNS.Name, |
| 135 | + OptionalResourceRelation: "viewer", |
| 136 | + }, options.WithQueryShape(queryshape.Varying)) |
| 137 | + require.NoError(b, err) |
| 138 | + require.Equal(b, mysqlBenchNumDocuments*mysqlBenchUsersPerDoc, drain(b, iter)) |
| 139 | + } |
| 140 | + }) |
| 141 | + |
| 142 | + // Cursor-paginated read (Finding 4 target). |
| 143 | + b.Run("PaginatedRead", func(b *testing.B) { |
| 144 | + const pageSize = uint64(500) |
| 145 | + for range b.N { |
| 146 | + var after options.Cursor |
| 147 | + total := 0 |
| 148 | + for { |
| 149 | + limit := pageSize |
| 150 | + opts := []options.QueryOptionsOption{ |
| 151 | + options.WithSort(options.ByResource), |
| 152 | + options.WithLimit(&limit), |
| 153 | + options.WithQueryShape(queryshape.Varying), |
| 154 | + } |
| 155 | + if after != nil { |
| 156 | + opts = append(opts, options.WithAfter(after)) |
| 157 | + } |
| 158 | + iter, err := ds.SnapshotReader(headRev).QueryRelationships(ctx, datastore.RelationshipsFilter{ |
| 159 | + OptionalResourceType: testfixtures.DocumentNS.Name, |
| 160 | + OptionalResourceRelation: "viewer", |
| 161 | + }, opts...) |
| 162 | + require.NoError(b, err) |
| 163 | + |
| 164 | + pageCount := 0 |
| 165 | + var last tuple.Relationship |
| 166 | + for rel, err := range iter { |
| 167 | + require.NoError(b, err) |
| 168 | + last = rel |
| 169 | + pageCount++ |
| 170 | + } |
| 171 | + total += pageCount |
| 172 | + if uint64(pageCount) < pageSize { |
| 173 | + break |
| 174 | + } |
| 175 | + lastCopy := last |
| 176 | + after = &lastCopy |
| 177 | + } |
| 178 | + require.Equal(b, mysqlBenchNumDocuments*mysqlBenchUsersPerDoc, total) |
| 179 | + } |
| 180 | + }) |
| 181 | +} |
0 commit comments