-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Benchmark AddConstraint function (#168)
* 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
Showing
2 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters