Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce System.Mutate runtime by 87% (#1454)
* Benchmark System.Mutate Signed-off-by: Will Beason <willbeason@google.com> * Early exit Mutate if no mutations Signed-off-by: Will Beason <willbeason@google.com> Co-authored-by: Oren Shomron <shomron@gmail.com>
- Loading branch information
Showing
3 changed files
with
75 additions
and
6 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
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,62 @@ | ||
package mutation | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/open-policy-agent/gatekeeper/apis/mutations/v1alpha1" | ||
"github.com/open-policy-agent/gatekeeper/pkg/mutation/match" | ||
"github.com/open-policy-agent/gatekeeper/pkg/mutation/mutators" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
func makeValue(v interface{}) runtime.RawExtension { | ||
v2 := map[string]interface{}{ | ||
"value": v, | ||
} | ||
j, err := json.Marshal(v2) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return runtime.RawExtension{Raw: j} | ||
} | ||
|
||
func assign(value interface{}, location string) *v1alpha1.Assign { | ||
result := &v1alpha1.Assign{ | ||
Spec: v1alpha1.AssignSpec{ | ||
ApplyTo: []match.ApplyTo{{ | ||
Groups: []string{"*"}, | ||
Versions: []string{"*"}, | ||
Kinds: []string{"*"}, | ||
}}, | ||
Location: location, | ||
Parameters: v1alpha1.Parameters{ | ||
Assign: makeValue(value), | ||
}, | ||
}, | ||
} | ||
|
||
return result | ||
} | ||
|
||
func BenchmarkSystem_Mutate(b *testing.B) { | ||
s := NewSystem() | ||
|
||
a := assign("", "spec") | ||
m, err := mutators.MutatorForAssign(a) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
err = s.Upsert(m) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
for i := 0; i < b.N; i++ { | ||
u := &unstructured.Unstructured{} | ||
|
||
_, _ = s.Mutate(u, nil) | ||
} | ||
} |
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