Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mutation] Cache tester in AssignMetadata like we do in Assign mutato…
…rs (#1442)
- Loading branch information
Will Beason
committed
Jul 17, 2021
1 parent
152e102
commit 938edcd
Showing
2 changed files
with
76 additions
and
7 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
65 changes: 65 additions & 0 deletions
65
pkg/mutation/mutators/assignmeta_mutator_benchmark_test.go
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,65 @@ | ||
package mutators | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/open-policy-agent/gatekeeper/apis/mutations/v1alpha1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
func assignMetadata(value interface{}, location string) *v1alpha1.AssignMetadata { | ||
result := &v1alpha1.AssignMetadata{ | ||
Spec: v1alpha1.AssignMetadataSpec{ | ||
Location: location, | ||
Parameters: v1alpha1.MetadataParameters{ | ||
Assign: makeValue(value), | ||
}, | ||
}, | ||
} | ||
|
||
return result | ||
} | ||
|
||
func BenchmarkAssignMetadataMutator_Always(b *testing.B) { | ||
mutator, err := MutatorForAssignMetadata(assignMetadata("bar", "metadata.labels.foo")) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
// Create a fresh object each time as AssignMetadata.Mutate does nothing if | ||
// a label/annotation already exists. Thus, this test is for when we do | ||
// actually make the mutation. | ||
|
||
// The performance cost of instantiating the Unstructured is negligible | ||
// compared to the time to perform Mutate. | ||
obj := &unstructured.Unstructured{ | ||
Object: make(map[string]interface{}), | ||
} | ||
_, _ = mutator.Mutate(obj) | ||
} | ||
} | ||
|
||
func BenchmarkAssignMetadataMutator_Never(b *testing.B) { | ||
mutator, err := MutatorForAssignMetadata(assignMetadata("bar", "metadata.labels.foo")) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
obj := &unstructured.Unstructured{ | ||
Object: make(map[string]interface{}), | ||
} | ||
_, err = mutator.Mutate(obj) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
// Use the same object each time as AssignMetadata.Mutate does nothing if | ||
// a label/annotation already exists. Thus, this test is for the case where | ||
// no mutation is necessary. | ||
_, _ = mutator.Mutate(obj) | ||
} | ||
} |