Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark AddConstraint function #168

Merged
merged 2 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
willbeason marked this conversation as resolved.
Show resolved Hide resolved
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