@@ -2,6 +2,7 @@ package rbac_test
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"testing"
6
7
7
8
"github.com/stretchr/testify/require"
@@ -10,6 +11,33 @@ import (
10
11
"github.com/coder/coder/coderd/rbac"
11
12
)
12
13
14
+ // BenchmarkCacher benchmarks the performance of the cacher with a given
15
+ // cache size. The expected cache size in prod will usually be 1-2. In Filter
16
+ // cases it can get as high as 10.
17
+ func BenchmarkCacher (b * testing.B ) {
18
+ b .ResetTimer ()
19
+ // Size of the cache.
20
+ sizes := []int {1 , 10 , 100 , 1000 }
21
+ for _ , size := range sizes {
22
+ b .Run (fmt .Sprintf ("Size%d" , size ), func (b * testing.B ) {
23
+ ctx := rbac .WithCacheCtx (context .Background ())
24
+ authz := rbac .Cacher (& coderdtest.FakeAuthorizer {AlwaysReturn : nil })
25
+ for i := 0 ; i < size ; i ++ {
26
+ // Preload the cache of a given size
27
+ subj , obj , action := coderdtest .RandomRBACSubject (), coderdtest .RandomRBACObject (), coderdtest .RandomRBACAction ()
28
+ _ = authz .Authorize (ctx , subj , action , obj )
29
+ }
30
+
31
+ // Cache is loaded as a slice, so this cache hit is always the last element.
32
+ subj , obj , action := coderdtest .RandomRBACSubject (), coderdtest .RandomRBACObject (), coderdtest .RandomRBACAction ()
33
+ b .ResetTimer ()
34
+ for i := 0 ; i < b .N ; i ++ {
35
+ _ = authz .Authorize (ctx , subj , action , obj )
36
+ }
37
+ })
38
+ }
39
+ }
40
+
13
41
func TestCacher (t * testing.T ) {
14
42
t .Parallel ()
15
43
0 commit comments