Skip to content

Commit

Permalink
filters/auth: add jwtMetrics benchmark (#3402)
Browse files Browse the repository at this point in the history
```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 <alexander.yastrebov@zalando.de>
  • Loading branch information
AlexanderYastrebov authored Feb 10, 2025
1 parent c843ca6 commit 128b437
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion filters/auth/jwt_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}

0 comments on commit 128b437

Please sign in to comment.