-
Notifications
You must be signed in to change notification settings - Fork 732
/
assignmeta_mutator_benchmark_test.go
65 lines (55 loc) 路 1.65 KB
/
assignmeta_mutator_benchmark_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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)
}
}