Skip to content

Commit d5c5922

Browse files
committed
Add benchmark for cached authorizer
1 parent d869792 commit d5c5922

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

e2core/auth/authorizer_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package auth
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"net/http"
67
"net/http/httptest"
78
"strings"
@@ -350,3 +351,34 @@ func TestAuthorizerCache_ExpiringEntry(t *testing.T) {
350351
assert.True(t, tc.assertOpts(t, opts))
351352
}
352353
}
354+
func BenchmarkCachedAuthorizer(b *testing.B) {
355+
opts := int32(0)
356+
357+
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
358+
atomic.AddInt32(&opts, 1)
359+
w.WriteHeader(http.StatusOK)
360+
_ = json.NewEncoder(w).Encode(&TenantInfo{
361+
AuthorizedParty: fmt.Sprintf("tester-%d", opts),
362+
Environment: fmt.Sprintf("env-%d", opts),
363+
ID: fmt.Sprintf("123-%d", opts),
364+
Name: fmt.Sprintf("functionname-%d", opts),
365+
})
366+
}))
367+
368+
authzCache := newAuthorizationCache(common.SystemTime(), 10*time.Minute)
369+
370+
authorizer := &AuthzClient{
371+
httpClient: svr.Client(),
372+
location: svr.URL + "/api/v2/tenant/",
373+
cache: authzCache,
374+
}
375+
376+
for i := 0; i < b.N; i++ {
377+
sfx := b.N / 1000
378+
_, _ = authorizer.Authorize(
379+
NewAccessToken(fmt.Sprintf("sometoken-%d", sfx)),
380+
fmt.Sprintf("ident-%d", sfx),
381+
fmt.Sprintf("namespace-%d", sfx),
382+
fmt.Sprintf("fnName-%d", sfx))
383+
}
384+
}

0 commit comments

Comments
 (0)