From 973a51c82f5b2a3240c93552b0c53a927cec38af Mon Sep 17 00:00:00 2001 From: Alexander Yastrebov Date: Mon, 10 Feb 2025 12:30:25 +0100 Subject: [PATCH] filters/auth: add jwtMetrics benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ```console $ go test ./filters/auth/ -run=NONE '-bench=BenchmarkJwtMetrics$' -count=10 | benchstat - goos: linux goarch: amd64 pkg: github.com/zalando/skipper/filters/auth cpu: Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz │ - │ │ sec/op │ JwtMetrics-8 2.279µ ± 4% │ - │ │ B/op │ JwtMetrics-8 747.0 ± 0% │ - │ │ allocs/op │ JwtMetrics-8 19.00 ± 0% ``` Signed-off-by: Alexander Yastrebov --- filters/auth/jwt_metrics_test.go | 40 +++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/filters/auth/jwt_metrics_test.go b/filters/auth/jwt_metrics_test.go index 3c6b324084..672a352ff7 100644 --- a/filters/auth/jwt_metrics_test.go +++ b/filters/auth/jwt_metrics_test.go @@ -14,6 +14,7 @@ import ( "github.com/zalando/skipper/eskip" "github.com/zalando/skipper/filters/auth" "github.com/zalando/skipper/filters/builtin" + "github.com/zalando/skipper/filters/filtertest" "github.com/zalando/skipper/metrics/metricstest" "github.com/zalando/skipper/proxy" "github.com/zalando/skipper/proxy/proxytest" @@ -359,7 +360,7 @@ func TestJwtMetricsArgs(t *testing.T) { }) } -func marshalBase64JSON(t *testing.T, v any) string { +func marshalBase64JSON(t testing.TB, v any) string { d, err := json.Marshal(v) if err != nil { t.Fatalf("failed to marshal json: %v, %v", v, err) @@ -380,3 +381,40 @@ func BenchmarkJwtMetrics_CreateFilter(b *testing.B) { _, _ = spec.CreateFilter(args) } } + +func BenchmarkJwtMetrics(b *testing.B) { + spec := auth.NewJwtMetrics() + args := []any{`{issuers: [foo, bar], optOutAnnotations: [oauth.disabled], optOutHosts: [ '^.+[.]domain[.]test$' ]}`} + + f, err := spec.CreateFilter(args) + require.NoError(b, err) + + m := &metricstest.MockMetrics{} + defer m.Close() + + ctx := &filtertest.Context{ + FRequest: &http.Request{ + Method: "GET", Host: "foo.test", + Header: http.Header{ + "Authorization": []string{ + "Bearer header." + marshalBase64JSON(b, map[string]any{"iss": "baz"}) + ".signature", + }, + }, + }, + FResponse: &http.Response{StatusCode: http.StatusOK}, + FMetrics: m, + } + + f.Request(ctx) + f.Response(ctx) + m.WithCounters(func(counters map[string]int64) { + require.Equal(b, map[string]int64{"GET.foo_test.200.invalid-issuer": 1}, counters) + }) + + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + f.Request(ctx) + f.Response(ctx) + } +}