Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
parkpow
GitHub Repository: parkpow/deep-license-plate-recognition
Path: blob/master/benchmark/benchmark_parkpow.py
641 views
1
import argparse
2
import datetime
3
4
import requests
5
6
7
def parse_arguments():
8
parser = argparse.ArgumentParser(description="Measure ParkPow load time.")
9
parser.add_argument(
10
"--url",
11
help="The base URL of the ParkPow website. Defaults to localhost.",
12
default="http://127.0.0.1:8000",
13
)
14
parser.add_argument("--email", help="The email address to be used for login")
15
parser.add_argument(
16
"--password", help="The corresponding password for the provided email."
17
)
18
return parser.parse_args()
19
20
21
def login(session, url, email, password):
22
login_url = f"{url}/accounts/login/"
23
24
res1 = session.get(login_url)
25
csrf_token = res1.cookies["csrftoken"]
26
res2 = session.post(
27
login_url,
28
data={"login": email, "password": password, "csrfmiddlewaretoken": csrf_token},
29
)
30
return "dashboard" in res2.url
31
32
33
def _get_load_time_or_none(res):
34
if res.status_code == 200:
35
return res.elapsed.microseconds / 1000
36
else:
37
return None
38
39
40
def get_first_plate(session, url):
41
_url = f"{url}/api/v1/vehicles/"
42
res = session.get(_url)
43
if res.status_code == 200:
44
return res.json()["results"][0]["license_plate"]
45
return False
46
47
48
def get_first_camera(session, url):
49
_url = f"{url}/api/v1/visit-list/"
50
res = session.get(_url)
51
if res.status_code == 200:
52
return res.json()["results"][0]["start_cam"]["name"]
53
54
55
def get_load_time(session, url, path="dashboard", days=1):
56
url = f"{url}/{path}/"
57
58
time_delta = datetime.timedelta(days=days)
59
dt_from = datetime.datetime.now() - time_delta
60
61
res = session.get(url, params={"from": dt_from})
62
63
return _get_load_time_or_none(res)
64
65
66
def get_load_time_search_plate(session, url, plate, path="dashboard", days=1):
67
url = f"{url}/{path}/"
68
time_delta = datetime.timedelta(days=days)
69
70
dt_from = datetime.datetime.now() - time_delta
71
res = session.get(url, params={"from": dt_from, "plate": plate})
72
73
return _get_load_time_or_none(res)
74
75
76
def get_load_time_filter_by_camera(session, url, camera_name, path="dashboard", days=1):
77
url = f"{url}/{path}/"
78
time_delta = datetime.timedelta(days=days)
79
80
dt_from = datetime.datetime.now() - time_delta
81
res = session.get(url, params={"from": dt_from, "camera_name": camera_name})
82
83
return _get_load_time_or_none(res)
84
85
86
def get_result(session, url, path, plate, camera):
87
for day in [1, 7, 14, 30, 60]:
88
load_time = get_load_time(session, url, path, day)
89
load_time_plate = get_load_time_search_plate(session, url, plate, path, day)
90
load_time_camera = get_load_time_filter_by_camera(
91
session, url, camera, path, day
92
)
93
94
load_time_str = f"{load_time}ms" if load_time else "failed to load"
95
load_time_plate_str = (
96
f"{load_time_plate}ms" if load_time_plate else "failed to load"
97
)
98
load_time_camera_str = (
99
f"{load_time_camera}ms" if load_time_camera else "failed to load"
100
)
101
102
yield dict(
103
day=day,
104
no_filter=load_time_str,
105
filter_plate=load_time_plate_str,
106
filter_camera=load_time_camera_str,
107
)
108
109
110
def print_table(title, results):
111
if not results:
112
return
113
print("| -------------------------------------------------------------------- |")
114
print(f"|{title.center(70)}|")
115
print("| -------------------------------------------------------------------- |")
116
print("| Range | Without filter | License plate search | Filter 1 camera |")
117
print("| --------- | --------------- | -------------------- | --------------- |")
118
for result in results:
119
print(
120
"| {day:2n} day(s) | {no_filter:^15s} | {filter_plate:^20s} | {filter_camera:^15s} |".format(
121
**result
122
)
123
)
124
print("| -------------------------------------------------------------------- |")
125
126
127
def main():
128
session = requests.session()
129
args = parse_arguments()
130
131
if not args.email or not args.password:
132
print("Provide a valid credential with --email and --password. Exiting...")
133
return
134
135
# Login
136
print("Logging in...")
137
if not login(session, args.url, args.email, args.password):
138
print("Login failed. Exiting...")
139
return
140
141
print()
142
143
plate = get_first_plate(session, args.url)
144
camera = get_first_camera(session, args.url)
145
146
if not plate:
147
print("Failed to get a plate to search")
148
return
149
150
if not camera:
151
print("Failed to get a camera for filtering")
152
return
153
154
# Dashboard
155
results = get_result(session, args.url, "dashboard", plate, camera)
156
print_table("Dashboard", results)
157
158
print()
159
print()
160
161
# Dashboard statistics
162
results = get_result(session, args.url, "dashboard/?statistics=true", plate, camera)
163
print_table("Dashboard Statistics", results)
164
165
print()
166
print()
167
168
# Dashboard chart
169
results = get_result(session, args.url, "dashboard/?chart-data=true", plate, camera)
170
print_table("Dashboard Chart", results)
171
172
print()
173
print()
174
175
# Alert
176
results = get_result(session, args.url, "alerts", plate, camera)
177
print_table("Alert", results)
178
179
180
if __name__ == "__main__":
181
main()
182
183