Skip to content

Commit

Permalink
Benchmark AddConstraint function (#168)
Browse files Browse the repository at this point in the history
* Benchmark AddConstraint function

We didn't expect this to be problematic, but it's nice to have a
benchmark for this as we do intend to make some changes to what happens
when Constraints are added.

Fix "ModuleSimple" so it actually compiles. I was originally against
having the error check in the benchmark since it's slightly slower, but
(1) the performance difference is negligible and (2) we want to be sure
the CT is actually being compiled, so it's worth the minuscule
performance cost.

Signed-off-by: Will Beason <willbeason@google.com>

* Actually perform error check

Signed-off-by: Will Beason <willbeason@google.com>
  • Loading branch information
Will Beason (he/him) authored Jan 12, 2022
1 parent 8c2d6e2 commit 8306a8d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
59 changes: 59 additions & 0 deletions constraint/pkg/client/client_addconstraint_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package client

import (
"context"
"strconv"
"testing"

"github.com/open-policy-agent/frameworks/constraint/pkg/client/drivers/local"
"github.com/open-policy-agent/frameworks/constraint/pkg/core/templates"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func makeConstraint(i int, ct *templates.ConstraintTemplate) *unstructured.Unstructured {
u := &unstructured.Unstructured{}
u.SetName(strconv.Itoa(i))
u.SetGroupVersionKind(schema.GroupVersionKind{
Group: "constraints.gatekeeper.sh",
Version: "v1beta1",
Kind: ct.Spec.CRD.Spec.Names.Kind,
})

return u
}

func BenchmarkClient_AddConstraint(b *testing.B) {
ctx := context.Background()

ct := makeConstraintTemplate(0, makeModuleSimple)

d := local.New()
backend, err := NewBackend(Driver(d))
if err != nil {
b.Fatal(err)
}

targets := Targets(&handler{})

c, err := backend.NewClient(targets)
if err != nil {
b.Fatal(err)
}

_, err = c.AddTemplate(ct)
if err != nil {
b.Fatal(err)
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
constraint := makeConstraint(i, ct)

_, err = c.AddConstraint(ctx, constraint)
if err != nil {
b.Fatal(err)
}
}
}
8 changes: 6 additions & 2 deletions constraint/pkg/client/client_addtemplate_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func makeKind(i int) string {
func makeModuleSimple(i int) string {
kind := makeKind(i)
return fmt.Sprintf(`package %s
violation[msg] {
violation[{"msg": msg}] {
input.review.object.foo == input.parameters.foo
msg := sprintf("input.foo is %%v", [input.parameters.foo])
}`, kind)
Expand All @@ -44,6 +44,7 @@ identical(obj, review) {
obj.metadata.namespace == review.object.metadata.namespace
obj.metadata.name == review.object.metadata.name
}
violation[{"msg": msg}] {
input.review.kind.kind == "Ingress"
re_match("^(extensions|networking.k8s.io)$", input.review.kind.group)
Expand Down Expand Up @@ -98,7 +99,10 @@ func BenchmarkClient_AddTemplate(b *testing.B) {
b.StartTimer()

for _, ct := range cts {
_, _ = c.AddTemplate(ct)
_, err = c.AddTemplate(ct)
if err != nil {
b.Fatal(err)
}
}
}
})
Expand Down

0 comments on commit 8306a8d

Please sign in to comment.