From 738387f2debea67f67068d58ce25cb279a612756 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Thu, 7 Nov 2024 17:23:35 +0100 Subject: [PATCH] Add benchmark tests to get account with claims (#2761) * Add benchmark tests to get account with claims * add users to account objects * remove hardcoded env --- management/server/account_test.go | 104 ++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/management/server/account_test.go b/management/server/account_test.go index 31405e3af1c..6a2d85fe8f7 100644 --- a/management/server/account_test.go +++ b/management/server/account_test.go @@ -982,6 +982,110 @@ func TestAccountManager_DeleteAccount(t *testing.T) { } } +func BenchmarkTest_GetAccountWithclaims(b *testing.B) { + claims := jwtclaims.AuthorizationClaims{ + Domain: "example.com", + UserId: "pvt-domain-user", + DomainCategory: PrivateCategory, + } + + publicClaims := jwtclaims.AuthorizationClaims{ + Domain: "test.com", + UserId: "public-domain-user", + DomainCategory: PublicCategory, + } + + am, err := createManager(b) + if err != nil { + b.Fatal(err) + return + } + id, err := am.getAccountIDWithAuthorizationClaims(context.Background(), claims) + if err != nil { + b.Fatal(err) + } + + pid, err := am.getAccountIDWithAuthorizationClaims(context.Background(), publicClaims) + if err != nil { + b.Fatal(err) + } + + users := genUsers("priv", 100) + + acc, err := am.Store.GetAccount(context.Background(), id) + if err != nil { + b.Fatal(err) + } + acc.Users = users + + err = am.Store.SaveAccount(context.Background(), acc) + if err != nil { + b.Fatal(err) + } + + userP := genUsers("pub", 100) + + pacc, err := am.Store.GetAccount(context.Background(), pid) + if err != nil { + b.Fatal(err) + } + + pacc.Users = userP + + err = am.Store.SaveAccount(context.Background(), pacc) + if err != nil { + b.Fatal(err) + } + + b.Run("public without account ID", func(b *testing.B) { + //b.ResetTimer() + for i := 0; i < b.N; i++ { + _, err := am.getAccountIDWithAuthorizationClaims(context.Background(), publicClaims) + if err != nil { + b.Fatal(err) + } + } + }) + + b.Run("private without account ID", func(b *testing.B) { + //b.ResetTimer() + for i := 0; i < b.N; i++ { + _, err := am.getAccountIDWithAuthorizationClaims(context.Background(), claims) + if err != nil { + b.Fatal(err) + } + } + }) + + b.Run("private with account ID", func(b *testing.B) { + claims.AccountId = id + //b.ResetTimer() + for i := 0; i < b.N; i++ { + _, err := am.getAccountIDWithAuthorizationClaims(context.Background(), claims) + if err != nil { + b.Fatal(err) + } + } + }) + +} + +func genUsers(p string, n int) map[string]*User { + users := map[string]*User{} + now := time.Now() + for i := 0; i < n; i++ { + users[fmt.Sprintf("%s-%d", p, i)] = &User{ + Id: fmt.Sprintf("%s-%d", p, i), + Role: UserRoleAdmin, + LastLogin: now, + CreatedAt: now, + Issued: "api", + AutoGroups: []string{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}, + } + } + return users +} + func TestAccountManager_AddPeer(t *testing.T) { manager, err := createManager(t) if err != nil {