MCPcopy Index your code
hub / github.com/coder/coder / BenchmarkRBACValueAllocation

Function BenchmarkRBACValueAllocation

coderd/rbac/roles_internal_test.go:22–66  ·  view source on GitHub ↗

BenchmarkRBACValueAllocation benchmarks the cost of allocating a rego input value. By default, `ast.InterfaceToValue` is used to convert the input, which uses json marshaling under the hood. Currently ast.Object.insert() is the slowest part of the process and allocates the most amount of bytes. Thi

(b *testing.B)

Source from the content-addressed store, hash-verified

20// data and uses a lot of extra memory for handling things like sort order.
21// A possible large improvement would be to implement the ast.Value interface directly.
22func BenchmarkRBACValueAllocation(b *testing.B) {
23 actor := Subject{
24 Roles: RoleIdentifiers{ScopedRoleOrgMember(uuid.New()), ScopedRoleOrgAdmin(uuid.New()), RoleMember()},
25 ID: uuid.NewString(),
26 Scope: ScopeAll,
27 Groups: []string{uuid.NewString(), uuid.NewString(), uuid.NewString()},
28 }
29 obj := ResourceTemplate.
30 WithID(uuid.New()).
31 InOrg(uuid.New()).
32 WithOwner(uuid.NewString()).
33 WithGroupACL(map[string][]policy.Action{
34 uuid.NewString(): {policy.ActionRead, policy.ActionCreate},
35 uuid.NewString(): {policy.ActionRead, policy.ActionCreate},
36 uuid.NewString(): {policy.ActionRead, policy.ActionCreate},
37 }).
38 WithACLUserList(map[string][]policy.Action{
39 uuid.NewString(): {policy.ActionRead, policy.ActionCreate},
40 uuid.NewString(): {policy.ActionRead, policy.ActionCreate},
41 })
42
43 jsonSubject := authSubject{
44 ID: actor.ID,
45 Roles: must(actor.Roles.Expand()),
46 Groups: actor.Groups,
47 Scope: must(actor.Scope.Expand()),
48 }
49
50 b.Run("ManualRegoValue", func(b *testing.B) {
51 for i := 0; i < b.N; i++ {
52 _, err := regoInputValue(actor, policy.ActionRead, obj)
53 require.NoError(b, err)
54 }
55 })
56 b.Run("JSONRegoValue", func(b *testing.B) {
57 for i := 0; i < b.N; i++ {
58 _, err := ast.InterfaceToValue(map[string]interface{}{
59 "subject": jsonSubject,
60 "action": policy.ActionRead,
61 "object": obj,
62 })
63 require.NoError(b, err)
64 }
65 })
66}
67
68// TestRegoInputValue ensures the custom rego input parser returns the
69// same value as the default json parser. The json parser is always correct,

Callers

nothing calls this directly

Calls 13

ScopedRoleOrgMemberFunction · 0.85
ScopedRoleOrgAdminFunction · 0.85
RoleMemberFunction · 0.85
regoInputValueFunction · 0.85
WithACLUserListMethod · 0.80
WithGroupACLMethod · 0.80
WithOwnerMethod · 0.80
InOrgMethod · 0.80
mustFunction · 0.70
NewMethod · 0.65
ExpandMethod · 0.65
RunMethod · 0.65

Tested by

no test coverage detected