-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance_analyzer.py
215 lines (163 loc) · 6.57 KB
/
performance_analyzer.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import datetime
import json
import time
import urllib.parse
from enum import Enum
import requests
from fastapi import status
from matplotlib import pyplot as plt
VERSION = "1.0.0"
URL = "http://localhost:8000/api"
# URL = "https://zolza-hairstyles.pl/api"
EMAIL = "test@test.pl"
PASSWORD = "Testing5!"
class AuthEndpoints(Enum):
LOGIN = "auth/login"
LOGOUT = "auth/logout"
class ResourceEndpoints(Enum):
ME = "users/me"
USERS = "users"
SESSIONS = "auth/sessions"
SETTINGS = "settings"
APPOINTMENT_SLOTS = "appointments/slots"
MY_APPOINTMENTS = "appointments/mine"
ALL_APPOINTMENTS = "appointments/all"
SERVICES = "services"
SERVICE_DETAILS = "services/details"
class PerformanceAnalyzer:
url: str
auth_endpoints: AuthEndpoints
resource_endpoints: ResourceEndpoints
email: str
password: str
iterations: int
bearer: str
performance_data: dict = {"iterations": [], "total_run_time": None}
def __init__(
self,
url,
auth_endpoints: AuthEndpoints,
resource_endpoints: ResourceEndpoints,
email,
password,
iterations: int,
report_prefix: str | None = None,
):
self.url = url
self.auth_endpoints = auth_endpoints
self.resource_endpoints = resource_endpoints
self.email = email
self.password = password
self.iterations = iterations
self.report_prefix = report_prefix
def _build_endpoint_url(self, endpoint: AuthEndpoints | ResourceEndpoints):
return f"{self.url}/{endpoint.value}"
def _get_auth_headers(self):
if not self.bearer:
raise Exception("Not Authenticated")
return {"Authorization": f"Bearer {self.bearer}"}
def _ensure_request_success(self, response):
if response.status_code != status.HTTP_200_OK:
raise Exception("Request failed: ", response.json())
def _login(self):
payload = f"grant_type=password&username={urllib.parse.quote(self.email)}&password={urllib.parse.quote(self.password)}"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
response = requests.post(
self._build_endpoint_url(self.auth_endpoints.LOGIN),
headers=headers,
data=payload,
)
self._ensure_request_success(response)
self.bearer = response.json()["access_token"]
def _logout(self):
response = requests.post(
self._build_endpoint_url(self.auth_endpoints.LOGOUT),
headers=self._get_auth_headers(),
)
self._ensure_request_success(response)
def _test_request(self, endpoint: ResourceEndpoints):
response = requests.get(
self._build_endpoint_url(endpoint), headers=self._get_auth_headers()
)
self._ensure_request_success(response)
def _test_endpoints(self):
endpoints_times = []
for endpoint in self.resource_endpoints:
start_time = time.time()
self._test_request(endpoint)
end_time = time.time()
endpoint_time = end_time - start_time
print(f"{endpoint.name} took {endpoint_time}")
endpoints_times.append({"name": endpoint.name, "time": endpoint_time})
return endpoints_times
def _create_graphs(self, filename):
endpoints = [
endpoint["name"]
for endpoint in self.performance_data["iterations"][0]["endpoints_times"]
]
endpoints_count = len(self.performance_data["iterations"][0]["endpoints_times"])
endpoints_times = [[] for _ in range(endpoints_count)]
for i in range(endpoints_count):
for j in range(len(self.performance_data["iterations"])):
endpoints_times[i].append(
self.performance_data["iterations"][j]["endpoints_times"][i]["time"]
)
endpoints_average_times = []
for endpoint_times in endpoints_times:
endpoints_average_times.append(sum(endpoint_times) / self.iterations)
plt.figure(figsize=(10, 6), num="API performance")
plt.barh(endpoints, endpoints_average_times, color="skyblue")
plt.xlabel("Time (seconds)")
plt.ylabel("Endpoints")
plt.title("Time taken by each endpoint")
plt.gca().invert_yaxis() # Invert y-axis to have the endpoints listed from top to bottom
plt.tight_layout()
plt.savefig(f"{filename}.png")
plt.savefig(f"{filename}.pdf")
plt.show()
def _save_data(self, filename):
with open(f"{filename}.json", "w") as output_file:
output_file.write(json.dumps(self.performance_data, indent=2))
def run(self):
run_start_time = time.time()
for iteration in range(self.iterations):
print(f"Starting iteration #{iteration + 1}")
iteration_start_time = time.time()
login_start_time = time.time()
self._login()
login_end_time = time.time()
login_time = login_end_time - login_start_time
print(f"LOGIN took {login_time}")
test_endpoints_times = self._test_endpoints()
logout_start_time = time.time()
self._logout()
logout_end_time = time.time()
logout_time = logout_end_time - logout_start_time
print(f"LOGOUT took {logout_time}")
endpoints_times = [
{"name": "LOGIN", "time": login_time},
*test_endpoints_times,
{"name": "LOGOUT", "time": logout_time},
]
iteration_end_time = time.time()
iteration_time = iteration_end_time - iteration_start_time
print(f"Iteration #{iteration + 1} time: {iteration_time}")
self.performance_data["iterations"].append(
{"endpoints_times": endpoints_times, "iteration_time": iteration_time}
)
run_end_time = time.time()
total_run_time = run_end_time - run_start_time
print(f"Total run time: {total_run_time}")
self.performance_data["total_run_time"] = total_run_time
timestamp = datetime.datetime.now().strftime("%d-%m-%Y_%H-%M-%S")
filename = f"{self.report_prefix} PA V{VERSION} {timestamp} I-{self.iterations}"
self._save_data(filename)
self._create_graphs(filename)
def main():
report_prefix = input("Please enter report prefix: ")
performance_analyzer = PerformanceAnalyzer(
URL, AuthEndpoints, ResourceEndpoints, EMAIL, PASSWORD, 10, report_prefix
)
performance_analyzer.run()
if __name__ == "__main__":
main()