Skip to content

Commit

Permalink
Add benchmark for cached authorizer
Browse files Browse the repository at this point in the history
  • Loading branch information
javorszky committed Mar 20, 2023
1 parent d869792 commit d5c5922
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions e2core/auth/authorizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -350,3 +351,34 @@ func TestAuthorizerCache_ExpiringEntry(t *testing.T) {
assert.True(t, tc.assertOpts(t, opts))
}
}
func BenchmarkCachedAuthorizer(b *testing.B) {
opts := int32(0)

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&opts, 1)
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(&TenantInfo{
AuthorizedParty: fmt.Sprintf("tester-%d", opts),
Environment: fmt.Sprintf("env-%d", opts),
ID: fmt.Sprintf("123-%d", opts),
Name: fmt.Sprintf("functionname-%d", opts),
})
}))

authzCache := newAuthorizationCache(common.SystemTime(), 10*time.Minute)

authorizer := &AuthzClient{
httpClient: svr.Client(),
location: svr.URL + "/api/v2/tenant/",
cache: authzCache,
}

for i := 0; i < b.N; i++ {
sfx := b.N / 1000
_, _ = authorizer.Authorize(
NewAccessToken(fmt.Sprintf("sometoken-%d", sfx)),
fmt.Sprintf("ident-%d", sfx),
fmt.Sprintf("namespace-%d", sfx),
fmt.Sprintf("fnName-%d", sfx))
}
}

0 comments on commit d5c5922

Please sign in to comment.