-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.py
116 lines (92 loc) · 2.88 KB
/
bench.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import requests
import time
ADMIN_USER = "admin"
VIEW_USER = "viewer"
CLIENT_ID = "demo"
CLIENT_SECRET = "demo_secret"
AUTHZ_REALM = "authz"
NO_AUTHZ_REALM = "no-authz"
BASE_URL = "http://127.0.0.1:8080"
ITERATIONS = 10
SEED_USERS = 100
def millis(duration):
return int(round(duration * 1000))
def prepare(realm):
with requests.Session() as s:
auth_session_user(s, realm, ADMIN_USER)
r = s.get(f"{BASE_URL}/admin/realms/{realm}/users")
r.raise_for_status()
users = r.json()
if len(users) >= SEED_USERS:
return
for i in range(SEED_USERS):
name = f"user{i}"
r = s.post(
f"{BASE_URL}/admin/realms/{realm}/users",
json={
"enabled": True,
"username": name,
"firstName": name,
"lastName": name,
"email": f"{name}@test.keycloak.org",
"emailVerified": True,
},
)
r.raise_for_status()
def get_users(s, realm):
now = time.time()
r = s.get(f"{BASE_URL}/admin/realms/{realm}/users")
r.raise_for_status()
duration = time.time() - now
return duration
def auth_session_user(s, realm, user):
r = s.post(
f"{BASE_URL}/realms/{realm}/protocol/openid-connect/token",
data={
"username": user,
"password": user,
"grant_type": "password",
"client_id": "admin-cli",
},
)
r.raise_for_status()
access_token = r.json()["access_token"]
s.headers.update({"Authorization": f"Bearer {access_token}"})
def auth_session_client(s, realm):
r = s.post(
f"{BASE_URL}/realms/{realm}/protocol/openid-connect/token",
data={
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"grant_type": "client_credentials",
},
)
r.raise_for_status()
access_token = r.json()["access_token"]
s.headers.update({"Authorization": f"Bearer {access_token}"})
def test_user(realm, user):
with requests.Session() as s:
auth_session_user(s, realm, user)
times = []
for i in range(ITERATIONS):
duration = get_users(s, realm)
times.append(millis(duration))
print(f"{user}:", times)
def test_client(realm):
with requests.Session() as s:
auth_session_client(s, realm)
times = []
for i in range(ITERATIONS):
duration = get_users(s, realm)
times.append(millis(duration))
print("service account:", times)
def main():
for realm in [AUTHZ_REALM, NO_AUTHZ_REALM]:
print(f"# Realm {realm}")
prepare(realm)
for u in [VIEW_USER, ADMIN_USER]:
test_user(realm, u)
test_client(realm)
print()
if __name__ == "__main__":
main()