Skip to content

Commit 11cdb8f

Browse files
committed
split compile and eval
Signed-off-by: Rita Zhang <[email protected]>
1 parent fe53db0 commit 11cdb8f

File tree

1 file changed

+66
-12
lines changed

1 file changed

+66
-12
lines changed

staging/src/k8s.io/apiserver/plugin/pkg/authorizer/webhook/webhook_v1_test.go

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -783,19 +783,36 @@ func TestStructuredAuthzConfigFeatureEnablement(t *testing.T) {
783783
}
784784

785785
func BenchmarkNoCELExpressionFeatureOff(b *testing.B) {
786-
benchmarkWebhookAuthorizer(b, []apiserver.WebhookMatchCondition{}, false)
786+
expressions := []apiserver.WebhookMatchCondition{}
787+
b.Run("compile", func(b *testing.B) {
788+
benchmarkNewWebhookAuthorizer(b, expressions, false)
789+
})
790+
b.Run("authorize", func(b *testing.B) {
791+
benchmarkWebhookAuthorize(b, expressions, false)
792+
})
787793
}
788794

789795
func BenchmarkNoCELExpressionFeatureOn(b *testing.B) {
790-
benchmarkWebhookAuthorizer(b, []apiserver.WebhookMatchCondition{}, true)
796+
expressions := []apiserver.WebhookMatchCondition{}
797+
b.Run("compile", func(b *testing.B) {
798+
benchmarkNewWebhookAuthorizer(b, expressions, true)
799+
})
800+
b.Run("authorize", func(b *testing.B) {
801+
benchmarkWebhookAuthorize(b, expressions, true)
802+
})
791803
}
792804
func BenchmarkWithOneCELExpressions(b *testing.B) {
793805
expressions := []apiserver.WebhookMatchCondition{
794806
{
795807
Expression: "request.user == 'alice'",
796808
},
797809
}
798-
benchmarkWebhookAuthorizer(b, expressions, true)
810+
b.Run("compile", func(b *testing.B) {
811+
benchmarkNewWebhookAuthorizer(b, expressions, true)
812+
})
813+
b.Run("authorize", func(b *testing.B) {
814+
benchmarkWebhookAuthorize(b, expressions, true)
815+
})
799816
}
800817
func BenchmarkWithTwoCELExpressions(b *testing.B) {
801818
expressions := []apiserver.WebhookMatchCondition{
@@ -806,7 +823,12 @@ func BenchmarkWithTwoCELExpressions(b *testing.B) {
806823
Expression: "request.uid == '1'",
807824
},
808825
}
809-
benchmarkWebhookAuthorizer(b, expressions, true)
826+
b.Run("compile", func(b *testing.B) {
827+
benchmarkNewWebhookAuthorizer(b, expressions, true)
828+
})
829+
b.Run("authorize", func(b *testing.B) {
830+
benchmarkWebhookAuthorize(b, expressions, true)
831+
})
810832
}
811833
func BenchmarkWithTwoComplexCELExpressions(b *testing.B) {
812834
expressions := []apiserver.WebhookMatchCondition{
@@ -817,7 +839,12 @@ func BenchmarkWithTwoComplexCELExpressions(b *testing.B) {
817839
Expression: "has(request.resourceAttributes) && request.resourceAttributes.namespace == 'kittensandponies'",
818840
},
819841
}
820-
benchmarkWebhookAuthorizer(b, expressions, true)
842+
b.Run("compile", func(b *testing.B) {
843+
benchmarkNewWebhookAuthorizer(b, expressions, true)
844+
})
845+
b.Run("authorize", func(b *testing.B) {
846+
benchmarkWebhookAuthorize(b, expressions, true)
847+
})
821848
}
822849
func BenchmarkWithManyCELExpressions(b *testing.B) {
823850
expressions := []apiserver.WebhookMatchCondition{
@@ -846,10 +873,37 @@ func BenchmarkWithManyCELExpressions(b *testing.B) {
846873
Expression: "has(request.resourceAttributes) && request.resourceAttributes.namespace == 'kittensandponies'",
847874
},
848875
}
849-
benchmarkWebhookAuthorizer(b, expressions, true)
876+
b.Run("compile", func(b *testing.B) {
877+
benchmarkNewWebhookAuthorizer(b, expressions, true)
878+
})
879+
b.Run("authorize", func(b *testing.B) {
880+
benchmarkWebhookAuthorize(b, expressions, true)
881+
})
850882
}
851883

852-
func benchmarkWebhookAuthorizer(b *testing.B, expressions []apiserver.WebhookMatchCondition, featureEnabled bool) {
884+
func benchmarkNewWebhookAuthorizer(b *testing.B, expressions []apiserver.WebhookMatchCondition, featureEnabled bool) {
885+
service := new(mockV1Service)
886+
service.statusCode = 200
887+
service.Allow()
888+
s, err := NewV1TestServer(service, serverCert, serverKey, caCert)
889+
if err != nil {
890+
b.Fatal(err)
891+
}
892+
defer s.Close()
893+
defer featuregatetesting.SetFeatureGateDuringTest(b, utilfeature.DefaultFeatureGate, features.StructuredAuthorizationConfiguration, featureEnabled)()
894+
895+
b.ResetTimer()
896+
for i := 0; i < b.N; i++ {
897+
// Create an authorizer with or without expressions to compile
898+
_, err := newV1Authorizer(s.URL, clientCert, clientKey, caCert, 0, noopAuthorizerMetrics(), expressions)
899+
if err != nil {
900+
b.Fatal(err)
901+
}
902+
}
903+
b.StopTimer()
904+
}
905+
906+
func benchmarkWebhookAuthorize(b *testing.B, expressions []apiserver.WebhookMatchCondition, featureEnabled bool) {
853907
attr := authorizer.AttributesRecord{
854908
User: &user.DefaultInfo{
855909
Name: "alice",
@@ -870,14 +924,14 @@ func benchmarkWebhookAuthorizer(b *testing.B, expressions []apiserver.WebhookMat
870924
}
871925
defer s.Close()
872926
defer featuregatetesting.SetFeatureGateDuringTest(b, utilfeature.DefaultFeatureGate, features.StructuredAuthorizationConfiguration, featureEnabled)()
927+
// Create an authorizer with or without expressions to compile
928+
wh, err := newV1Authorizer(s.URL, clientCert, clientKey, caCert, 0, noopAuthorizerMetrics(), expressions)
929+
if err != nil {
930+
b.Fatal(err)
931+
}
873932

874933
b.ResetTimer()
875934
for i := 0; i < b.N; i++ {
876-
// Create an authorizer with or without expressions to compile
877-
wh, err := newV1Authorizer(s.URL, clientCert, clientKey, caCert, 0, noopAuthorizerMetrics(), expressions)
878-
if err != nil {
879-
b.Fatal(err)
880-
}
881935
// Call authorize may or may not require cel evaluations
882936
_, _, err = wh.Authorize(context.Background(), attr)
883937
if err != nil {

0 commit comments

Comments
 (0)